Guides  ›  Lifecycle  ›  Errors ← PREV NEXT →

Errors

Read state.err and set state.result in an error hook.

module.exports = {
  paths: '/example',
  handler: function (state) {
    throw Error('Something failed')
  },
  error: function (state) {
    console.error(state.err)
    state.res.statusCode = 500
    state.result = { ok: false, message: 'Please try again' }
  }
}

Route error hooks run before the app error hook. Set the result explicitly; returning a value from an error hook does not replace it.

Next: After response →

Tools for business systems.