Skip to content

Instantly share code, notes, and snippets.

@sabrinaluo
Created December 30, 2015 07:16
Show Gist options
  • Save sabrinaluo/39ee7d94a5227834b212 to your computer and use it in GitHub Desktop.
Save sabrinaluo/39ee7d94a5227834b212 to your computer and use it in GitHub Desktop.
function error(err, options) {
var originalError = null;
if (typeof err.message === 'string' && err.message !== '') {
if (typeof options === 'string' || (options && options.message)) {
originalError = util.copy(err);
originalError.message = err.message;
}
}
err.message = err.message || null;
if (typeof options === 'string') {
err.message = options;
} else if (typeof options === 'object' && options !== null) {
util.update(err, options);
if (options.message)
err.message = options.message;
if (options.code || options.name)
err.code = options.code || options.name;
if (options.stack)
err.stack = options.stack;
}
if (typeof Object.defineProperty === 'function') {
Object.defineProperty(err, 'name', {
writable: true,
enumerable: false
});
Object.defineProperty(err, 'message', {
enumerable: true
});
}
err.name = options && options.name || err.name || err.code || 'Error';
err.time = new Date();
if (originalError) err.originalError = originalError;
return err;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment