Skip to content

Instantly share code, notes, and snippets.

@dlueth
Last active March 27, 2018 15:48
Show Gist options
  • Save dlueth/f4a49c7caa974de4e3a29f1e9cae7bec to your computer and use it in GitHub Desktop.
Save dlueth/f4a49c7caa974de4e3a29f1e9cae7bec to your computer and use it in GitHub Desktop.
NodeJS: Custom errors
'use strict';
class CustomError extends Error {
constructor(...args) {
super(...args);
Object.defineProperty(this, 'name', { value: this.constructor.name });
// This is for Node only...
Error.captureStackTrace(this, this.constructor);
// ... whereas use this for Browser instead
/*
if(Error.captureStackTrace) {
Error.captureStackTrace(this, CustomError);
}
*/
}
}
class ExtendedError extends CustomError {
constructor(...args) {
super(...args);
}
}
try {
throw new ExtendedError('gnarf')
} catch(err) {
console.log(err.name);
console.log(err);
}
module.exports = CustomError;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment