Events error handling

/articles/error_event.jpg

AdonisJS added support for handling errors inside events

Since @adonisjs/[email protected] it’s now possible to catch errors that happen inside events

In previous versions all events had to be wrapped into try-catch statements and then handled manually. With new version onError listener was introduced

Only thing that’s required is to add onEvent event handler before any other event handlers

Event.onError((event, error, eventData) => {
  // Here we are free to do anything with error
  
  // Can log it out
  console.log(event)
  console.log(JSON.stringify(eventData))

  // And when using Sentry can send it to there
  Sentry.captureException(error)
})

// Rest of the events
Event.on('user:signup', 'User.onSignup')
The Latest