Skip to content

Instantly share code, notes, and snippets.

@calvintwr
Created June 28, 2020 18:52
Show Gist options
  • Save calvintwr/b2723468ee54e023dbe53acc48ea4807 to your computer and use it in GitHub Desktop.
Save calvintwr/b2723468ee54e023dbe53acc48ea4807 to your computer and use it in GitHub Desktop.
Express res.sendError
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