Skip to content

Instantly share code, notes, and snippets.

@kpdecker
Created November 5, 2013 16:26
Show Gist options
  • Save kpdecker/7321683 to your computer and use it in GitHub Desktop.
Save kpdecker/7321683 to your computer and use it in GitHub Desktop.
Hapi error handling cases
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();
@jeromecovington
Copy link

jeromecovington commented Nov 30, 2016

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