Issues with exception handling in v4.1


#1

Is it just me or there is an issue with exception handling in v4.1?

I keep getting redirected to the / route for every exceptions (route not found, model not found)


#2

Have you read the upgrade guide?

http://adonisjs.com/docs/4.1/upgrade-guide#_exception_handling


#3

Yeah! It’s a new v4.1 project


#4

I’m trying create new adonis project project, create Exception handler with adonis make:ehandler and try to make exception. And it’s work as expected.


#5

Maybe I’ve screwed things up. Will take a look at it again.


#6

@mezie Can u please share the ExceptionHandler file of your project?


#7

Sure @virk

'use strict'

const BaseExceptionHandler = use('BaseExceptionHandler')

class ExceptionHandler extends BaseExceptionHandler {
 async handle (error, { view }) {
   if (error.name === 'ModelNotFoundException') {
     return view.render('404')
   }

   super.handle(...arguments)
 }

 async report (error, { request }) {}
}

module.exports = ExceptionHandler

#8

It should be

await super.handle(...arguments)

or

return super.handle(...arguments)

#9

Thanks for pointing that out. It working now. But I noticed rendering a view inside handle() still doesn’t work. For example, trying to render a 404.edge view.


#10

I have a related question, in the case of a 404 error, how do you get the custom view globals to get loaded from start/hooks.js ?

The edge template complains that it can’t find any of my custom Globals.

return view.render('404')

Thanks!


#11

I need to see some code


#12

Basically the same code above:

const BaseExceptionHandler = use('BaseExceptionHandler')

class ExceptionHandler extends BaseExceptionHandler {
 async handle (error, { view }) {
   if (error.name === 'ModelNotFoundException') {
     return view.render('404')
   }

  return super.handle(...arguments)
 }

 async report (error, { request }) {}
}

module.exports = ExceptionHandler

#13

@mezie

How about

response.send(view.render('404'))

Controllers get extra convince that they can return values, I believe same can be done for Exception handlers too, I will fix it


#14

Yes, this worked.


#15

sorry, I missed that the view object is being passed into the handle() function here:

async handle (error, { view }) {

at this point any View.global() is already defined.