Error 404 Exception Handling - Rendering View Issue - v4.1


#1

I am facing an issue in the HTTP_EXCEPTION block. It is not rendering the login page. However, the same code works for the INVALID_SESSION_EXCEPTION and renders the login page.

This is my Handler.js method.

async handle (error, { request, response, session, view }) 
  {
    response.status(error.status).send(error.message)
    if (error.name === 'HttpException') 
      {
         console.log('Error 404')
         session.withErrors(error.messages).flashAll()
         await session.commit()
         return response.send(view.render('register.login'))
      }
else if (error.name === 'InvalidSessionException') 
     {
         console.log('Error 401')
         session.withErrors(error.messages).flashAll()
         await session.commit()
         return response.send(view.render('register.login'))
     }
return super.handle(...arguments)
  }

After running this code I am getting this view:

Although, I have included the session middleware inside the Global Middleware Array


#2

Since HttpException (404) is raised when no route is found, the global middleware are not executed.

If you want to serve the login page for 404’s, then simply move the Session middleware to the serverMiddleware array.


#3

Thank you :slight_smile:
This helped