Search code examples
error-handlingrequesthapi.js

Attach request.url to any errors being logged, using hapi


I am trying to add the requested path to all (or most, where it makes sense) server errors logged by NodeJS, the idea being that when viewing the logs in (insert tool of choice here) I'd be able to directly correlate NodeJS errors to the request.url.

I've been reading through the documentation for hapi but I haven't yet stumbled across a way to do this, partially because I am still wrapping my head around the request/response lifecycle and the best place to interject this logic. I tried something in my base controller class by adding request.url as error.url, but did not seem to get the results I was hoping for. I think what I need is an error event handler to attach this extra error property to, similar to restify's server.on('restifyError'), is there anything similar in hapi?


Solution

  • In the hapi docs it shows there are various events you can listen to like you would do like your Restify example. If you see this link it shows the names of events you can listen to.