Error Handling in Express

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Neat tutorial, good job. No unncessary talking and straight to the point

Only thing that somewhat "bothered" me is that you say next(....) and then return in a new line instead of doing it in one line like return next(....). Another example is at 6:21 in the vid

πŸ‘οΈŽ︎ 6 πŸ‘€οΈŽ︎ u/leiinth πŸ“…οΈŽ︎ Jun 11 2020 πŸ—«︎ replies

Thank you, this was super useful. Now my code looks better and cleaner.

πŸ‘οΈŽ︎ 4 πŸ‘€οΈŽ︎ u/dada513 πŸ“…οΈŽ︎ Jun 11 2020 πŸ—«︎ replies

Great video, subbed to your channel :)

πŸ‘οΈŽ︎ 3 πŸ‘€οΈŽ︎ u/Neekoy πŸ“…οΈŽ︎ Jun 11 2020 πŸ—«︎ replies

This is a good channel, I subscribed.

Request: using middleware with node/express

πŸ‘οΈŽ︎ 3 πŸ‘€οΈŽ︎ u/Wenzel-Dashington πŸ“…οΈŽ︎ Jun 11 2020 πŸ—«︎ replies

Very informative and really simple to understand. Thank you.

πŸ‘οΈŽ︎ 3 πŸ‘€οΈŽ︎ u/TheWiseScarecrow πŸ“…οΈŽ︎ Jun 12 2020 πŸ—«︎ replies

Pretty straightforward, that's what I look for, thanks.

πŸ‘οΈŽ︎ 2 πŸ‘€οΈŽ︎ u/IAmRC1 πŸ“…οΈŽ︎ Jun 12 2020 πŸ—«︎ replies

Fantastic!

The way you speak reminds me of Elon Musk.

πŸ‘οΈŽ︎ 2 πŸ‘€οΈŽ︎ u/[deleted] πŸ“…οΈŽ︎ Jun 12 2020 πŸ—«︎ replies

nice video

πŸ‘οΈŽ︎ 2 πŸ‘€οΈŽ︎ u/jdrichardstech πŸ“…οΈŽ︎ Jun 12 2020 πŸ—«︎ replies

Neat! Thanks

πŸ‘οΈŽ︎ 2 πŸ‘€οΈŽ︎ u/CommercialBuilder99 πŸ“…οΈŽ︎ Jun 13 2020 πŸ—«︎ replies
Captions
now what is going on everybody today we're going to talk about error handling on an API level with Express so we're not talking about like try catch in general like with JavaScript but how do you return proper errors in your expressjs server and how do you not leak any sensitive information and in order to demonstrate this I got a really simple example app over here so I already pre wrote this because it would have been boring to just write all of this out so basically it's just a simple server and it's listening on port 8080 and it's going it has like one router and this router has like one route which is a post request to slash tweet and our tweet control like we have a controller and this controller is handling like the street this tweet and we're just going to pretend that with this endpoint that we're going we can create like a tweet so we should look at the implementation it's like pretty simple so we have this method here it's going to expect like a message field inside of the body and if this is not there then it's going to return a bad request error and otherwise it's just going to return 201 status which is kind of fake right because we didn't do anything but for the sake of this tutorial I think it's fine and what you often see people do is what I have here so that they are trying to handle the arrow directly like in the controller and directly return the correct status code and while this might work for like such a simple tutorial like the one we have here it can get pretty messy later on and that's why there's actually a better approach now I have already shown you like this web page and I would highly encourage you to kind of read through it it's like the Express guide on how to do error handling but I'm just going to show you how I typically do it and maybe you can get some inspiration from that let me know what you think about it in the comments so what I typically do is I'll make a directory and I'll just call this arrow and we have two calls right now so our first goal is we do not want to handle the error here directly and our second goal is we want to make sure that we do not leak any sensitive information like if someone tries to attack our server we don't want to leak any information and what I typically do is I create a class so I'll just call this API era and this API error has like two fields a code which stands for the status code and a message which is like the actual error message okay so I'm just going to set this and construct it right here and then I am typically adding methods for the different statuses or the expected errors that I have so for example I could say bad requests and you can pass in an arbitrary message and I'm just going to return a new API error with 400 status code and the message so what this is going to do is it's going to make to create a static method called bad request and since we have this static keyword over here what we can do is we can invoke this method by saying a PIR out of that request so we do not need to use new API arrow and so on but we can just use like this method over here and what you probably also want to have is you probably want to have an internal server error because that's pretty much like the default so if you don't know what to return then you probably want to return an internal server error to not leak like any information cool so that's our API error class and I'm just going to export this class yeah and that's it and instead of you know returning like this thing directly what I'm going to do is I'm going to create an error handling middleware and this middle way is going to check if the error we get is of type API error and if it is then we know that this is some kind of expected error and that we can safely return the error message to the user without accidentally leaking some information so I'm just going to add other handler in here so I'm going to call this API error handler and I'm just this is just a function and the signature looks basically exactly like a standard request Handler signature the only difference here is that the first parameter is an error and if you have four parameters in your middleware then basically Express knows that it is an error handling middle aware and what we should do is the first thing you want to do is you want to lock this error but in prod don't use console don't use console error lock or console error because it's not a sink sink not a sink just wanted to mention this over here so if whatever if this error like is super big and you try to log it with console lock or console error then it's going to be super slow so I would recommend like a logging library like Winston boon-'boon young I think yeah cool and what we are just going to do is we're going to say if our error is an expected error so if it's something that we want to return to the user then we are going to return the status code like of the error and we're going to return a message and afterwards we're going to return now this return here is important because if we don't know what to do then we want to return a generic internal server error and then we'll say something went wrong and I just said it's important because if we are in here and we return like something then we need to get out of this mess method because otherwise we're trying to return something twice so that might be a problem and we still need to export this okay so that's our error handler it's actually pretty simple right it just logs it it checks is this like some kind of expected error if yes I return the error and otherwise I just return a generic internal server error and now we actually get to the part where it gets interesting because you need to understand what this next parameter does but before we do that I just saw that I forgot to import the API error so I'm just going to require it yeah okay that should be it and now this next what this next function does is if you call next like this what it's going to say is hey I'm done here with this Handler and please pass over the control to the next handler in the chain so if we're talking about Express right and about middlewares you kind of plug in your first milli where and then you plug in your next Milla where so to say and if you call next in any of these then it's just going to pass on the control to the next Handler and what I typically do is I just make my API error handler the last handler in the chain so I just import this over here Pera api our handler okay so that looks pretty good and then I'm just going to say after use a Piro handler okay did we export this yes okay cool so what we can do now here instead of returning like or hard-coding like this 400 code what we can just say is we can just return an API error so we can say a concise error equal is require and then one up and then error and an API error and we can just say API a wrote dot bad request message field is required and must be non blank and what this is going to do is it's going to pass on the control to the next handler and the important thing here to understand is that if you call next like this then Express is assuming that no error happened however if you put anything inside of this inside of the method like as a parameter then it's going to assume that it is an error and that is important to understand so what this thing is doing it's checking okay to have a correct request or to have the correct data if not I'm just going to say okay I'm done here here's like the error that I created and then I'm going to return now the return here is important because otherwise we execute this as well at the new server crash and since our error handler is basically the last in the line what's going to happen is that we will end up at this error handler here and that we're going to properly return the error so let's just try this out let's run this and see whether we did everything correctly yeah it seems it's fine but let's see okay so what I'm going to do is I have a postman request here and I'm going to make a post request to slash tweet and I'm going to omit like this message thing and now technically we should get an error yes so pretty nice you see it has like a bad request and the message field is required and must be non blank but this is good like an error message like this can be safely passed to the user and if I put like it back in again yeah then you see we get a 201 back so we're basically inside here so this is working pretty well now one more thing if whatever reason like an error occurs down here and you you pass something that is not an API error then it's just going to return the generic error message and right here and this right here like this might not be it might not be whoops why is this oh because I need to put oh sorry I need to put this back in yeah and then you get something went wrong so let's just assume down here you have additional logic right with try-catch and you know or maybe you have some error that you're just not handling because you have a bug then if you pass any non any error that is not of type API arrow to the middle then it's going to just return like the generic error message and that is exactly what we want so like so we make sure that we do not leak any data to the user yeah so that's it pretty much for error handling as you can see it's pretty simple please let me know what you think about this in the comments please leave a like if you found that useful and you can also reach out to me on twitter my twitter handle is at production coder i'll also put it in the description down below and i have also created an email list so if you guys want to have a say in what we built next on this channel you can sign up there and then we can like you can have a say in what we do next on this channel because I will send an email around from time to time so again thank you very much for watching and I'll see you in the next video
Info
Channel: productioncoder
Views: 17,532
Rating: undefined out of 5
Keywords: node, node.js, node-error, node-error-middleware, javascript, javascript-error-handling, js, express, javascript error handling, express error handling, node error handling, error handling in express js, javascript error handling best practices, async await javascript error handling
Id: DyqVqaf1KnA
Channel Id: undefined
Length: 12min 46sec (766 seconds)
Published: Thu Jun 11 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.