Extending the standard Node / javascript error, modifying status

Viewed 13

I wanted to extend the normal NodeJS error (running Node v16.16) that my own collection of asserts throws.

class FailError extends Error {
  constructor(...whatever) {
    super(...whatever);
    // @ts-ignore  should trim all the way to module-job (COULDDO filter from keyword onward)
    // this.stack = this.stack
    //   .split(/\r\n|\r|\n/)
    //   .slice(2, -5)
    //   .map(line => (stackLine.test(line) ? '\u001b[38;5;112m' + line + '\u001b[0m' : line))
    //   .join('\n');
    this.stack = this.stack
  }
}

This works fine, if I only change for example this.name of the error:

    ↓
FailError: FAILURE ensureTrue(): ensureFileExists: no such ……
(followed by stacktrace)

However I start getting another, internal exception, as soon as I touch the stack. That's what I tried to in the commented-out section, shortening the latter and doing some ansi-coloring on the actual “at ” lines...

Even doing as little as this.stack = this.stack already gets me:

node:internal/process/esm_loader:94 internalBinding('errors').triggerUncaughtException(

What is that, and how can I avoid this? What am I doing wrong on my derived error class? Are there better (i.e. output) methods to override instead of the constructor? ( the shortening and coloring does work btw, albeit with that odd error line in-between)

enter image description here

0 Answers
Related