Error Handling in Express

Поделиться
HTML-код
  • Опубликовано: 27 июл 2024
  • 🔥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
    @stiventson4464 3 года назад +18

    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
    @PadurariuDragosh 3 года назад +2

    Great explanation ! Sort, easy and with big impact!

  • @joeygarcia7826
    @joeygarcia7826 2 года назад

    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.

  • @Felipe-53
    @Felipe-53 3 года назад

    Probably the best vid on express error handling, thanks!

    • @jgoebel
      @jgoebel  3 года назад

      thx, I'm glad it helped!

  • @pratikbhangale2982
    @pratikbhangale2982 3 года назад

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

    • @jgoebel
      @jgoebel  3 года назад

      you're welcome Pratik 👍

  • @JitendraDalai
    @JitendraDalai 3 года назад

    As clean as it could be. Thanks for sharing.

  • @pPablo240
    @pPablo240 2 года назад

    Perfect, simple and efficient. Thanks man

  • @nicogarcia7302
    @nicogarcia7302 2 года назад

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

    • @jgoebel
      @jgoebel  2 года назад +1

      Awesome, thanks Nico 👍

  • @kamil3409
    @kamil3409 3 года назад

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

    • @jgoebel
      @jgoebel  3 года назад

      thanks, I hope it helps 👍

  • @EricOnYouTube
    @EricOnYouTube Год назад

    Well done! Thanks for this.

    • @jgoebel
      @jgoebel  Год назад +1

      Glad you enjoyed it!

  • @izichtl
    @izichtl 2 года назад +1

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

  • @codinginflow
    @codinginflow Год назад

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

    • @jgoebel
      @jgoebel  Год назад +1

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

  • @anuteimatei7119
    @anuteimatei7119 2 года назад

    Good tutorial. Keep up the good work!

  • @rachidb9624
    @rachidb9624 Год назад

    Thank you very much mate, helped me a lot !

    • @jgoebel
      @jgoebel  Год назад +1

      Glad to hear that!

  • @pryansh_
    @pryansh_ Год назад +1

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

  • @shadigaafar3091
    @shadigaafar3091 2 года назад

    Thank you so much, that was very helpful!

    • @jgoebel
      @jgoebel  2 года назад

      Glad it was helpful!

  • @anuragkuradia2676
    @anuragkuradia2676 2 года назад

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

    • @jgoebel
      @jgoebel  2 года назад

      You are most welcome

  • @tech3425
    @tech3425 Год назад

    Got a lot of insight from this video

    • @jgoebel
      @jgoebel  Год назад

      I'm glad it helped

  • @missiontechdk6064
    @missiontechdk6064 2 года назад

    Outstanding explanation sir 🏆

  • @OleksandrDanylchenko2k
    @OleksandrDanylchenko2k 3 года назад

    Thank you a lot!!!

    • @jgoebel
      @jgoebel  3 года назад

      you're welcome!

  • @foreveryoung6108
    @foreveryoung6108 2 года назад

    Thanks for useful info

    • @jgoebel
      @jgoebel  2 года назад

      Glad it was helpful!

  • @santiagoguerra8826
    @santiagoguerra8826 3 года назад

    Thank youuuu!!

    • @jgoebel
      @jgoebel  3 года назад

      you're welcome 👍

  • @jusamuel4246
    @jusamuel4246 3 года назад

    Great way to use class for error handling.

  • @peternicola6429
    @peternicola6429 2 года назад

    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 года назад +1

      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.

  • @matteocinti6482
    @matteocinti6482 2 года назад

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

  • @deretano_fondelli
    @deretano_fondelli 4 года назад +2

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

  • @Euquila
    @Euquila 3 года назад +4

    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 года назад +1

      exactly Euquila. Thx for the nice addition 👍

  • @tinpham6413
    @tinpham6413 2 года назад

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

  • @lukelu
    @lukelu 2 года назад

    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

  • @abdurrakibrony_518
    @abdurrakibrony_518 3 года назад

    nice

  • @gatoloco1873
    @gatoloco1873 2 года назад

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

  • @kruuuux
    @kruuuux 3 года назад

    thanks

    • @jgoebel
      @jgoebel  3 года назад +1

      thx Konrad, you're welcome!

  • @pPablo240
    @pPablo240 2 года назад

    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

  • @privacypolicy9201
    @privacypolicy9201 2 года назад

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

    • @jgoebel
      @jgoebel  2 года назад +1

      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?

  • @turan_sultan
    @turan_sultan 3 года назад

    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 года назад +2

      @@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 года назад +2

      @@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-ez6xb3rf3p 3 года назад

    subscribed

  •  Год назад

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

  • @sumitpandey6958
    @sumitpandey6958 3 года назад

    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
    @mohityadav21 2 года назад

    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 года назад +3

      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

  • @Pareshbpatel
    @Pareshbpatel 11 месяцев назад

    {2023-09-01}

  • @christianaustria741
    @christianaustria741 4 года назад

    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

  • @huh_wtf
    @huh_wtf 3 года назад

    zoom a bit more next time😅

    • @jgoebel
      @jgoebel  3 года назад

      sure, will do next time