How can I display a custom internal server error page?
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):
- Create separate entry points for the server and client as described here
- In your
server.js
file, create aninit()
action. - Inside of the
init()
action, listen for theuncaughException
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 toOn 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!
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.