Created
December 30, 2015 07:16
-
-
Save sabrinaluo/39ee7d94a5227834b212 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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