Master NextJS Error Handling in 10 Minutes

Поділитися
Вставка
  • Опубліковано 8 вер 2024
  • In this video, you'll learn my approach to handling errors using "error boundaries". Handling errors this way is super user-friendly and drastically improves the reliability and user-friendliness of your code.
    My GitHub: github.com/jos...
    Wishing you lots of fun building your own cool stuff with this knowledge!

КОМЕНТАРІ • 63

  • @n8body_dev
    @n8body_dev Рік тому +19

    I don't think that's the right way to use error.js, because the documentation says "An error file defines an error UI boundary for a route segment. It is useful for catching unexpected errors that occur in Server Components and Client Components and displaying a fallback UI." where the word "unexpected" is specially bolded. That explains why we actually can't use custom error classes - the name of the caught error will always be "Error" (as one other guy pointed out in the comments) and we won't be able to get any custom props.

    • @nabiabdelkader9833
      @nabiabdelkader9833 10 місяців тому

      You are right I have created custom error classes
      Inside the error file, I was trying to display the appropriate component, but to no avail, because the error was always of the type Error (instance)
      if (error instanceof FetchException) return ()
      else if (error instanceof AuthException) return (
      ...rest of the code
      Are there any suitable solutions in your opinion?

    • @nabiabdelkader9833
      @nabiabdelkader9833 10 місяців тому +2

      Suggested solution (but it doesn't seem ideal to me)
      1-create custom error class
      #FetchException.ts
      export const FetchExceptionMessage = "error when fetch data"
      export default class FetchException extends Error {
      constructor() {
      super(FetchExceptionMessage);
      this.name = 'FetchException'
      }
      }
      #error.ts
      const Error = ({error, reset}: { error: Error; reset: () => void }) => {
      if (error.message === FetchExceptionMessage) return ()
      else if (error.message === AuthExceptionMessage) return ()
      return (unexpected error)
      };
      export default Error

    • @n8body_dev
      @n8body_dev 9 місяців тому

      @@nabiabdelkader9833For now, I've just created CustomErrorPage component with the following props:
      export type CustomErrorPageProps = {
      error: {
      name: string
      status?: number
      reason?: string
      }
      reset?: () => void
      }
      And I use this it in not-found.tsx, error.tsx files as well as in all my pages, when I need to return early due to an error.

    • @n8body_dev
      @n8body_dev 9 місяців тому

      ​@@nabiabdelkader9833By the way, interesting solution, but I think that AuthError is an expected error, so we probably shouldn't handle it in error.tsx. Hmmm.

    • @pouriakalantari6537
      @pouriakalantari6537 6 місяців тому

      Thx​@@nabiabdelkader9833

  • @akmaldiraa
    @akmaldiraa 26 днів тому +1

    but on the production, u just can see
    "An error occurred in the Server Components render. The specific message is omitted in production builds to avoid leaking sensitive details. A digest property is included on this error instance which may provide additional details about the nature of the error."

  • @bongkotsaelo4239
    @bongkotsaelo4239 9 місяців тому +1

    Thanks, I'm struggled at ErrorBoundary at first, no idea that nextjs handle it automatically. 😂

  • @techwithimad4672
    @techwithimad4672 Рік тому +23

    Nice!
    What about production error messages?
    They are often hidden to ensure that no sensitive data is leaked.
    How can we deal with this to know exactly which exceptions were thrown?

    • @thePINGUSTAR
      @thePINGUSTAR 9 місяців тому +4

      yeah I am also interested in this. Did you figure it out?

    • @user-xu3lj1ej8d
      @user-xu3lj1ej8d 6 місяців тому +1

      @@thePINGUSTAR I didn't find solution for this 😢

  • @monirhrabby
    @monirhrabby 3 місяці тому

    I learned something. Thanks Josh - Big fan of you.

  • @alaeddinemenai5380
    @alaeddinemenai5380 11 місяців тому +2

    Thank you Josh- Big fan of you-. I went in depth with documentation and I just confused between expected and unexpted exceptions. Based on the documentation, this file is used for unexpected errors during the runtime. LIke network interruption, ... and we use this file to display human friendly errors to the users with the ability to recover the situation ( recovered network ) instead of refresh the page. While we can track the errors using error reporting service like sentry. Correct me if I'm wrong?

  • @satyabhangt
    @satyabhangt Рік тому +1

    Dude this makes me so happy! Thank you for caring ❤

  • @motivation2code
    @motivation2code Рік тому +1

    you are amazing Josh. Thank You

  • @starmorph
    @starmorph Рік тому

    this is sweet thank you. would love to have those error messages in a toast notification... time to experiment

  • @nirjoyhasanantor3149
    @nirjoyhasanantor3149 Рік тому +4

    Can you tell us how can we access req object/context/headers in app directory that we accessed before using getServerSideProps in Next.Js

    • @lacascadaobregon
      @lacascadaobregon 8 місяців тому

      Maybe return them in getServerSidePropsb

  • @GuilhermeSiebert
    @GuilhermeSiebert 2 місяці тому

    Awesome content!

  • @fiendsgaming7589
    @fiendsgaming7589 Рік тому +1

    How do i send error with status code from api?

  • @aymenbachiri-yh2hd
    @aymenbachiri-yh2hd Місяць тому

    THank you so much josh

  • @julianari8768
    @julianari8768 2 місяці тому

    If you don't want a redirect, you can just pop up something like a toast with a timer

  • @LevyneTu
    @LevyneTu 2 місяці тому

    Great video. Where does the error message toast on the bottom left come from?

  • @yabuking84
    @yabuking84 6 місяців тому

    Why does error.tsx on APP Router doesnt work if "use client" is used on a component? It works if i removed the "use client". (error.tsx is "use client")

  • @user-lb3wu9xi5g
    @user-lb3wu9xi5g 6 місяців тому

    Amazing and helpful video broo; but I'm asking the extension that you use to calculate the size of imports in the vscode

  • @microspacer
    @microspacer Рік тому

    Thank you for your content!

  • @dedrunker
    @dedrunker Рік тому +1

    Hey josh , how could yo remove the red box with the warning of error? everytime that i click there it show a part of my code
    where the exception was throwed

    • @terenceng3704
      @terenceng3704 7 місяців тому

      Im also stuck on this, has anyone figured out how to fix this?

    • @xenomorph80
      @xenomorph80 Місяць тому

      @@terenceng3704 This not be visible in production build.

  • @ju5263
    @ju5263 6 місяців тому

    Hi Josh !
    I'm interested in knowing how you manage to keep explicit server error message in production mode as well, as I could'nt find any workaround.

  • @hasst9261
    @hasst9261 2 місяці тому

    It doesn't work in production mode
    You will not see errors as next js doesn't allow to show them

  • @janikaikkonen6384
    @janikaikkonen6384 Рік тому +1

    I tried creating a custom error class, but I can’t access the custom error.name in error component. error.name is just always ’Error’. I’d like to give user different options depending on the error type, but with only error.message it gets quite messy 😢

    • @nabiabdelkader9833
      @nabiabdelkader9833 10 місяців тому

      Suggested solution (but it doesn't seem ideal to me)
      1-create custom error class
      #FetchException.ts
      export const FetchExceptionMessage = "error when fetch data"
      export default class FetchException extends Error {
      constructor() {
      super(FetchExceptionMessage);
      this.name = 'FetchException'
      }
      }
      #error.ts
      const Error = ({error, reset}: { error: Error; reset: () => void }) => {
      if (error.message === FetchExceptionMessage) return ()
      else if (error.message === AuthExceptionMessage) return ()
      return (unexpected error)
      };
      export default Error

  • @galusmarcin87
    @galusmarcin87 Рік тому

    i dont know why but nextjs returns 200 status code when error occurs, why not 503?

  • @ronhenly7936
    @ronhenly7936 3 місяці тому

    can i use the app router error handling in a component?

  • @LaluIbnuHidayatullah
    @LaluIbnuHidayatullah Рік тому

    can you share mote about handle "Error: Hydration failed because the initial UI does not match what was rendered on the server. " ?

  • @andrepadez
    @andrepadez 11 місяців тому

    Can this also be implemented in a pages router? If not, i would consider migrating to app router just for this

  • @edmilson8890
    @edmilson8890 Рік тому

    Thank you my friend very nice content

  • @yousuf4you
    @yousuf4you Рік тому

    How to remove left-bottom error notation button from nextjs default error?

  • @aroojshahid5658
    @aroojshahid5658 9 місяців тому

    can you please talk about prerender error on nextjs 13? i am having this error on my cart folder which consists of components and 1 page.tsx file

  • @tabmax22
    @tabmax22 Рік тому

    does this work via the catch error when fetching data?

  • @anuj7286
    @anuj7286 Рік тому +1

    Hey josh, what do you prefer new app directory or pages directory for new projects? I was using remix run its is also great

    • @emmanuelezeagwula7436
      @emmanuelezeagwula7436 Рік тому

      what's the difference with remix

    • @anuj7286
      @anuj7286 Рік тому

      @@emmanuelezeagwula7436 Remix run is server side framework it performs better than next js.

    • @joshtriedcoding
      @joshtriedcoding  Рік тому

      definitely prefer the new app directory

  • @andersbraathen1256
    @andersbraathen1256 10 місяців тому

    Thank you, great video! Wouldn`it be easier to just put all the Auth error code on the error.ts page? Use session and show message since every error redirects to error page instead of putting AuthRequiredError on every page? Like: {!session && 'Please log in if you want to continue'}

  • @Rinace
    @Rinace Рік тому

    Can I ask what snippets do you use?

  • @1chbinamin
    @1chbinamin Рік тому

    At 4:32: what keybinding did you execute on the component for a window to popup with import suggestion?

  • @solimanfarag2845
    @solimanfarag2845 Рік тому

    -1
    I used google as an auth provider with nextjs and i deplyed the app but receive There is a problem with the server configuration. Check the server logs for more information. nextjs

  • @Unknown-so7qv
    @Unknown-so7qv 8 місяців тому +1

    Error name is always "Error", so i cant make error.tsx dynamic based on error.name

  • @naman_dw
    @naman_dw Рік тому

    can i use next 13 app directory with trpc?

  • @nicolasribeirodossantos2760

    Josh, would you mind sharing your snippets extension? I find it very convenient for ts components

  • @abdelkarimelalaoui1538
    @abdelkarimelalaoui1538 Рік тому

    thank you so mush bro

  • @lostin8159
    @lostin8159 Рік тому

    Thank you

  • @psyferinc.3573
    @psyferinc.3573 Рік тому

    this was awesome

  • @JokerNationYt
    @JokerNationYt Місяць тому

    please brush thank you !!

  • @kidslovehistory-y8j
    @kidslovehistory-y8j Рік тому

    Josh keep tried coding