Hate Try...Catch Error Handling in Node.js? Do This Instead

preview_player
Показать описание


You will never have to write another try…catch statement again!

This trick will save you from writing hundreds of lines of unnecessary code.

QUESTIONS

CONNECT

SUBSCRIBE
Рекомендации по теме
Комментарии
Автор

Great Video, no try catch needed anymore, short and simple alternative.

mysteriousmsn
Автор

I was just researching this yesterday. I like the idea of HOF, so I'll give it a test.
For completeness, you might want to have shown how to manage granular error responses.
There are some valid error responses that need to make it back to the user. For my project, I take advantage of throwing as quickly as possible on the first sign of an error, and using classes extended from Error that take an error code and payload. Once this error reaches the app.use for error handling, it sends the response itself. This way you can keep your app.use code very small, but still support complex error handling.

Thanks for the HOF tip!

joelr
Автор

Very useful. I'd add a fifth argument to the high order function: data about the operation, for logging in case of an error. Thank you. Great tutorial. 👏👏👏

leoMC
Автор

Subscribed.. Please continue to do more videos on Node. Thanks

celebrate
Автор

Thank you so much your video helped me understand things that my lecturer didn't explain to me. Thanks again ❤

thevietphung
Автор

Couldn't one just use the 'express-async-errors' library ? Just wondering !!

vnnyboy
Автор

thanks for explaining this, but I don't think you should be saying every error is a 500 error. It could be a bad request. I would suggest this code. res.status(err.status || 500);
res.end();

AlTearjen
Автор

Dude, this thing is awesome, thank u

alexandryurkov
Автор

Great video! One drawback of this approach is that you lose complete control over creating contextual errors at different layers of your app, which improves debugging, because you’re never catching/handling them and just bubbling up anything that might happen.

adventurer
Автор

bro this is so nice👍 this is going to save lives!

ahmadkhudai
Автор

Thanks a lot for this informative and helpful video .😃

mrtourist
Автор

Cool. Hope my bootcamp teacher approves haha

RyanGosselin-xetl
Автор

@Gravity . I really liked your solution, it worked perfect here. But wouldn't it be better to write function as handler than use?

dav.R
Автор

interesting how the fn function return it self as a promise and somehow it is connected to the express route

flowerofash
Автор

i am trying to implement this on a higher level, on the express router level, it works for non-async-routes, and fails for async routes . any insights ??

kabapy
Автор

What if I have a 4 middlewares and the error happened on 3 rd middleware ?

npcx
Автор

Thx for the video but I think in this way you just put all the different possible errors under '500+ general message'.
Imagine we are developing a registration endpoint. maybe the problem could be that the user is trying to use a username that is used yet. In that case, the user will become back 500 'something went wrong' and he'll never understand how to fix the problem.
is it my doubt right or I'm missing something in the workflow?

roccodenicolo
Автор

On your front-end like vue what do you recommend doing for your api? can you recommend any good docs on this or repos I can check out?

sheldonfourie
Автор

What about chaining different handlers, if the catch calls the next middleware and the next middleware is to handle email notification. Even if there is an error, the email will get send?

icheston
Автор

bro sorry but can tell me how I can use it in any code and where in MVC??

aliadel
visit shbcf.ru