Authentication in Node.js - #5 Error Handling

Поділитися
Вставка
  • Опубліковано 15 вер 2024
  • Following up on our previous video about registration, we are going to implement proper error handling in this episode. Currently, any unhandled promise rejection such as the case with validation, causes the response to time out. This is because in Express, any exception thrown in an asynchronous route handler needs to be passed to next() explicitly.
    We can ameliorate this with repeated try-catch blocks around asynchronous code. However, this approach doesn't scale and leads to duplication. Instead, we are going to wrap each async handler function with a wrapper function. This will tack on catch(next) to each promise chain, so in case of an exception, it gets properly forwarded to the centralized error handler. You can read up on this technique in more detail at StrongLoop strongloop.com...
    As far as capturing 404 and 500 errors in Express, the docs, specifically the FAQ section, are very clear on this expressjs.com/... You can mount two middleware functions at the bottom of the stack, one to handle any unmatched routes and the other to be a catch-all for any unexpected errors (ex: failed DB queries, runtime exceptions, etc.). Just be sure to attach them to the express() and not Router() instance; otherwise, they won't get matched.
    To differentiate between 4xx and 5xx errors, we will also define custom Error classes for each HTTP status code, such as 400 and 404. This will allow us to send back proper status codes and only log the stack trace of internal server errors.
    More on error handling in Express expressjs.com/...
    GitHub repo github.com/ale...

КОМЕНТАРІ • 19

  • @CodeRealm
    @CodeRealm  4 роки тому +12

    Hey guys, this will be the last housekeeping i.e. boring video in this series. The next one will be much more animated and interesting. Really excited to share that very soon. Thanks for your viewership in 2019 and I wish you a happy new year!

    • @maykbrito
      @maykbrito 4 роки тому

      thank u bro!

    • @kinglamido
      @kinglamido 4 роки тому

      Pls Alex I want talk to u this my email kinglamido@gmail.com thanks

    • @julianmay9566
      @julianmay9566 4 роки тому

      Thanks :D..... :D .....

    • @otgovor
      @otgovor 4 роки тому +1

      I really enjoy watching your videos. Quick but descriptive.
      And one suggestion from me: can you publish the code somewhere and put a link in the description?

    • @CodeRealm
      @CodeRealm  4 роки тому +3

      It will be one huge init commit, but I sure will.

  • @MistaL33
    @MistaL33 4 роки тому +4

    Alex, thanks for sharing your outstanding knowledge with us! You are precise like a laser! I love and enjoy all of your content, man! Hope you´ll get a million subs! Much love and respect from germany! cheers

  • @jeromealtariba7339
    @jeromealtariba7339 4 роки тому +4

    thanks a lot. Merry chritmas and happy new year

  • @smportis
    @smportis 4 роки тому

    Very concise info. 1 minute = 10 minutes of normal presentation.
    Flexing: I listen to this at 2x. (Yea, right)

  • @mariobross4658
    @mariobross4658 4 роки тому

    Awesome content, thank you for sharing your knowledges! Cheer up and respect from France

  • @aziaev
    @aziaev 3 роки тому

    That was great, thank you for this video! I like it!

  • @AbdelhameedG
    @AbdelhameedG 4 роки тому

    Thanks for the content man

  • @AbdelhameedG
    @AbdelhameedG 4 роки тому

    Thanks mate

  • @armaandhanji2112
    @armaandhanji2112 4 роки тому

    Hey Alex, not sure if this is necessarily something you need to add in to the series, but it would be neat if you had a video about adding in integration with Sentry. This way, when we get errors being thrown in production, we have a nice way to see them logged out. Love your series!
    sentry.io/

  • @marcodalborgo
    @marcodalborgo 4 роки тому

    hi alex. Is there a repository to view the code? thanks.

    • @CodeRealm
      @CodeRealm  4 роки тому +1

      Just created one by popular demand, here you go github.com/alex996/node-auth

  • @code2287
    @code2287 2 роки тому

    A little unreadable there cleaner ways of doing this...Good video tho