0

I need to reformat the console output from HAPI to spit out exceptions with the stack trace in a single line. Everything I come across looks like it's all about sending different info to the client, but I need to reformat the output going to stdout when an exception occurs.

I have tried adding a request-error handler and writing to console.log, and I see my formatted response, but the multi-line exception output still follows immediately afterward.

How do I format exception output?

simon-p-r
  • 3,623
  • 2
  • 20
  • 35
user1588877
  • 775
  • 1
  • 5
  • 9

1 Answers1

0

Try doing console.log(new Error('asdf')) while watching stdout.

The stack trace should be included in that, but here's how to isolate it:

    const test = new Error('test')
    console.log(test.stack)

I'm not running node right now or I would confirm my recommendations.

In addition, I believe console.error() writes to stderr.

Read these:

You could probably do something like this:

const test = new Error('Detonations detected.')
console.log(`\n
    TIME: ${new Date().valueOf()}
    ERROR: ${test.message}
    STACK: ${test.stack}
`)
// Try this exactly as shown first so you can see
// how the template literal is operating.

Is that kind of what you are looking for? Hopefully, that is illustrative enough.

agm1984
  • 15,500
  • 6
  • 89
  • 113
  • Maybe I interpretted this wrong. Are you trying to suppress the Hapi error output and put your own? – agm1984 Oct 05 '17 at 00:27