When extending the Error
object, I'm frustrated that calling super(originalError)
results in a normal stack trace printed to STDOUT. I want my logs to be formatted in JSON so that they're consumable by logstash/Kibana. Stack traces in Kibana that include \n
are split into multiple entries, as it does not really know what to do with that input apparently. I can avoid this through a little modification before logging
function logError(errorDetails, stack) {
logger.error({ errorDetails, stack: sanitizeLineBreaks(stack) } // removes `\n`
}
However the error object I'm implementing which does this looks like:
class ServerError extends Error {
constructor(originalError, errorDetails, stack) {
super(originalError) // <- this is the problem
Error.captureStackTrace(this, ServerError)
// stuff ...
logError(originalError, stack)
}
}
However, calling super(originalError)
prints to the console with new line characters; this makes for ugly entries where each line of the trace is a separate entry in the Kibana GUI. I'm not getting any complaints in the console when I do not call super(originalError)
, but I am still hesitant to remove it.