Answer the question
In order to leave comments, you need to log in
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
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;
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 questionAsk a Question
731 491 924 answers to any question