Hi everyone.
In this video let me show you the core concept of Nest.js will be exception filters.
Okay.
Click on the exception filters right here.
And where what is the exception filter?
If you already built the API inside the expressjs framework we use the custom the error by hand,
right?
For example like bad request exception or unauthorized exception or internal exception.
Right.
But with but with NestJS, it's already have a some exceptions okay.
By default it will be used the exception which will be the internal server.
Okay.
It also provides something like this.
This is the global exception filter.
And let me show you how we can throw the exceptions.
Okay.
Nestjs have a HTTP exception inside the nestjs/common.
Okay.
We can throw something like this.
Let me show you.
For example, I have a service right here.
I get the data, but before I get the data, I will throw new.
Let me see, HTTP exception, that it will be exception.
Okay.
We can use for with that.
Let me be forbidden.
And the status?
The status is already had.
Status.
Inside the HTTP status.
Constant status.
from common right here.
Okay.
And .FORBIDDEN
Like this.
And I think I will open the postman to test this.
Let me create and I will close the other tab.
Okay.
I use get method.
And I will copy the endpoint here.
Paste it, and if I send request, now you will see it has a forbidden http status code.
And we some message right here.
Very cool.
Right.
Now
This here will be a
Right here.
I can change the message.
For example.
This one.
Okay, let's see, if I send okay, we have the message, right?
Very good.
And, we can custom that, for example, I don't want the throw with the some arguments like this.
I can easily create the custom exception.
For exception.
Okay.
I can create a class called forbidden exception.
And let me copy everything right here.
Paste it's okay.
Export a class.
And make sure we will inherit from at the http exception.
And import that okay.
And right now, how we can use that.
We can easily throw forbidden exception, from my exception
Okay.
Now, let me see, I can send.
And it also work, right?
Okay.
Here is a video.
And actually in the nestjs, it's already built-in the some exception for me.
For example, like bad exception, unauthorized exception, not found forbidden.
Uh, and many more.
We can easily do that.
For example, I can throw new BadRequestException with something like.
Data not found, for example.
Like this
And if I send, you can see, it will have a message data not found and error and the 400
Very cool.
Right.
So I think that's for the video.
Let me show see.
Okay.
We have many more.
And it have an internal server error.
Internal server error reception.
Very cool.
And for that reason we doesn't need to custom.
Okay.
But if you want to custom you can you feel free to use in your case.
Okay.
And that's for the video.
I will see you in the next one.