‫All right.
‫So in the last video, we've created a query called Teacher where you can specify the idea of the teacher
‫to fetch just one individual teacher.
‫And that's the message that we've got when we used an ID that we know does not exist.
‫It looks kind of messy.
‫There is a message.
‫The code seems to be internal server error, which is 500 in HTTP terms.
‫And this usually means that something bad has happened, but we don't know exactly what happened.
‫Well, that's not really the case in the stack trace.
‫We see exact exception dust was thrown.
‫This is entity not found error and it was thrown just because we have used a specific method inside
‫this data resolver, which is find one or fail.
‫This is exactly how this method works.
‫It will try to find one record using the specific criteria that you specify or it will fail.
‫And the failure is just throwing this entity not found exception.
‫That's all it does.
‫So to handle this error and other errors that might happen in your application and they aren't really
‫thrown because something went terribly wrong.
‫There are just something that is predictable and kind of expected.
‫Like in this case, something is not found.
‫And this is our typical thing that happens in web applications.
‫There is a specific error code for that in HTTP, which is 404.
‫And in this video we will see how to generate such a nice error messages using the built in nest feature
‫called exception filters.
‫So let's talk about the exception filters first, shall we?
‫Now, exception filters is a generic thing, generic feature to handle and handled exceptions inside
‫your nest JS applications.
‫It works with Rest and GraphQL, so it is a generic thing.
‫So let me scroll to some interesting examples.
‫So first off, you can handle any logic by wrapping it in a try catch block where you try to do something.
‫And when there is an error, no matter what kind of error, you just throw an HTTP exception, which
‫should then return some kind of error message to the user.
‫You can also create custom exceptions.
‫So we already know that there are some built in HTTP exceptions in NEST for some specific cases and
‫some specific HTTP error codes like not found, etc..
‫Now typically when returning a response, especially a response with an error, you'd like to be more
‫specific.
‫You'd like to say what has happened.
‫And for that you can create an exception filter class like this one.
‫This is an arrest example in this case.
‫And the way it works is you will create a class decorated with a catch decorator.
‫You would specify which exception classes should be cached.
‫And whenever an exception of this type is being thrown or an exception that extends this base class,
‫then you can customize what kind of error and what kind of response is then being returned from your
‫application.
‫And basically this is what we are going to do in this video.
‫So let's jump to the code editor and let's create a custom exception filter for this specific entity
‫not found error.
‫So let me create a new file inside as our C folder.
‫And let's call that entity node found error dot filter ts and let me export a class from it.
‫This can be called entity not found error.
‫Filter.
‫Well, actually, we need to import this one, so it's nice that it was automatically imported.
‫We'll have to import this exception here as we need to add a decorator.
‫This is cache imported from Edge's slash common.
‫And we specify this exact class that we want exception filter for this specific exception.
‫And since we are creating an exception filter that is specific for GraphQL, it needs to implement a
‫specific interface which is called GCL exception filter.
‫Now this one also has to be imported.
‫That's this line.
‫So we need to import it from the next GraphQL namespace and let's use VTS code to implement this interface.
‫Let me just hit this button.
‫You can also add this manually.
‫It needs one method called catch.
‫This method would be the place where we will specify what's the actual response we would like to return.
‫The first thing you need to do is to just call this GCL argument host Create method on this host argument
‫to convert everything to GraphQL.
‫This class has to be imported from this GraphQL namespace.
‫And the next thing is actually returning the final exception, the final HTTP error that we would like
‫to produce.
‫So in those cases we just want to return not found exception.
‫This is one of the built in nest exceptions.
‫It has to be imported from this namespace.
‫And you specify an error message.
‫This could be just an entity not found.
‫Don't forget the new keyword here.
‫All right, so now we have this exception filter.
‫We need to register it.
‫And this is done inside the main file the same way as we register validation pipes.
‫We need to go use global filters for every exception filter that you create.
‫You will have to call this one.
‫And let's do an entity not found error filter.
‫All right, so now save changes and let's jump back into the playground and let's try the same query.
‫So the message is exactly what we have specified in our exception filter.
‫We've got a404 code, which is HTTP four not found, and then this data is repeated here in the response.
‫All right, so let's recap so you can create an exception filter class that needs to be decorated with
‫this catch decorator for GraphQL.
‫It has to implement this specific interface and it discards decorator.
‫Specifies for what kind of exceptions this filter is, what exceptions from and on handled it needs
‫to catch.
‫And then for all those exceptions you just return some other generic exception.
‫Like in this case we have returned and not found exception.
‫Then you have to register such classes in the main file using use global filters method.