Next.js Discord

Discord Forum

How do you really make error page in NextJS v14?

Answered
West African Lion posted this in #help-forum
Open in Discord
West African LionOP
My company is using NextJS version 14.1.4.
I wish to create a custom error page, that triggers when accessing, for example, localhost:3000/fsdjfsdfnsdkfnsas, would show a 404.
I am following these documents: https://nextjs.org/docs/app/building-your-application/routing/error-handling
I have created a file inside src/app/error.tsx
It contains an exact copy of the code on the documentation page (See below)
When I visit localhost:3000/fsdjfsdfnsdkfnsas, I would expect to see "Something went wrong!" in the browser.
Instead, I see the default error page, with the message "This page could not be found" as if I had not created a custom error page, or as if the documentation is incorrect.

Please tell me, how do you create a custom error page in NextJS v14?
Thank you.

'use client' // Error components must be Client Components
 
import { useEffect } from 'react'
 
export default function Error({
  error,
  reset,
}: {
  error: Error & { digest?: string }
  reset: () => void
}) {
  useEffect(() => {
    // Log the error to an error reporting service
    console.error(error)
  }, [error])
 
  return (
    <div>
      <h2>Something went wrong!</h2>
      <button
        onClick={
          // Attempt to recover by trying to re-render the segment
          () => reset()
        }
      >
        Try again
      </button>
    </div>
  )
}
Answered by B33fb0n3
you should use not-found.tsx to catch not found errors and you should use error.tsx to catch unexpected error. Everything else should be handled from the page/function/... itself
View full answer

9 Replies

@West African Lion My company is using NextJS version 14.1.4. I wish to create a custom error page, that triggers when accessing, for example, localhost:3000/fsdjfsdfnsdkfnsas, would show a 404. I am following these documents: https://nextjs.org/docs/app/building-your-application/routing/error-handling I have created a file inside src/app/error.tsx It contains an exact copy of the code on the documentation page (See below) When I visit localhost:3000/fsdjfsdfnsdkfnsas, I would expect to see "Something went wrong!" in the browser. Instead, I see the default error page, with the message "This page could not be found" as if I had not created a custom error page, or as if the documentation is incorrect. Please tell me, how do you create a custom error page in NextJS v14? Thank you. 'use client' // Error components must be Client Components import { useEffect } from 'react' export default function Error({ error, reset, }: { error: Error & { digest?: string } reset: () => void }) { useEffect(() => { // Log the error to an error reporting service console.error(error) }, [error]) return ( <div> <h2>Something went wrong!</h2> <button onClick={ // Attempt to recover by trying to re-render the segment () => reset() } > Try again </button> </div> ) }
when visiting a page, that does not exists, you can catch this request with your not-found.js (https://nextjs.org/docs/app/api-reference/file-conventions/not-found). Like that you won't get an error and can show your stuff, that you want to show, when the page not exists
West African LionOP
Ah ok, so the error.tsx file does not catch 404, and you must use not-found.tsx for these status codes?
@West African Lion Ah ok, so the error.tsx file does not catch 404, and you must use not-found.tsx for these status codes?
you should use not-found.tsx to catch not found errors and you should use error.tsx to catch unexpected error. Everything else should be handled from the page/function/... itself
Answer
West African LionOP
Thank you 🙂
@West African Lion Thank you 🙂
Happy to help. Please mark solution
West African LionOP
What if I have a layout in src/app/layout.tsx that I want to use on the 404 page in src/app/not-found.tsx?
@West African Lion What if I have a layout in src/app/layout.tsx that I want to use on the 404 page in src/app/not-found.tsx?
then you can nest it deeper in the tree and the nearest would be used