32

I am rather new to nodejs so I will explain in a bit more detail what I am trying to do.

I have a webserver. If a request fails I want to log the stack trace of that exception, but deliver a error page and not crash the server.

As an example, the function handling the requests:

var Q = require('q');

var requestHandler = function () {
    // Here I get the data etc. that was requested. As this is not important, just a dummy here
    Q.resolve()
        // Now I answer the request
        .then(function (data) {
            // Dummy Code representing the sending of a response
            console.log('sending response …');
            console.log('oh no! an exception');
            // Now an Exception occurs
            throw new Error('You did something wrong!');
        })
        // If there was any error, show the error page instead
        .fail(function (err) {
            // Dummy Code representing the sending of the error page
            console.log('sending error page');
            // Additionally I want to write the error into a log file
            console.error('You had an error: ', err);
        })
        // If there was an error in the .fail-handler, I deserve the server to crash
        .done();
};

// A request comes in, I want to call my handler
requestHandler();

The console's output:

sending response …
oh no! an exception
sending error page
You had an error:  [Error: You did something wrong!]

I can't see a way to access the stack trace. But when I throw an exception in the .fail-handler (or just omit the complete .fail-handler), I get a stack trace on the console (but I'd have to restart the server).

So I guess my question is:

How do I access the stack trace in a promise fail handler?

EDIT: Any tips on how to improve the explanation are welcome, of course. If I didn't make myself clear, please let me know.

dave
  • 4,024
  • 2
  • 18
  • 34
  • Can you not build the trace in javascript and output it to the client rather than viewing it on the server? – Beetroot-Beetroot Mar 28 '13 at 23:55
  • How do I build the trace in javascript? I'd prefer to write errors to a log file rather than displaying them to the website visitor. – dave Mar 29 '13 at 08:05

1 Answers1

54

Logging the error object won't print error's stack trace. You need to ask for it specifically:

console.error('You had an error: ', err.stack);
Mariusz Nowak
  • 32,050
  • 5
  • 35
  • 37
  • 1
    Thanks Mariusz. why is this not in the q documentation? Is it in the doc, and I simply missed it? The examples they give should show this explicitly. – Cheeso Aug 08 '13 at 20:08
  • 7
    Cheeso it's plain JavaScript, it's not related to Q. – Mariusz Nowak Aug 09 '13 at 15:32
  • Well in my case it's certainly related to Q. The fail method is an interface of Q, right? And in case of failure it gets invoked by Q, right? Would not Q want to document what it is sending and how a fail handler method might use it? It seems like an obvious candidate for an example in the doc. – Cheeso Aug 10 '13 at 20:27
  • 5
    It's more about "How to access stack trace on JavaScript error object". Q.fail doesn't alter in any way error object, it just passes it with first argument exactly as documented. – Mariusz Nowak Aug 12 '13 at 08:16
  • When doing err.stack with sourcemaps, it's showing the `.js` files, not the source file. Is there another method to get the browser to write out the source files? – Kevin Ghadyani Dec 06 '16 at 18:38