Search code examples
node.jsecmascript-6hapi

How to catch response properly from Hapi server.inject


I want to have a catch for errors and success for the server.inject method as currently if it throws an error, it is not caught by the function doing the inject, so how would I do that with a function like this

I am using hapi (17.x)

server.inject({
  method: 'POST',
  url: `/xxx`,
  payload: {
    x: x
  }
});

Solution

  • in hapi 17 you need to use async await. with that, you can use try/catch.

    try {
      await server.inject(options)
    } catch(e) {
      //handle error here
    }