Error Handling in Express

🔥More exclusive content: productioncoder.com/you-decid...
Twitter: / _jgoebel
Blog: productioncoder.com
Code: github.com/productioncoder/ex...
In this video, we cover how do error handling in express and Node.js. We explain how to return expected errors and how to not leak internal details of your application in case of unexpected errors.
00:00 application walk through
01:30 a better way of hard coding HTTP status codes
02:36 defining API errors with status codes
04:19 creating an API error handling middleware
07:14 making use of next in express
08:14 plugging our our api error handling middleware into our router
08:55 making use of our predefined API errors
10:17 testing our error handling
11:03 preventing leaks from unexpected errors

Пікірлер: 84

  • @stiventson4464
    @stiventson44643 жыл бұрын

    The way you implemented it feels like the way it was intended to be, I found this really helpful

  • @jgoebel

    @jgoebel

    3 жыл бұрын

    Thanks, I'm glad you liked it 👍

  • @PadurariuDragosh
    @PadurariuDragosh3 жыл бұрын

    Great explanation ! Sort, easy and with big impact!

  • @jgoebel

    @jgoebel

    3 жыл бұрын

    thx

  • @Felipe-53
    @Felipe-533 жыл бұрын

    Probably the best vid on express error handling, thanks!

  • @jgoebel

    @jgoebel

    3 жыл бұрын

    thx, I'm glad it helped!

  • @pratikbhangale2982
    @pratikbhangale29823 жыл бұрын

    I was looking for this approach from a long time. Thanks a lot

  • @jgoebel

    @jgoebel

    3 жыл бұрын

    you're welcome Pratik 👍

  • @pPablo240
    @pPablo2402 жыл бұрын

    Perfect, simple and efficient. Thanks man

  • @jgoebel

    @jgoebel

    2 жыл бұрын

    thx Pablo

  • @joeygarcia7826
    @joeygarcia78262 жыл бұрын

    Loved the video! I would like to see one like this doing Content Negotiation and using the route information to know whether or not to throw a 406 or 415 if you have a variety of routes (GET & POST) and you have to know based on the route whether this route would ever be returning a payload, if not, then no need to throw a 406 error. Similarly for the 415.

  • @nicogarcia7302
    @nicogarcia73022 жыл бұрын

    Awesome explanation, it was really helpful👏🏽 subscribed!!

  • @jgoebel

    @jgoebel

    2 жыл бұрын

    Awesome, thanks Nico 👍

  • @JitendraDalai
    @JitendraDalai3 жыл бұрын

    As clean as it could be. Thanks for sharing.

  • @jgoebel

    @jgoebel

    3 жыл бұрын

    Thanks 👍

  • @izichtl
    @izichtl2 жыл бұрын

    This video blow my mind to a new perspective, thank you

  • @jgoebel

    @jgoebel

    2 жыл бұрын

    thx Ivan

  • @kamil3409
    @kamil34093 жыл бұрын

    Really clean explanation, I'm going to use this in University Project. Have a good day.

  • @jgoebel

    @jgoebel

    3 жыл бұрын

    thanks, I hope it helps 👍

  • @pryansh_
    @pryansh_ Жыл бұрын

    i luv u 💖💖 , u might not know how much you helped me.

  • @EricOnYouTube
    @EricOnYouTube Жыл бұрын

    Well done! Thanks for this.

  • @jgoebel

    @jgoebel

    Жыл бұрын

    Glad you enjoyed it!

  • @shadigaafar3091
    @shadigaafar30912 жыл бұрын

    Thank you so much, that was very helpful!

  • @jgoebel

    @jgoebel

    2 жыл бұрын

    Glad it was helpful!

  • @rachidb9624
    @rachidb9624 Жыл бұрын

    Thank you very much mate, helped me a lot !

  • @jgoebel

    @jgoebel

    Жыл бұрын

    Glad to hear that!

  • @anuteimatei7119
    @anuteimatei71192 жыл бұрын

    Good tutorial. Keep up the good work!

  • @jgoebel

    @jgoebel

    2 жыл бұрын

    thx Anutei

  • @anuragkuradia2676
    @anuragkuradia26762 жыл бұрын

    thank you, I was looking for this solution for days.!!!!

  • @jgoebel

    @jgoebel

    2 жыл бұрын

    You are most welcome

  • @matteocinti6482
    @matteocinti64822 жыл бұрын

    Would you consider throwing an error instead of returning it in the class constructor?

  • @missiontechdk6064
    @missiontechdk60642 жыл бұрын

    Outstanding explanation sir 🏆

  • @jgoebel

    @jgoebel

    2 жыл бұрын

    thx 👍

  • @tech3425
    @tech3425 Жыл бұрын

    Got a lot of insight from this video

  • @jgoebel

    @jgoebel

    Жыл бұрын

    I'm glad it helped

  • @foreveryoung6108
    @foreveryoung61082 жыл бұрын

    Thanks for useful info

  • @jgoebel

    @jgoebel

    2 жыл бұрын

    Glad it was helpful!

  • @peternicola6429
    @peternicola64292 жыл бұрын

    what a great channel man 👍💪, I saw some of your videos and you are a really good teacher man, so happy I stumbled upon your channel .. one question I have is what do you mean by data leakage .. what kind of data do you mean, like where the code errored out maybe or what?

  • @jgoebel

    @jgoebel

    2 жыл бұрын

    e.g. let's assume you have some manhandled error, you might by accident return the call stack to the client. This might be problematic in terms of security.

  • @OleksandrDanylchenko2k
    @OleksandrDanylchenko2k3 жыл бұрын

    Thank you a lot!!!

  • @jgoebel

    @jgoebel

    3 жыл бұрын

    you're welcome!

  • @jusamuel4246
    @jusamuel42463 жыл бұрын

    Great way to use class for error handling.

  • @jgoebel

    @jgoebel

    3 жыл бұрын

    thanks

  • @santiagoguerra8826
    @santiagoguerra88263 жыл бұрын

    Thank youuuu!!

  • @jgoebel

    @jgoebel

    3 жыл бұрын

    you're welcome 👍

  • @lukelu
    @lukelu2 жыл бұрын

    Great tutorial, very logical. Just wondering how to send error message to the front end, do we need another res.send to send error message ?

  • @jgoebel

    @jgoebel

    2 жыл бұрын

    Hi Luke, we return the error message in the error handling middleware that we plug in at the very end

  • @deretano_fondelli
    @deretano_fondelli4 жыл бұрын

    I personally would leverage try catch block and extend ApiError to the native Error object

  • @Euquila
    @Euquila3 жыл бұрын

    In case someone is wondering, how does express know it's an error handler middleware ? The number of arguments is 4!!! (err, req, res, next)

  • @jgoebel

    @jgoebel

    3 жыл бұрын

    exactly Euquila. Thx for the nice addition 👍

  • @gatoloco1873
    @gatoloco18732 жыл бұрын

    What is the purpose of "static internal" method if you nerver used it?

  • @pPablo240
    @pPablo2402 жыл бұрын

    The generic error gona work for every kind of error? Like bugs, connection problems with db, etc ?

  • @jgoebel

    @jgoebel

    2 жыл бұрын

    yes, any error that is not anticipated, i.e. that is not an instance of ApiError will be caught

  • @abdurrakibrony_518
    @abdurrakibrony_5183 жыл бұрын

    nice

  • @privacypolicy9201
    @privacypolicy92012 жыл бұрын

    why you return; empty just to stop the program instead of returning next(new ApiError(404,blabla)), is there any difference?

  • @jgoebel

    @jgoebel

    2 жыл бұрын

    because then it would look like you have to return the value of the next(new ApiError(...)) which you don't because the call to next just passes on the control to the next available middleware.

  • @privacypolicy9201

    @privacypolicy9201

    2 жыл бұрын

    @@jgoebel so the middleware will stop passing to next() till that point?

  • @kruuuux
    @kruuuux3 жыл бұрын

    thanks

  • @jgoebel

    @jgoebel

    3 жыл бұрын

    thx Konrad, you're welcome!

  • @tinpham6413
    @tinpham64132 жыл бұрын

    5:38 Thanks for point it out, I alway wonder why I can't console in nodejs

  • @jgoebel

    @jgoebel

    2 жыл бұрын

    👍

  • @codinginflow
    @codinginflow Жыл бұрын

    I like your stuff. I saw that you're German as well so Hallo von mir 👋

  • @jgoebel

    @jgoebel

    Жыл бұрын

    Thx, schöne Grüße nach Erfurt 😁

  • @turan_sultan
    @turan_sultan3 жыл бұрын

    Is it acceptable to use ApiError inside services? Or is it intended to be used only in controllers?

  • @jgoebel

    @jgoebel

    3 жыл бұрын

    I would primarily use it in controllers because the service layer should only handle business logic

  • @turan_sultan

    @turan_sultan

    3 жыл бұрын

    @@jgoebel I'm a bit struggling with handling business logic errors. Imagine there is a login service, which can throw errors because 1) Username can't be found, 2) Password is incorrect. The appropriate status codes would be 404 and 400, respectively. However, since the business logic shouldn't be aware of http stuff, how the controller knows which status to send to the client?

  • @jgoebel

    @jgoebel

    3 жыл бұрын

    @@turan_sultan you could throw custom errors and then depending on what error you get, you return the proper status code. The error handling shown in this video is the simplest form of error handling available. Technically speaking you could make it more sophisticated, make custom errors and then check in your error handling middleware which error it is. Now if somewhere in your service an error is thrown, you would eventually end up in the error handling middleware which would automatically return the correct status code. In this case the controller would not be involved. This approach is probably the most flexible one

  • @turan_sultan

    @turan_sultan

    3 жыл бұрын

    @@jgoebel Thank you, the second method is what I will probably implement. I don't know if it is just me, but it is rare to find a proper way of doing a thing in express, especially when it comes to architecture. Many times I find myself reading articles/stackoverflow on Nest.js or Java Spring.

  • @user-ez6xb3rf3p
    @user-ez6xb3rf3p3 жыл бұрын

    subscribed

  • @jgoebel

    @jgoebel

    3 жыл бұрын

    thx

  • Жыл бұрын

    It's clear and simple but, when you work with async functions it all gets messy

  • @sumitpandey6958
    @sumitpandey69583 жыл бұрын

    bro i have .unshift error in my code plse help me out of it in express

  • @jgoebel

    @jgoebel

    3 жыл бұрын

    Hi Sumit, I don't think this is related to the error handling code in the video because there we do not use unshift

  • @sumitpandey6958

    @sumitpandey6958

    3 жыл бұрын

    Sorry but i was doing a blog website project by git hub of clever programmer so i got an error ao i thought here i have chance to get help😅 so that's the reason i wrote my issue.

  • @sumitpandey6958

    @sumitpandey6958

    3 жыл бұрын

    Plse help me on that problem

  • @jgoebel

    @jgoebel

    3 жыл бұрын

    @@sumitpandey6958 Looks like your array at which you call unshift is null or the indexes which you are passing are already out of bounds

  • @mohityadav21
    @mohityadav212 жыл бұрын

    In api-error--handler.js, you are checking if err is instance of ApiError. What if we have a large number of Error classes, do we check if error is instance of a specific error class? And why do we check this? I don't see any point of this, we are just setting the status code and sending the error message as json. Shouldn't this be common for all types of errors? Or we can just inherit all errors from a common class and check only if error is instance of that base class?

  • @jgoebel

    @jgoebel

    2 жыл бұрын

    we do this to not by accident expose any internals to the client (in case there is an error we do not anticipate). Yes, you could also create your custom error types and inherit from Error directly

  • @mohityadav21

    @mohityadav21

    2 жыл бұрын

    @@jgoebel got it. Thanks

  • @christianaustria741
    @christianaustria7414 жыл бұрын

    the github repo doesn't exist

  • @jgoebel

    @jgoebel

    4 жыл бұрын

    thanks for the hint. Sry. I forgot to make it public. Now it should work: github.com/productioncoder/express-error-handling

  • @Pareshbpatel
    @Pareshbpatel10 ай бұрын

    {2023-09-01}

  • @huh_wtf
    @huh_wtf3 жыл бұрын

    zoom a bit more next time😅

  • @jgoebel

    @jgoebel

    3 жыл бұрын

    sure, will do next time