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();
@geek
Copy link

geek commented Nov 5, 2013

var Hapi = require('hapi');

// Create a server with a host, port, and options
var server = new Hapi.Server('localhost', 8001, { debug: { 'request': ['error', 'uncaught'] } });

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.start();


/*
GET /failure


Debug: hapi, handler, error { msec: 1 }
Debug: hapi, response, error 
Debug: hapi, internal, error { isBoom: true,
  response: 
   { code: 500,
     payload: 
      { code: 500,
        error: 'Internal Server Error',
        message: 'Returned' },
     headers: {} },
  data: [Error: Returned],
  message: 'Returned',
  created: undefined }



GET /throws

Debug: hapi, uncaught, handler, error Error: Thrown
    at server.route.config.handler (/Users/wlyonpr/Github/test/kevin.js:21:23)
    at /Users/wlyonpr/Github/test/node_modules/hapi/lib/request.js:588:43
    at /Users/wlyonpr/Github/test/node_modules/hapi/lib/ext.js:144:9
    at /Users/wlyonpr/Github/test/node_modules/hapi/lib/request.js:585:17
    at Function.internals.Ext.runProtected (/Users/wlyonpr/Github/test/node_modules/hapi/lib/ext.js:133:5)
    at /Users/wlyonpr/Github/test/node_modules/hapi/lib/request.js:551:17
    at /Users/wlyonpr/Github/test/node_modules/hapi/node_modules/hoek/lib/index.js:594:22
    at process._tickDomainCallback (node.js:459:13)
Debug: hapi, response, error 
Debug: hapi, internal, error { isBoom: true,
  response: 
   { code: 500,
     payload: 
      { code: 500,
        error: 'Internal Server Error',
        message: 'An internal server error occurred' },
     headers: {} },
  message: 'Uncaught error',
  trace: 
   [ 'Error: Thrown',
     '    at server.route.config.handler (/Users/wlyonpr/Github/test/kevin.js:21:23)',
     '    at /Users/wlyonpr/Github/test/node_modules/hapi/lib/request.js:588:43',
     '    at /Users/wlyonpr/Github/test/node_modules/hapi/lib/ext.js:144:9',
     '    at /Users/wlyonpr/Github/test/node_modules/hapi/lib/request.js:585:17',
     '    at Function.internals.Ext.runProtected (/Users/wlyonpr/Github/test/node_modules/hapi/lib/ext.js:133:5)',
     '    at /Users/wlyonpr/Github/test/node_modules/hapi/lib/request.js:551:17',
     '    at /Users/wlyonpr/Github/test/node_modules/hapi/node_modules/hoek/lib/index.js:594:22',
     '    at process._tickDomainCallback (node.js:459:13)' ],
  outterTrace: 
   [ 'null (/Users/wlyonpr/Github/test/node_modules/hapi/lib/ext.js:138:32)',
     'g (events.js:175:14)',
     'EventEmitter.emit (events.js:95:17)',
     'process._fatalException (node.js:249:27)' ],
  data: 
   { [Error: Thrown]
     domain: { domain: null, _events: {}, _maxListeners: 10, members: [] },
     domainThrown: true } }
*/

@rainabba
Copy link

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?

@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