Korzystanie z obietnic - rejestrowanie śledzenia stosu w programie obsługi błędów
Jestem raczej nowy w nodejs, więc wyjaśnię nieco bardziej szczegółowo, co próbuję zrobić.
Mam serwer internetowy. Jeśli żądanie nie powiedzie się, chcę zarejestrować ślad stosu tego wyjątku, ale dostarczyć stronę błędu, a nie awarię serwera.
Na przykład funkcja obsługująca żądania:
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();
Wyjście konsoli:
sending response …
oh no! an exception
sending error page
You had an error: [Error: You did something wrong!]
Nie widzę sposobu na dostęp do śladu stosu. Ale kiedy rzucę wyjątek w .fail-handler (lub po prostu pomijam kompletny .fail-handler), otrzymuję ślad stosu na konsoli (ale musiałbym zrestartować serwer).
Myślę więc, że moje pytanie brzmi:
Jak uzyskać dostęp do śledzenia stosu w procedurze obsługi niepowodzenia obietnicy?
EDYTOWAĆ: Oczywiście, wszelkie wskazówki, jak poprawić wyjaśnienia, są mile widziane. Jeśli nie wyraziłem się jasno, daj mi znać.