Created
June 28, 2020 18:52
-
-
Save calvintwr/b2723468ee54e023dbe53acc48ea4807 to your computer and use it in GitHub Desktop.
Express res.sendError
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 sendError(message, options) { | |
var response = { | |
success: false, | |
message: message || 'Nil message' | |
} | |
if (options.error) { | |
if (!response.debug) response.debug = {} | |
if (options.error instanceof Error) { | |
response.debug.message = options.error.message | |
response.debug.stack = options.error.stack | |
} else { | |
Object.assign(response.debug, options.error) | |
} | |
} | |
if (options.hint) { | |
if (!response.debug) response.debug = { hint: options.hint } | |
response.debug.hint = options.hint | |
} | |
if (options.timestamp) { | |
if (!response.debug) response.debug = { timestamp: new Date().getTime() } | |
response.debug.timestamp = new Date().getTime() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment