Next.js Discord

Discord Forum

How to differentiate different server side errors to show an error UI to the user

Unanswered
Southeastern blueberry bee posted this in #help-forum
Open in Discord
Southeastern blueberry beeOP
I have some routes that need to call the backend for getting some data, i forward the token of the user, but if the token is expired, it will cause a 403 error, i want to be able to show the user different error messages depending on the status code of such request (if there is), how could i do it?

17 Replies

Southeastern blueberry beeOP
And also, i have a global-error.tsx file that seems to never be caught, i still get the default next.js error, i'm using the app router obviously
Root layout/template error do not get caught in error.js
@Clown Root layout/template error do not get caught in error.js
Southeastern blueberry beeOP
But global-error.tsx should catch all
at least that's what is written in the documentation
Also error.js recieves the the error so you can do whatever you want with it
@Southeastern blueberry bee But global-error.tsx should catch all
Where is your global-error placed?
Southeastern blueberry beeOP
root folder (src/app/global-error.tsx)
Hmm...
Does it have the html tag?
Southeastern blueberry beeOP
ye
export default function GlobalError({error, reset}: ErrorProps) {
    return (
        <html>
        <body>
        <Navbar/>
        <Page crop style={{gap: "1rem"}}>
            <Header>
                {`C'è stato un errore durante il caricamento della pagina`}
            </Header>
            <div style={{margin: "2rem"}}>
                <p>{error.message}</p>
                {//@ts-ignore
                    error.digest && <p>Code: ({error.digest})</p>
                }
            </div>
            <Link href={"/"}>Torna alla home</Link>
        </Page>
        <Footer/>
        </body>
        </html>
    )
}
Have you tried using error.js in combination with this?
Southeastern blueberry beeOP
not yet, i was trying to get some info on how to structure the error component properly first
Try using error.js then in conjunction
IMO it might be because its just a part of your application that is error-ring out and not the entire layout which is why this is happening
So nextjs just uses its own error