Error Handling in Express

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello this is bashar in this tutorial we are going to see how we can handle the errors in an express application in node.js as usual we have this repository right here and previously we have implemented the basic crowd application then on top of that one we implemented the page nation now we are going to implement the error handling in this tutorial so you can clone this repository and follow the steps we are going to do in this tutorial with me i've already cloned this repository and it's opened in visual studio code right here and if we go over the project structure we have this package.json and we have the dependencies of the express sqlize and sqlite and we also have this nodemon development dependency and this start script is running the index with the nodemon so it is detecting the changes and restarting our application whenever it is necessary and all our logic is here in this index index.js we have c clients for database connection and we are creating users initially and we are creating express application and we have the endpoints for adding getting updating and deleting user so if you would like to follow the previous steps you can just check the previous tutorials and see what we have done on this file so far now let's start the application i'm going to run npm start so this was initially creating 25 users we don't need that much user so let's reduce the number changing it to five five is enough now let's implement error handling functionality and let's do that on this endpoint right here here we are getting specific user based on its id and here we are taking the id coming in the request and running a database query for this id and returning the response back but the problem is our id for the user object is actually a number but in the request we can send this parameter as string so let's try that one first here in postman let's send a request to this endpoint this is users and let's say the id is one which is the user we have in database and here we are receiving this response but if we type id something like this one we are receiving this empty response back now let's implement a solution for this one first of all let's check if this id is really a number or not so we can do that control right here previously we have done similar thing here in the pagination tutorial where we were parsing the the parameters and converting them to the integer and controlling them if they are really integer or not so we will be doing same thing right here let's parse this incoming id and if this is not really a number and we can do that check like this if number is not a number this id if this id is not a number then we can return a response with the status code of 400 which is for bad request and we can send a response buddy and let's say that there will be a message in it and it's going to be invalid id now let's save this one and let's send the request with postman once again here we are receiving this 400 pad request and we are receiving the the message we are setting there now instead of handling the response like this here in the error case we can throw an error object so just like this one we can draw a new error and we can set the message like this invalid id then we can catch this exception in a common function like an error handler of the express so we can do it like this but first of all we have to comment this part out and removing this async from this function implementation we will get back to it so the implementation is like this we have this route handler function and it is checking the id coming in the request and drawing this error based on the the if the id's number or not so after our route handlers we will define our exception handler and we do that by running app use this is a function this uses a function and to this use function we pass our own error handling functionality and the error handling functionality is let's define it right here this is an inline error function this function takes four parameters the first one is the error the second one is the request direct the third one is res and the fourth one is next now if we just log the error here let's see what's going to happen so we are drawing error at this point and we expect this error to be locked right here saving this one going back to postman sending request we are not receiving a response for this one because if we check the console lock right here here we are seeing this log it is coming from this line this line this error handler is printing this part so we are able to catch the error right here in this function so in this function we can just return a response like this let's say we are going to return a response with the status code of 400 and we want to send a response buddy having a message and we can get the message from this error object this error is containing that message we are setting there and we can access to it like this error message let's save this one now the application is restarted going back to postman sending the request and here we are receiving this 400 pad request with the message we are setting so let's revert our implementation here let's mark our function as async once again and let's uncomment this part let's see what's going to happen the application is restarted sending post request once again again this is start we are not receiving the error response we are looking for and that's happening because the the errors drawn in this asynchronous function is not cached in this this error handler function we set here so express currently does not support that as a solution we can send this error to our exception handler by using the third parameter of this function currently we are having only the request and response in our route handler but we can have a third parameter which is next and instead of throwing this exception we can pass it to next function like this so we call the next with this error and the next is this function we use here so the order is important again we are having these rest endpoint handlers and after them we are defining our error handling function here so let's save this one the application is restarted again sending the request and here we are receiving this invalid id with the 400 bad request now let's throw exception for the user not found conditions so we are querying the table we are getting the user object but let's try that in the postman first so for the user one we are receiving this user one for the user five we are receiving user five but there is only five users and if you try to get the user six we are receiving 200 okay with enter response body so let's fix this if the user does not exist with this idea let's return an error message back and we can do it like this if there is no user let's send an error to next which is our exception handler and let's say this is this user not found let's say this and let's go back to postman sending the request here we are receiving the error message but the problem is the the status code for this one is also 400 pad request but for the not found cases the response status code must be 404 so instead of drawing this error object we can create our exception functions so let's do that right here we can have let's say this is for invalid id exception [Music] and this is just a function having the let's say this is going to include the status which is 400 and it will have the message and that's going to be invalid id and similarly we will have user not found exception and this one will have the status of 404 and it will have the message of user not found so instead of throwing this error let's draw just the invalid exception here and in this one let's throw the user not found exception now these exceptions are sent to this exception handler and we can get the message they have the message fields but also they have the status so we can use the status coming with the error right here so we can say error status at this part saving this one going back to postman sending the request and here we are receiving this 404 not found but if we go with this invalid id like this one we are receiving 400 bad requests with the corresponding message now since we are returning some generic error object we can extend this error object body and we can let's say return an object like this not only we have the message but we can print the the timestamp let's say we will have a timestamp of this error like this one we log the current time and let's also lock the pad so it will be request original url let's save this and if we go back to postman once again sending the request here we are receiving this message invited id with the timestamp and the path and if we go to this unknown user we are receiving this user not found with this timestamp and with this pad so that's all for this tutorial thanks for watching and hope to see you in the next tutorials
Info
Channel: Programming with Basar
Views: 591
Rating: 5 out of 5
Keywords:
Id: xdsm3QMSX6c
Channel Id: undefined
Length: 13min 29sec (809 seconds)
Published: Sun Mar 28 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.