AriaMinaei / pretty-error

See node.js errors with less clutter
MIT License
1.52k stars 48 forks source link

Get raw stacktrace #16

Closed grimurd closed 9 years ago

grimurd commented 9 years ago

I'm creating a rest api and when in development mode i also return all errors via json as well as logging it to the console. However the prettified stack trace is unreadable when it is returned as a string. Is there a way for me to get the raw stack trace before it was modified?

Example:

"message": "Cannot read property 'dataValues' of null",
"error": " Error: Cannot read property 'dataValues' of null\  \  - TypeError: Cannot read property 'dataVa\   lues' of null\  \  -  \  \  -  - loginservice.js:188 [0\   mauthenticateUser\  \  -   /home/grimur/Documents/Repos/AuthenticationServer/src/servi\   ces/loginservice.js:1\  \  -   88:23\  \  -  \  \  -  - q.js:794 _fulf\   illed\  \  -   [AuthenticationServer]/[q]/q.js:794:54\  \  -  \  \  -  - q.js:823 self.\   promiseDispatch.done\  \  -   [AuthenticationServer]/[q]/q.js:823:30\  \  -  \  \  -  - q.js:756 Promi\   se.promise.promiseDispatch\  \  -   [AuthenticationServer]/[q]/q.js:756:13\  \  -  \  \  -  - q.js:564 \  \  -   [AuthenticationServer]/[q]/q.js:564:44\  \  -  \  \  -  - q.js:110 flush\   \  \  -   [AuthenticationServer]/[q]/q.js:110:17\  \  -  \  \  - \  \ "
AriaMinaei commented 9 years ago

How do you setup PE? Is it like require("pretty-error").start();? Or is it like pe.render(someError);?

grimurd commented 9 years ago

require("pretty-error").start(); is how i start it. Should i do it the other way instead?

AriaMinaei commented 9 years ago

Well we could (and probably should) provide an api for that. But right now, if you wanna intercept an error before PE modifies it, you could do something like this:

PrettyError = require '../lib/pretty-error'
pe = new PrettyError()

prepare = Error.prepareStackTrace or (exc, frames) ->
    result = exc.toString()
    frames = frames.map (frame) -> "  at #{frame.toString()}"
    result + "\n" + frames.join "\n"

# We replace the original Error.prepareStackTrace with a custom one
Error.prepareStackTrace = (exc, trace) =>
    # Apply the modifications that were originally intended to be
    # applied on the stack trace
    stack = prepare.apply(null, arguments)
    # now we have access to the original stack trace
    console.log stack
    # and we can render the trace too
    pe.render {stack, message: exc.toString().replace /^.*: /, ''}, no

# Just a sample error to test things
a = b

I could rewrite this in JS if you prefer though.

AriaMinaei commented 9 years ago

Closing this issue since it's been inactive for some time.