NestJS Error Handling Logging and Validation Best Practices | LinkedIn Clone [20]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we'll be working on error handling logging and some validation for our linkedin clone now previously if you were to log in with an email address that wasn't in the database you'd get a catastrophic error and the whole server would crash so we've implemented some generic error handling in the worst case scenario and we've also implemented some error handling for more custom behavior for particular endpoint so if i run this we can see that we've got invalid credentials as a custom error message coming back along with the status code the path the method and the time stamp if i were to do a get request but i wasn't logged in i'd get unauthorized here and likewise with a put request i'll also get unauthorized and if i wasn't the creator of the post i'd get forbidden there now in addition to the error handling and we can see that the server is still running quite nicely now we've added some logging here now this is logging for all of the endpoints and we've used morgan to be able to do this but we've also added some custom error handling so in addition to this we've got some uh information so the last uh endpoint record for example uh is api feed 2 is a put request and we got a 401 back here's the entire object and we got some information about the user so in this case we got the user who's not signed in but if i was to say for example sign in as a user here and i copy the json web token [Music] to that request now even though i wasn't the person creating the post and i shouldn't be able to edit it rather than in a 401 we'll get this 403 forbidden and if we take a look at the error log we can see that we have this forbidden resource but we also have information about the user and these are logged and they're just sent to the root directory here and finally we're going to cover some very basic validation so it should note that you know we might not want to necessarily um we want to if we pass something that wasn't an email for example we had the number five here obviously that's not going to be in the database but rather than pass back the uh you know the resource isn't there we can say that we're getting a 400 bad request so it's actually there's some class validation and it's going to say okay you haven't submitted the right form and that's happening before um it you know it goes to even check for anything in the database or anything like that so that's what we'll be working on in this video so let's go ahead and get started so here's our project up today and in the api folder we've got this auth and feed module here now i'm also going to want to create another folder and i'm going to call it shared actually i'll call it call because shared sort of refers to [Music] you know at the module level elements that are shared to other modules but this is more core functionality so handling exceptions and um stuff like that and logging so these are more general these can be used in any project whereas the shared stuff is particular for this project uh but of course it's up to you what you call it so in the shared folder i'm going to have an all exception filter and this is going to be a service so we can um use it well actually it's not going to be a service it's going to be a filter so we're going to have a class here and it's going to be called the all exceptions filter matches the file name but in the uh pascal case rather than the kebab case and ignoring the dot for the filter and putting that in the name and this is going to go ahead and it's going to implement the exception filter now this comes from nest js common so we have access to that and we're going to have to in order to use that and implement this we're going to have to um we're going to have to call the catch method so that gets rid of the first error now what we're actually going to catch here is you could actually type in here for example http um exceptions but we don't want to just handle http exceptions we want to handle any error at all so anything that can crash our server we want to be able to handle that so if we if we go ahead and let's just before i save this file let's just run the server here with a npm run start dev and we'll wait for that to map all the end points for us and it's successfully been bootstrapped so if we go ahead and we try to login as something that doesn't exist here we get a 500 internal server error and that's okay but the problem is we're getting this it's crashing our entire application so it's not necessarily um it's not the http exception in this case that's crashing it's this particular part here the password of undefined so if we have a look in the auth module here and we look at the auth controller that is the login method we can see that it refers to the login method in the auth service so if i just go ahead and open that we can see that well we haven't actually checked if the user exists or not so we have this login method and this calls the validate user but this validate user is just trying to find one user but the problem is if that doesn't exist we can't compare it or anything like that and obviously this is the place to check the error um to make sure that the you know this function is handled gracefully in the case that there is no user um but as a last resort we don't want our server to crash at all so we're going to look for all the we're going to catch all the errors and handle those in a way that doesn't let our server crash so i will come back to fixing this here and actually i will do that right now it's very simple basically what we want to do here is if we have a if there's no user here what we want to do is we just want to go ahead and throw a new and i just realized this whole thing this was being returned so i'm going to have to put curly brackets around next thing now because there's going to be more than one expression here in addition to the return statement so i'm just going to go ahead and cut this in here and that means i'm going to have to return this now and we're still getting a extra bracket somewhere i believe so let me just follow this through so this should be up here and i've just gone ahead and added the extra bracket in there and that allows us to put in this if statement here so if there's no user and obviously this has to be within the code block for the return statement if there's no user what i want to do is i want to throw a new http exception and you can pass in the message so we could say not found and we can also say we can use the enum of the http status dot not found and this is going to need to be imported from sjs common so all of the statuses so if i do f12 you can just see all the statuses are here and it makes it so that you can have a consistent way of showing a status so that's true um one thing i'll write here but i'll just comment something out is we may want to have a custom message so rather than or a custom exception so rather than just having the message and the status what we could do is i'll go ahead and duplicate this line here but within here as the first parameter we have our custom object so i'm going to have the status and that's just going to be the http status not found and we can also have rather than message we can have we can call it error and of course you can have whatever you like in here and you can go ahead and i might have a different error message here because this will be the one i actually want and this is the invalid credentials that we've seen earlier but of course i'm going to have to handle both of the cases here now we do need to tell the http exception what the actual status is so we can just copy this from here now we've got it here twice because it it needs this data here and i'll comment this line out here for a sec so we can see the coloring but essentially we need this here as a second argument so it knows what type of except um status this exception is going to be but then we can have our custom object that we want to return here and i've got error here rather than the built-in message that's nested within this object and i've talked about this uh preemptively because i'm anticipating that we're going to have to handle both of these cases so when we do an error handling application um by default exceptions will be raised in a particular way and will be thrown in this way here or we might choose to do that explicitly however if we want to do something more custom and return some a custom object back as in this particular case we're going to have to handle that case as well so if even if we save this here and we make our request we will get back this 404 invalid credentials which is this object here but we really want to be handling all of our exceptions uh a little bit cleaner so if i come to the all exceptions filter here so basically what i want to do here and of course if you're not throwing an error it won't catch that so we're going to have to catch all of the errors in addition to showing those error messages and we might want to adapt the format anyway so what i want to do here is this catch method it takes two arguments the first uh well parameter it's going to be unknown now it's not going to be the http um exception because it might not be a http related issue as discussed previously it could be relating to the methods itself although you certainly want to write your code in a way um so you're handling all those cases and ideally you'd want to set this up earlier on in the project so you don't have to go back and go through it all but i'm going to demonstrate it for this here and you also have the host which is of the type argument post and that just allows you to get some context about the response and the request that called this uh that got caught so i'm just going to have to get this type in from sjs common so we can just say we want to get the context which is coming from the host and we need to call this switch to http method it switched the context to http returns the interface with methods to retrieve http arguments so we can just get that which means we'll be able to get our response which is on our context object get response now we can see that it has this type here and it's going to have a type of response and this is going to come in from um this is actually going to come from express it's going to be an express uh let's see here express uh type here that sort of response because the word response is overloaded we need to get the right type there and in addition to the response we also want to get information from the request so the context relating to the api that caught it we can get the request and likewise in an analogous way you can get the request type from express also so it's not giving me the intellisense i want but i should be able to just save it like this and of course um i've got the equal sign here which might be adding to the intellisense issue however we're past that now so things we want to get we want to get the status and we want to get the error message however depending on whether or not the exception is an instance of the http exception we want to set it in a different way because obviously if it's nothing to do with http exception the status is internal as we seen earlier ie relating to the code and we want to give a 500 error here and return a critical response otherwise we can extract the status and the error response so with that said we can say let status be of the type http status and we won't instantiate that variable just yet and likewise with the error message and there'll be nothing more than a string so if we have the case where the exception and the exceptions being passed in here so in this case when we throw this exception this exception filter and that just reminded me in the app module here we're going to need to um added as a provider here so after the app servers we can create this provide and nest has these different types of providers that you can have so this is going to be a app filter and we're going to go ahead and we're going to use the class and we can just go ahead and we can copy this here the all exceptions filter and of course we need to do a control period to import that and that's all we need to do in the app module so i'll close that up so that means when this error gets thrown or if it gets thrown somewhere else in the application or gets handled implicitly or explicitly in either case we can deal with that exception so if we take a look at the exceptions here we can say if the exception and if that's an instance of the http exception then you want to say okay well we can get this status from that because it's going to be related to http otherwise we won't be able to get the status so we can set the status equal to the http status dot internal server error enum and then we can just type in this error message here and we can say we've got a critical internal server error occurred and i'm going to demonstrate how this is going to happen and soon so basically the status from any of the instances where it's a http exception we can say okay well we've got past the exception here and there's a method get status there so that's easy enough and we can construct our error message actually we'll get our error response because that's going to come from the exception and get the response and then based on that we can determine what our error message is going to be so our error message now we can get our error response um but if we take a look at the type inference we've seen we're getting this string or this object here so what i'm going to do is i'm going to create a in the models here a http exception response and a custom http exception response so i'm going to create a models folder and i'm going to create the interface http exception response and of course you call this whatever you want and then i'm going to suffix the file name with interface for the typescript file here and i'm also um actually i'll just explore the same file so i'm going to have the interface http exception response and we see by default we get this status and the error um although it's not by default uh entirely because i've created this format here but it's going to be related to the status code and the error message so i'm that's this that's the type i want i'm going to make it a status code of the number type and then also have an error which is a string so that's the standard http exception response i'm also going to just duplicate that because i'm going to have the http exception response a the custom one and that's simply just going to extend the http exception response interface that we've just created so it's going to have these two properties and in addition we're also going to get the path so the url path that we've called the method type so whether or not it's a get request or a put request or a post request and so on and we can also get the start time stamp because that's going to be importing for our logging so we can see when something's occurred so with that we can just go ahead and close the file now because now in our all exceptions filter here we can say okay so we know that the error response it's either going to be um you know the http exception response or it's just going to be a string um and if it's our custom object case so obviously if you're calling this standard uh throw method you're going to get the error or the exception message but we've got this nested structure here in our custom case where we got the our custom object and then we have this error method on top of that or nested in that i should say so if it's an instance of the http exception we know it's an http exception we can say well if it's of the type http exception response that we just created we can just go ahead and we can get the error and if that's and of course we're gonna have to import that however if we're using our other way of throwing errors we can just get that directly from the exception from the exception message and if you um don't have that it's going to like if it's not a http exception it's like something more serious we're going to get this critical internal server error and the reason we're logging these things is we might not be aware of these we might be you know trying to write super clean code everything's all handled but you know we're not perfect we might not get everything and we can periodically check the logs or we could even set up some sort of automation such as a bash script that goes line by line through the logs and looks for these sorts of things and it can inform us and it can even email us or something like that of course that's completely overboard for this simple example um but sort of just heading in the general direction uh of an enterprise application so basically we've got a different status and a different error message in either case but the way we construct our error response is going to be the exact same so we want our error response to be of the custom http exception response that we just created there and this is going to be equal to oh what's going on here so this may need to be within this catch still yes it will and actually i might actually go ahead and make a method here it will still be in the class but i'll make it a private method because it's only going to be used in this class and what i want to do is i basically just want to log something so if i make a private method and i will call it log error this will be a function and it's going to i have this sort of syntax i suppose so basically what we want to do is we want to create a error response and then we want to go ahead and get the error response so how about rather than log error we say get error response and for this we're going to need our some information so we're going to need our status we're going to which is of the type http status we're going to need our error or our error message to be explicit which is going to be a string and we're also going to need our request object and our request object is going to be the uh request here and let's just have a look here see it's the right request type so we need the request url and we need the request method so it looks like it's there so from here we can go ahead and construct our error response so we know we're going to be returning something of the custom http exception response type so we can simply just return this object here oh this yeah this object here and i believe you can use this syntax maybe it's just complaining because i haven't got the properties defined yet um so we can get the status the error which is the error message has been passed in the path which is the request url you also want the method request method and the timestamp which we can just say is the new date all right so it's happy with that so we can say error response is going to be equal to the get error response function and we might need to call this um and then we're going to have to pass in a few things here so i'm going to pass in status we're going to pass in error message we're going to pass in the request and that should give us our error response now we want to log the response so i'm going to have another method here and this is going to be called private log error and it's going to take the error response which of course is the custom http exception response and it's going to return nothing but it's actually going to do the work of logging it so basically this is where we can construct our message here so we can say i want to say let's say i have this constant variable here let's put this equal sign here of error log we're just going to use the template strings here we want the response code and we want that to be equal to the status so i'm also going to have extract those variables and they're going to be coming from the error response so i'm going to have the status code i'm going to have the [Music] the error which is the error message we're going to have the uh let's see here let's just work on what we've got the response code is going to be the status code and then we want to have the method here and we're going to need the request object for this so we will also pass in here the request and we can just copy that from above so we can also go ahead and from the request that was passed in we should be able to get the the method and also the url so we can just go ahead and put the method in here and we can continue to bring out uh type out our string literal here so we might want to have something like this and we can have the url and this is going to be equal to the request url and then that we want that on the first line and then we're going to have two lines we go to the next line there'll be a space between them and then we're going to want the jason we want to stringify our object because we're going to have want to display an object but in a nice formatted way and we know if we have a user we want to display that so if we have a user we display that but if not we can use the double question mark here and we can just say not signed in and then we can after that we can have two slash ends here um and then i want to show the uh exception stack so as a template literal here if the exception if that is an instance of the http exception so we'll have a ternary expression here or we can say that well we know that is on the exception stack [Music] so obviously we're going to need exception here as well so the exception in this particular case we do know it um because it's going to be well if it is an instance of the http exception so i suppose it will be as before where we have the exception which is of the type unknown but if it is of the http exception type we can get this stack so we can trace the error otherwise what we'll do here is we'll just say uh we'll just return nothing more than the error response message so we've got the error response here so we can just say all right give us back the error and then we can have two new lines so then the next request will be spaced nicely so we can come to our code here and we can call this function log error and we can pass in the error response here but we're also going to need to pass in the and of course this can just be url here and we can just pass in the um request and exception so we've got that from above here got the request and we've got the exception [Music] so we can just pass that in and call it and it's on the class we need to call this dot log error method and that should be doing the logging for us so and of course we need to log the uh actual thing so right here at the end here i'm just going to do a console log put a number seven here so i know which log i'm looking at and then i can put this here like this so i'm not logging into a file just yet um but it should be able to handle our cases so let's go back to here and we'll take off the throws and this was a catastrophic error because it should have been handled at the position of this function here [Music] now if i go to login as an email that doesn't exist let's see what we get so we see that we've get this 7 here response code 500 method post url api auth slash login there's no user signed in so we get not signed in and actually i might put the word user here where is it here so let's rerun this now we're getting our first line here we're getting this not signed in and then we're getting this critical internal server error occurred and that's what we want because it's not of the http exception type it's of a different type it's coming because the parameters uh well it doesn't exist and that's before it's been handled here so this same sort of approach should be done all throughout the code now i'm not going to do that because that's way too tedious and this is something you'd really want to do at the beginning of the project but if you do want to do that i'll leave that as an exercise to the viewer so if i get this standard exception here i can re send this request here and we get the 404 and then we get the stack trace here and i'm just realizing i don't think we're getting our object that we want so let's have a look here um so we've got our response code method and url and after that before our user we want the actual object as well so i'm going to say [Music] um in a template literal here jason.stringify and this is where we can just put in our object that we've just constructed so that's the error response here and then i'm just going to add two new lines here all right so let's do this again we run our request get the response code method url we get the object we get the user now right now they're not signed in but if they were signing you'll get details about the user but obviously this is the login method and then we get the stack trace and then we can see it points towards the service and so on and finally if i throw this custom error message this should be handled similarly but with a nested object structure for the error message and we can see here we get these invalid credentials as an error here so everything's working as expected so that's pretty much how you do error handling and right now we're just logging it to the console and you could imagine how that isn't uh extremely useful um so i'm going to have another method here and i'm going to say um let's see here vulgaro yeah i might change this to get error log and then i'll call this get error log and this is basically one big string it's returning here so we just want to return the error log and then that means we can have another method where we're actually writing the log to a file so if i say ah what's a good function name write rights error log to file okay so this is just going to take the error log which is nothing more than a string and it's not going to return anything so it's going to have the void return type but what it will do is it's going to use the file system so we're going to need to import that and we can import that from import or as fs from fs so if we come back to here and of course we can call the file in our function here this dot right error log to file and we're going to need to set this to a variable so let's call this error log which is a string although that's probably implicit um and then we can just pass this in to write the error log to our file so basically what we want to do is now that we have the file system imported from fs we can use it and we can say all right we want to append a file and we just want it in the root directory and we're just going to call this error log now the particular message that we want to put in here is just the error log and that's going to have all the formatting of the lines and stuff for us and then we just need to have the particular type that we're dealing with so by default you can say utf-8 and then we need a callback function and if some sort of error was to occur um we can go ahead and we can just say if error throw error and this will actually uh funnily enough refer to this own uh [Music] catching of the errors here since we're dealing with catching all the error types uh you could separate this into two like various files if you wanted to do something like this but there's a lot of duplication like that uh you could probably architect it in a way that suits your particular application or way you like to do things but of course that's up to you um and that's pretty much all you need to do here for that so after that's written to the file um we can just go ahead and we can return it to the person uh and you could return it before as well um but this final return uh statement uh is related to the catch here so if i just go ahead and right at the end here i say for the response so we're finally using this line here we know what the status is that's just nothing more than the status that we've derived and then we want to give back some json format of the particular error response that we've constructed here so let's save that now if i just open up the api folder we see right now we're not getting any files here but let's see what happens if i make this request here so now that we're actually returning json this doesn't hang for a while we get this 404 invalid credentials because this email doesn't exist and we get that return type there and in addition we also get this error log and look how nicely it's formatted we get the response code the method the url we get the object we get the user and then we get information about this exception for the core stack um i will show an example of us being signed in but just before i do that i might go ahead and download morgan or in store morgan so i'm just going to run an npm in store morgan and this is going to be very simple in comparison um to what we've just done and while i'm here at the command line i'm also going to run an npm install class validator and class transform and we'll see that in just a moment how that's used let's just go ahead and get those and of course if i was to do another request here while i'm waiting for that and i look into the um oh i stopped the service and nothing's going to happen um but i've got morgan now so let's go ahead and run the server again i was only doing that while i was waiting um but if i open up the main file what i want to do here is basically i want to import the file system because i want to write to another file so we're going to have two files we're going to have one for the error log and then we're going to have one for all of the requests that are made and we're using morgan for that one where the errors we're having a custom thing because we want to keep a closer eye on those things um such as critical errors or anything like that so we have a separate spot for that of course you can write a script or automate it in however you'd like but if i just go ahead and import the file system from fs and i can import morgan in an analogous way so i can import star as morgan from morgan so morgan's just a library that makes it easy to log things basically and put them to your file system so we want this on every single request that's why i'm in this main ts file here now before we bootstrap the application i'll just have a variable here and i'm going to call this log stream and we can use the file system and we can create a right stream so we're able to continuously write as we receive api requests and i'm going to put this in the api.log file and i'm just going to need a flag here so i'm going to put these flags here as a and a all that is it's nothing more than a pen so rather than concatenating to the end of the string in the file it's going to append so basically or overriding i should say it's going to append to the end of the file so basically right here after we set the global prefix we can say okay we can go ahead and we can use morgan and there's different ways you can do it you can have like a minimalist view or you can have a max view or a combined view uh i think combined looks the nicest so i'm going to use that one and that just it's basically how much of the request is displayed to you in each line of the wall so you can experiment with that you just need to look at the npm package to look at the different uh inputs there and we can just go ahead and we can pass in our log stream so we can see that we've now got this api log here so let's make a call let's make this call here and we see that we've got some information here so we've got the date the type the end point um you know the codes uh and there's also the environment it's running so uh you can get some slightly different uh output there if you experiment with the this here for example i believe there's tiny here like this uh if i make a request here like that um we can see that we've got this slightly different structure we get the time uh and so on uh actually i might just leave it as this one because i like having the time of the request here obviously you could delve deeper further again to the database to get the uh queries and see how fast they're executing and stuff but i believe this is sufficient um you can see that in the database anyway when you're running queries um but you know you do have type orm and there's this layer of abstraction on top of that so sometimes you need to determine whether or not your own queries are going to be more efficient uh than typeforms but they generally do a pretty good job certainly for massive operations you need to consider something like that um yeah so we've got error login we've got our custom api well we've got our api login for all of our requests and then we've also got this uh nice custom error messaging handling [Music] so we've done handling catastrophic errors so the application doesn't break we can now handle throwing exceptions even in the default way or the custom way uh so all that's really left now is some validation so i no longer need to console log this because we're getting um them logged out and i'm not going to have to if you have log files and using the uh default nest stuff if you look at the git ignore file there's this wild card and it ignores all the dot log files so it's like the convention for log files here you have a dot log of course you can do whatever you want um so yeah okay so i think one thing i'd like to do is just log in as a user so let's just log in as user one and now if i copy this code to uh you know get the user for example now would expect this to just go through so i actually wanted to do this api feed controller although we will come back to error i just spotted um and then we get this 403 because we weren't the user that created that particular post so we can't change it on the put request here um but then we can look we need more information so we made these requests here we got a 500 previously and i've got a 403 here and if i look at the error log look at the latest you know we can see um some information here we've got a forbidden and that's true that's exactly what we want and that's handled for us forbidden resource and we can also have a look at the uh critical error from before and we can see that it's a critical internal server error so that indicates that it's on the method there's some sort of issue uh but for this particular uh forbidden resource here um this has been handled nicely and what i really wanted to demonstrate at the end of all of that was the user and we now have the user object because that's attached to our request when we set up the json web tokens and attach that to the request so what i want to do just to finish off is i want to come to some validation sort of stuff so let's see what i want to do well i installed those packages before the class validator and the class transformer if i come back to this main ts file i can actually go ahead and i can say and i'll import this up here and i'll put the third-party morgan stuff and the built-in stuff at the top here so like nest at the top third party and built-in stuff and then uh my stuff however the particular import that we want here is going to be the validation part and this is coming from nest js flash common and basically what we can do here is before we do our login we can say okay we can go ahead and use the global pipes and we can just say new validation pipe now we will need to create that actually sorry we won't need to create that because it's we're using the built-in validation although you could create your own custom pipes but you won't really need to for what we're doing here but basically the thing i want to do for validation it's going to be quite simple it's just the built-in validation is basically when we go to log in and i'll change this to john 6 because now that doesn't exist but if i was to change this email to the number six we're still getting back invalid credentials here but we actually this isn't the right type at all so what we can do we can go to our user class uh we've actually created a user interface so if we go to core and we go into models and use interface now an sjs it lets you use interfaces or classes but there's a slight preference for classes and that's because you know it allows you to do some extra things such as class validation and stuff like that now i'm not going to go back through this whole project and change all the interfaces to class it's very simple all you need to do is change this to class and that does the change for you even all the inputs uh similar but i'm actually going to go ahead and rename the file now that's not interface it's the class so you can change that and i'm going to say yes update imports for user class yes and then i'm just going to go ahead and save all this just opened a whole bunch of files that for me here so i'm just going to close to the right those files that it opened up and now that we've changed it to a class you can just go ahead and add some properties on so if we take a look at the controller we are um our auth is relating to uh you know an email or a password basically so you can add these now you can say is email these uh decorators uh and that's coming from class validator and then on the password you can also have is string for example so just saving it like that and now if i do the request here we get our bad request so class validator handles all of that we just need to change the uh the dto layer into a class and then we can use the class validation metadata decorators and then it obviously it won't make it through um the request uh because you can see that the arguments passed to it uh aren't of the right type so it just detects that and gives back a bad request but of course if i go back to this version we get invalid credentials and of course if we look at our error logging scroll to the bottom here we can see that the uh we get a 404 here and then we've got a 400 for the bad request and all of these are being recorded here one thing to know is when you have a actual application um sometimes the uh server providers they might do some of this logging for you but this is just a way to sort of customize it to exactly what you want to do perhaps you want to get some very specific analytics for some specific endpoints and just makes it easier to do that but the drawback of that is um well the first is you're going to have to be doing these right operations continuously on your own server rather than through the third-party server uh it's pretty quick operation you can imagine if you had millions of people using this concurrently you'd really want to optimize your rights down uh but that's not really the biggest concern the biggest concern is if you're storing all this data and you have lots and lots of people uh you know writing things uh and this is true for third parties as well but uh you have to handle it directly if you're setting it up yourself is that these log files are actually the biggest part of the entire server um you know if you're not familiar with enterprise applications they may come as a surprise and you might think well why would you even have them but they're very they're an invaluable tool uh be to detect for bad behaviors because you might be wanting to detect things like people's ip addresses you could easily add that in um and if they're making you know multiple login requests maybe they've got the bot on trying to you know writing a script that's going through passwords just trying to get information you can go ahead and just have your own script that bans them or something like that or handles it however you want um now these are things you don't really think about if you're just you know a regular person on the website but we're dealing with the back end here so we're going to have to think of ways to make this secure and you know people can be quite creative so you know there's an extra setup there but you get the benefit of peace of mind uh over your server anyway that was a very large tangent um so we've set up error handling we've set up custom error handling we've set up logging and custom error uh logging with morgan and our own custom logging we set up some validation i just wanted to return to this error here and that's if there's no token we get this unauthorized but if we have a valid token we're getting this critical internal server error occurred and that's not what we want so since we're getting this critical internal server error occurred it implies that it's making into this part of the code block and just working backwards from there that implies that this catch uh you know this exceptions filters being called by that method somehow so uh the bad news uh [Music] is that when we look in our logging unlike some of this other logging we're getting for unauthorized and it goes through and the track core stack and you can see oh it's related to passport and unauthorized and stuff like that um because it's a catastrophic error um we're only getting back this critical internal server error occurred um but it sort of implies that the particular endpoint the get request for that particular method there's some sort of behavior that's no longer working as um we'd like now that we've introduced these new uh you know logging error handling and stuff like that and again this is something that really should have been set up at the beginning and then we wouldn't be in this predicament but we're going to have to work through this so let's just go have a look at the end point real quick so so i've just navigated to the user.controller and i've just commented out this jwt guard um just to see any effects that has so we're still getting our critical internal error so i'm suspect it's nothing to do with that uh which sort of indicates that uh well we did add this uh global pipes to the validation pipes and perhaps there's some sort of interaction between the uh you know what just changed here and that although it is a little surprising to be honest so now i just really want to get anything that works so i'm just going to cut out this here and cut out this here and also cut out this and just put you know just a number here just to see if the error is at this level if it's inside here could be we're trying to find a user that doesn't exist or something like that uh although that actually that might give a critical error uh as discussed previously but let's just go ahead okay so even with all that if we go into the so i'm gonna undo all that then and then i'm gonna go into this find user buy id function here and it potentially is because i've changed the user class um and we're doing some sort of mapping here deleting the password although that was happening before we put the validation on and this is actually a perfect example of why air handling is so important and that's because um i'll make i was making the request to get the id of two in postman here um but if you look at the database here the problem was that i did some deletions and now the primary key of two and three are deleted so they're no longer there and this is exactly why you'd want the error handling so um you can have something like this and i'm not gonna do this throughout the entire application but i will need to do in these sorts of areas that are breaking like this um um although i am i will mean i'm making the request if i make the request better i won't be getting these but any application you should be handling all of these cases uh and then i can just have this user not found exception here and then our error handling will go ahead and it will take over and it will handle that for us so that's awesome we can really just see how useful it was now what i'm going to do just before i finish off today is i'm going to cd into the front end and i'm just going to confirm that nothing's broken so i'm just going to run an ionic serve and we'll be pretty much ending there so let me just check things aren't working they are working now i'll just wait for this to load up all right so let's try the join uh say jono jono p johno p test.com password let's see we sign in yeah everything seems to be working obviously we don't have a couple of images but let's just go ahead and get an image going cool let's see if i can create something edit it all right everything seems to be working as before um all right awesome so yeah in summary we've done error handling logging custom logging and some validation and everything appears to be working obviously i didn't add the validation and error handling to all the endpoints again it's something you want to start from the beginning but if you do want to do that for this particular application please feel free um you know i'll leave that as an exercise to the viewer if you want to send a pull request or something like that i'll happily add it to the github source code um but if not uh i think that's all i really wanted to cover there on that one so i think if there's any demand for it i'll the next video i want to do is some web socket stuff uh building up the chat application uh so we've done the friend requests i want to make a new section um so there's a bit of work in that one so you can create chat rooms and use websockets to communicate to one another to each of your friends and stuff like that um so let me know if you want to see that one if you do oh yeah thanks so much for watching please subscribe to my youtube channel and i'll see you in the next video cheers
Info
Channel: Jon Peppinck
Views: 1,378
Rating: undefined out of 5
Keywords: nestjs, nestjs validation, nestjs error handling, nestjs tutorial, nestjs logger, nestjs typeorm, nestjs crash course, nestjs swagger, nestjs dependancy injection, nestjs exception filters, nestjs mongoose, nestjs training, nestjs validation pipe, error handling, nestjs microservices, nestjs validation pipe example, nestjs services, nestjs tutorials, nestjs sequelize, nest.js, nestjs middlewares, installation nestjs en francais, nestjs query param, nestjs js
Id: 2pkKd8WW6tc
Channel Id: undefined
Length: 75min 24sec (4524 seconds)
Published: Sun Aug 08 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.