In this series you’ll learn how to make a ninja-themed API with Nest.js, which is a node.js framework for making server side applications.
⭐ Thanks to Marius for making this course. Subscribe to his channel here –
?? Nest.js docs –
?? VS Code –
All right so we're pretty much like 90 Of the way there and having a pretty Reasonable crud API however remember That in our service we added some logic Here of if a ninja was not found it Throws an error so let's take a look at How Nest behaves when we get into that Situation so we'll do a get on slash Ninjas and then we'll provide it with You know a number that clearly is not in Our collection of ninjas with the ID Correct so hit send there and you'll see That we've got an internal server error And the reason that happened is because Again we threw this error so imagine That that bubbles up to our get one Ninja controller method here but then From there Nest doesn't know what to do With that error so then it kind of Assumes that this is a server error We're gonna respond with a status 500. Now what would be great is if we were Able to perhaps return with a 404 Response the way to achieve that is Using the built-in exception classes in Sjs so let's wrap this in a try catch Right so we'll try to perform this get But we'll also catch the error if Something happens with that get so in The catch here what we're going to do is We're going to throw another error a Specific error The not found exception from an sjs so We're effectively just mapping the error
That we have here in our service to a Not found exception and let's take a Look at how that behaves when we do the Request again in this non-existing ID Earlier remember that we got back a 500 Response this time we're going to get a 404 response it says that status code 404 message not found so Nest has a Bunch of these built-in classes If you take a look at the exception Filters documentation you'll see that There's built-in HTTP exceptions You know so they have all the typical Ones that you probably would need so for Example bad request that's a 400 right They have stuff for auth so if you need Like a four or one or a four or three That's usually you know either the Unauthorized or the Forbidden exception We just saw using the not found Exception right and there's a bunch of Others here that uh you know represents Other typical statuses that you might Return you know even the teapot Exception is in there if you want to use That right so the thing to understand Here is that an sjs has these things Built in if you throw those specifically It automatically knows to respond in a Certain way now you might be wondering Why don't we just throw the not found Exception in here which you could but The thing I was trying to point out here Is typically imagine that you're talking
To a database right you have some kind Of database driver that driver is Probably going to throw in its own Special database not found record not Found error right so to typically you Won't see the the exception the HTTP Exception being thrown here it'll Probably be the underlying Services That's throwing a specific error which You might want to catch in the Controller for example maybe you want to Do some kind of if error instance of You know DB exception And then based on what that Cod error is It's up to you to then rethrow a Specific exception from this JS to Respond in a certain way now in some Cases you might want to create you know Your own exception classes you can Create your own custom ones you can Extend existing exceptions or you can Use the base HTTP exception and extend It or you can even use one of those Standard exceptions and pass in a custom Message if you want so in our case we Might change it to something like ninja Not found right so you might be Wondering how does Nest know to respond With the correct status code based on The classes that you're throwing Basically in Nest there is a concept of Exception filters which are responsible For you know effectively catching any Exceptions that were not caught by your
Own code and the default exception Filter that Nest has is set up so that It's meant to specifically catch each of These exceptions that are built in and It determines what the status code to Respond with now you can fully customize How this exception filter behaves so for Example like we said maybe you have a Database driver that throws a very Specific exception when it can't find a Record in the database in that case you Might want to set up an exception filter That automatically catches that specific Exception and responds with a 404 that Way you don't have to explicitly keep Catching it like the way we're doing it Over here right that would be kind of Annoying to constantly rethrow a not Found exception so know that there is a Possibility to extend and customize this Behavior that's a little bit more Advanced so we're not going to cover That in detail here but I wanted to Mention it anyways so that's pretty much Exception handling in a nutshell in sjs