How to handle internal server 500 errors - display custom internal server error page

How can I display a custom internal server error page?

2 Likes

@mmczaplinski Can you advise on this?

1 Like

Hi @leogardner12 :wave:

The short answer is that right now there is no built-in mechanism to do this in Frontity. Server Extensibility should make this easier.

You might be able to hack something together by doing the following (not tested, just a rough idea):

  1. Create separate entry points for the server and client as described here
  2. In your server.js file, create an init() action.
  3. Inside of the init() action, listen for the uncaughException event like:
    init: ({ state }) => {
      process.on('uncaughtException', (err, origin) => {
         // set some data in the state or whatever you want to do
      });
    }
    

However, read the docs for uncaughtException to make sure you’re not creating more problems than you’re solving, in particular this section:

Warning: Using 'uncaughtException' correctly

'uncaughtException' is a crude mechanism for exception handling intended to be used only as a last resort. The event should not be used as an equivalent to On Error Resume Next. Unhandled exceptions inherently mean that an application is in an undefined state. Attempting to resume application code without properly recovering from the exception can cause additional unforeseen and unpredictable issues.

Hope this helps! :slight_smile:

@leogardner12 Same problem bro thanks for asking this i want to know about this server problem. :heart_eyes:

Still yet to find a solution. When I do I’ll post it on here.

Hi,

Thanks for your suggestion. Unfortunately, I haven’t been able to get this working.

I found the following piece of code on stack overflow.

  app.use(async (ctx, next) => {
    try {
      await next()

      if (ctx.status === 404) ctx.throw(404)
    } catch (err) {
      console.error(err)
      ctx.status = err.status || 500
      ctx.body = errorPage.render({ // Use your render method
        error: err,
      })
    }
  })

Would this be possible to implement in beforeSSR?

I’m sorry @leogardner12 but there is no way to accomplish that currently in Frontity until Server Extensibility is merged into core.

You do not have access to the next() callback in any of the actions that Frontity provides.

1 Like