D
D
Denis11112018-09-22 09:47:03
Node.js
Denis1111, 2018-09-22 09:47:03

How to rewrite this code in ES6?

let util = require('util'),
     http = require('http');

function HttpError(status, message) {
  Error.apply(this, arguments);
  Error.captureStackTrace(this, HttpError);

  this.status = status;
  this.message = message || http.STATUS_CODES[status] || "Error";
}

util.inherits(HttpError, Error);

HttpError.prototype.name = 'HttpError';

exports.HttpError = HttpError;

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Masterov, 2018-09-23
@Denis1111

Node.js v8.12/10.11

'use strict';

const { STATUS_CODES } = require('http');
// https://nodejs.org/api/esm.html#esm_enabling
// import { STATUS_CODES } from 'http';

class HttpError extends Error {
  constructor(status, message=STATUS_CODES[status]) {
    super(message);

    Error.captureStackTrace(this, this.constructor);

    this.name = this.constructor.name;
    // this.status = status;
    // https://nodejs.org/api/errors.html#errors_error_code
    this.code = status;
    this.message = message || 'Error';
  }
}

module.exports = HttpError;
// export default HttpError;

V
Vahe, 2018-09-22
@vahe_2000

import util from 'util';
import http from 'http';

function HttpError(status, message) {
  Error.apply(this, arguments);
  Error.captureStackTrace(this, HttpError);

  this.status = status;
  this.message = message || http.STATUS_CODES[status] || "Error";
}

util.inherits(HttpError, Error);

HttpError.prototype.name = 'HttpError';

export {HttpError};

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question