Master NextJS Error Handling in 10 Minutes

แชร์
ฝัง
  • เผยแพร่เมื่อ 18 พ.ย. 2024

ความคิดเห็น • 62

  • @n8body_dev
    @n8body_dev ปีที่แล้ว +22

    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 ปีที่แล้ว

      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 ปีที่แล้ว +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 ปีที่แล้ว

      @@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 ปีที่แล้ว

      ​@@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 9 หลายเดือนก่อน

      Thx​@@nabiabdelkader9833

  • @bongkotsaelo4239
    @bongkotsaelo4239 ปีที่แล้ว +1

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

  • @akmaldiraa
    @akmaldiraa 3 หลายเดือนก่อน +2

    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."

  • @alaeddinemenai5380
    @alaeddinemenai5380 ปีที่แล้ว +3

    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?

  • @techwithimad4672
    @techwithimad4672 ปีที่แล้ว +25

    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 11 หลายเดือนก่อน +4

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

    • @adel.dev.account
      @adel.dev.account 8 หลายเดือนก่อน +1

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

  • @monirhrabby
    @monirhrabby 5 หลายเดือนก่อน

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

  • @satyabhangt
    @satyabhangt ปีที่แล้ว +1

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

  • @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 11 หลายเดือนก่อน

      Maybe return them in getServerSidePropsb

  • @Blorvax
    @Blorvax หลายเดือนก่อน

    How to access this name property inside of error.tsx template? I get "Error" value every time.

  • @motivation2code
    @motivation2code ปีที่แล้ว +1

    you are amazing Josh. Thank You

  • @kennydebrice
    @kennydebrice 8 หลายเดือนก่อน

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

  • @ju5263
    @ju5263 8 หลายเดือนก่อน

    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.

  • @julianari8768
    @julianari8768 4 หลายเดือนก่อน

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

  • @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 ปีที่แล้ว

      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

  • @LevyneTu
    @LevyneTu 4 หลายเดือนก่อน

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

  • @yabuking84
    @yabuking84 8 หลายเดือนก่อน

    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")

  • @LaluIbnuHidayatullah
    @LaluIbnuHidayatullah ปีที่แล้ว

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

  • @galusmarcin87
    @galusmarcin87 ปีที่แล้ว

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

  • @fiendsgaming7589
    @fiendsgaming7589 ปีที่แล้ว +1

    How do i send error with status code from api?

  • @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

  • @andrepadez
    @andrepadez ปีที่แล้ว

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

  • @aroojshahid5658
    @aroojshahid5658 11 หลายเดือนก่อน

    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

  • @andersbraathen1256
    @andersbraathen1256 ปีที่แล้ว

    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'}

  • @GuilhermeSiebert
    @GuilhermeSiebert 4 หลายเดือนก่อน

    Awesome content!

  • @ronhenly7936
    @ronhenly7936 6 หลายเดือนก่อน

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

  • @aymenbachiri-yh2hd
    @aymenbachiri-yh2hd 4 หลายเดือนก่อน

    THank you so much josh

  • @Rinace
    @Rinace ปีที่แล้ว

    Can I ask what snippets do you use?

  • @yousuf4you
    @yousuf4you ปีที่แล้ว

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

  • @tabmax22
    @tabmax22 ปีที่แล้ว

    does this work via the catch error when fetching data?

  • @Unknown-so7qv
    @Unknown-so7qv 10 หลายเดือนก่อน +1

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

    • @juanfrancomartin5342
      @juanfrancomartin5342 9 หลายเดือนก่อน

      I'm having the same issue =(

    • @adel.dev.account
      @adel.dev.account 8 หลายเดือนก่อน

      @@juanfrancomartin5342 we are all having the same problem xD

  • @hasst9261
    @hasst9261 5 หลายเดือนก่อน

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

  • @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

  • @microspacer
    @microspacer ปีที่แล้ว

    Thank you for your content!

  • @nicolasribeirodossantos2760
    @nicolasribeirodossantos2760 ปีที่แล้ว

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

    • @joshtriedcoding
      @joshtriedcoding  ปีที่แล้ว +2

      Hi for sure, it's a custom snippet and not an extension. I've covered that here: th-cam.com/video/Dert_kpLWE0/w-d-xo.html

    • @nicolasribeirodossantos2760
      @nicolasribeirodossantos2760 ปีที่แล้ว

      @@joshtriedcoding ty

  • @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 10 หลายเดือนก่อน

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

    • @xenomorph80
      @xenomorph80 4 หลายเดือนก่อน

      @@terenceng3704 This not be visible in production build.

  • @naman_dw
    @naman_dw ปีที่แล้ว

    can i use next 13 app directory with trpc?

  • @edmilson8890
    @edmilson8890 ปีที่แล้ว

    Thank you my friend very nice content

  • @psyferinc.3573
    @psyferinc.3573 ปีที่แล้ว

    this was awesome

  • @lostin8159
    @lostin8159 ปีที่แล้ว

    Thank you

  • @abdelkarimelalaoui1538
    @abdelkarimelalaoui1538 ปีที่แล้ว

    thank you so mush bro

  • @JokerNationYt
    @JokerNationYt 4 หลายเดือนก่อน

    please brush thank you !!

  • @focusound15
    @focusound15 ปีที่แล้ว

    Josh keep tried coding