2

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.

Is super(originalError) necessary when extending the Error object?

Hyyan Abo Fakher
  • 3,497
  • 3
  • 21
  • 35
1252748
  • 14,597
  • 32
  • 109
  • 229
  • `super()` does not print anything to the console?! It just creates the `Error` instance. Or do you have an overwritten `Error` constructor in your environment? – Bergi Sep 12 '18 at 20:08
  • @Bergi `However calling super(originalError) prints to the console with new line characters` Seems like I've misled somewhere. Can you point to what made you think it didn't print anything? – 1252748 Sep 12 '18 at 20:09
  • @Bergi Hmm I am not totally following. `Super(originalError)` _does_ print to the console in my case. – 1252748 Sep 12 '18 at 20:10
  • @Bergi I only `extend` `Error`, not overwrite it. – 1252748 Sep 12 '18 at 20:11
  • Can you show a complete program that reproduces the problem? Creating a `new Error(…)` does not print anything to the console in my node - except when you do `console.log(new Error(…))`. – Bergi Sep 12 '18 at 20:12
  • The only reason it'd print anything is if you're doing this from the console; constructing an error does no logging, AFAIK. In the console it just prints whatever the last value was. E.g., if you type `f = new Foo(new Error('hi')); null` it prints `null`, because that was the last evaled expr. – Dave Newton Sep 12 '18 at 20:12
  • Does it still happen when you remove the `logError(originalError, stack)` from the constructor? A constructor should not do such a thing anyway, it should only initialise the instance. – Bergi Sep 12 '18 at 20:13
  • Oh, yeah, I guess there's the part where it's explicitly logged :/ – Dave Newton Sep 12 '18 at 20:13
  • @Bergi yes it still logs the stack when I remove `logError(originalError, stack)` – 1252748 Sep 12 '18 at 20:20

1 Answers1

1

(Ignoring your issue with logging)

Is super(originalError) necessary when extending the Error object?

Yes. Every class that extends another and defines a custom constructor must call super() to create the instance.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • That does answer the question, so thank you. Do you know if there is any way to suppress it printing the error stack however? – 1252748 Sep 12 '18 at 20:21
  • Calling the `Error` constructor should not print anything anywhere. Please create a [mcve] in your question – Bergi Sep 12 '18 at 20:22