Created
November 5, 2013 16:26
-
-
Save kpdecker/7321683 to your computer and use it in GitHub Desktop.
Hapi error handling cases
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Hapi = require('hapi'); | |
// Create a server with a host, port, and options | |
var server = new Hapi.Server('localhost', 8001); | |
server.route([ | |
{ | |
path: '/failure', | |
method: 'GET', | |
config: { | |
handler: function(req) { | |
req.reply(new Error('Returned')); | |
} | |
} | |
}, | |
{ | |
path: '/throws', | |
method: 'GET', | |
config: { | |
handler: function(req) { | |
throw new Error('Thrown'); | |
} | |
} | |
} | |
]); | |
server.on('internalError', function (request, err) { | |
console.log(err.data.stack); | |
//console.log('Error response (500) sent for request: ' + request.id + ' because: ' + (err.trace || err.stack || err)); | |
}); | |
server.start(); |
geek
commented
Nov 5, 2013
In one case you're returning a semi-friendly 500 to the browser, but get little detail. In the other, you get a great stack, but nothing returned to the user. No way to strike a balance?
Is there any way to attach the HTTP request path that resulted in an error being thrown, to the Error
object itself, either as part of the message or as an additional property? I suppose now that the event is renamed it could just be:
server.on('request-error', function (request, err) {
console.error('Error response (500) sent for request: ' +
request.id + ' at ' + request.url.path +' because: ' +
(err.trace || err.stack || err));
});
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment