Skip to content

Instantly share code, notes, and snippets.

@piyushrajput
Forked from joyrexus/http-errors.js
Created May 7, 2020 13:51
Show Gist options
  • Select an option

  • Save piyushrajput/2b5fca266d0a1cdd46223c51ff218e82 to your computer and use it in GitHub Desktop.

Select an option

Save piyushrajput/2b5fca266d0a1cdd46223c51ff218e82 to your computer and use it in GitHub Desktop.
HTTP Error classes in Node.js

Sometimes you want to wrap error objects with application-specific errors for your stack traces.

Your custom error constructor:

function CustomError(e, extra) {
  Error.call(this);
  Error.captureStackTrace(this, CustomError);
  this.id = e.id;
  this.name = 'CustomError';
  this.message = e.message
}

require('util').inherits(CustomError, Error);

Example usage:

function doWork(callback) { doErrorProneWork(funtion(err, result) { if (err) return callback(new CustomError(err));

  ...
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment