Skip to content

Instantly share code, notes, and snippets.

@hpherzog
Last active August 29, 2015 14:02
Show Gist options
  • Select an option

  • Save hpherzog/026c51a5d7d342ae2105 to your computer and use it in GitHub Desktop.

Select an option

Save hpherzog/026c51a5d7d342ae2105 to your computer and use it in GitHub Desktop.
How to inherit a custom error object in node.js.
"use strict";
var util = require("util");
var errors = {};
errors.ErrorA = function ErrorA(message) {
Error.call(this);
Error.captureStackTrace(this, this.constructor);
this.name = errors.ErrorA.name;
this.message = message;
};
util.inherits(errors.ErrorA, Error);
try {
throw new errors.ErrorA("My Error A!");
} catch(err) {
console.log("Name: " + err.name);
console.log("Message: " + err.message);
console.log("Stack: " + err.stack);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment