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...
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!
thank u bro!
Pls Alex I want talk to u this my email kinglamido@gmail.com thanks
Thanks :D..... :D .....
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?
It will be one huge init commit, but I sure will.
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
thanks a lot. Merry chritmas and happy new year
Very concise info. 1 minute = 10 minutes of normal presentation.
Flexing: I listen to this at 2x. (Yea, right)
true that
Awesome content, thank you for sharing your knowledges! Cheer up and respect from France
That was great, thank you for this video! I like it!
Thanks for the content man
Thanks mate
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/
hi alex. Is there a repository to view the code? thanks.
Just created one by popular demand, here you go github.com/alex996/node-auth
A little unreadable there cleaner ways of doing this...Good video tho