Skip to content

Instantly share code, notes, and snippets.

Created February 26, 2014 18:54
What would you like to do?
Creating custom Error objects 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.captureStackTrace(this, CustomError); =; = '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