WEBVTT

00:03.870 --> 00:04.320
No.

00:04.320 --> 00:10.470
Geez, errors are really, really powerful and I honestly don't see too much of the need to customize

00:10.470 --> 00:11.660
this module itself.

00:11.670 --> 00:16.440
So notice here you can see that there is a new error and you can simply just come up here and raise

00:16.440 --> 00:16.860
the message.

00:16.860 --> 00:21.570
So all you got to do whenever you need to send an error response as a message, just like we have been

00:21.570 --> 00:22.140
saying.

00:22.140 --> 00:24.510
Resident Send you simply go ahead and say.

00:24.510 --> 00:28.830
Resident Send inside that you just come up here and say, Hey, just construct a new error.

00:28.830 --> 00:29.430
That's it.

00:29.430 --> 00:30.390
That's all you got to do.

00:30.390 --> 00:34.260
And you will see that sometimes we'll be sending the error messages when let's just say we didn't find

00:34.260 --> 00:40.740
the user inside our database or something, we'll be sending these messages and it is perfectly 100%

00:40.740 --> 00:45.930
okay to just go ahead and say that, hey, I want to use this default new error message, but I've seen

00:45.930 --> 00:48.480
people actually overcomplicating this stuff.

00:48.480 --> 00:53.340
I'll show you how they do it and how we can keep it actually bare minimum there so that, you know,

00:53.340 --> 00:58.170
okay, I can go ahead and create these customize error messages or customize error handlers as well.

00:58.200 --> 01:02.100
But my recommendation is you shouldn't be doing that, at least not too much.

01:02.100 --> 01:03.840
So let me go ahead and bring it up to you.

01:04.320 --> 01:08.760
So how we're going to be working with that now, this is going to be our very first utility that we

01:08.760 --> 01:09.630
are going to go ahead and.

01:09.630 --> 01:14.790
Right, so open this up new file and let's call this one as simply a custom error.

01:15.660 --> 01:15.860
Jeez.

01:15.990 --> 01:16.690
Of course.

01:16.710 --> 01:17.490
There we go.

01:17.520 --> 01:22.560
Now in here, we want to go ahead and simply create a class what this class is going to be called as.

01:22.560 --> 01:26.280
So we're going to call this one as custom header just to look fancy.

01:26.280 --> 01:27.780
You don't need to create that.

01:27.780 --> 01:31.380
And this obviously is going to go ahead and extend what class.

01:31.380 --> 01:33.870
We're going to extend the classic error class.

01:33.870 --> 01:37.320
So you obviously need to study it a little bit more in case you want to go with that.

01:37.590 --> 01:41.580
And I have seen in some of the application that this is how they work on.

01:41.580 --> 01:47.390
And finally after that, they go ahead and say, hey, module exports and equals errors.

01:47.400 --> 01:49.050
That's it, that's it.

01:49.050 --> 01:52.770
I have seen this class to be totally empty, no functionality, nothing.

01:52.770 --> 01:55.260
But they don't want to use the classic error class.

01:55.260 --> 01:57.120
They want to use own their customer.

01:57.120 --> 02:01.260
So this is kind of a thing that I've seen in many of the production code even as well.

02:01.380 --> 02:04.620
Now further down the road, you can do in case you want to truly customize.

02:04.620 --> 02:06.660
This is by default.

02:06.660 --> 02:09.390
This class actually takes just one thing, which is a message.

02:09.390 --> 02:14.700
Now you can kind of redefine the constructor in this class that instead of calling new error, you call

02:14.700 --> 02:21.150
new customer and it accepts error message error code, probably something more of the definitions that

02:21.150 --> 02:23.460
you want to give it to that so you can go ahead and do that.

02:23.460 --> 02:27.960
All we got to do for that is create another constructor, kind of overriding the existing constructor.

02:28.110 --> 02:31.290
So there constructor the error, just take the message.

02:31.290 --> 02:32.880
So we will take that as well.

02:32.880 --> 02:34.170
We don't want to change that.

02:34.170 --> 02:38.190
And you can go ahead and you can go ahead and say, hey, I want to accept the code as well.

02:38.190 --> 02:38.850
That's it.

02:38.850 --> 02:41.070
And this code is rather a status code.

02:41.070 --> 02:43.170
So just go ahead and provide me that as well.

02:43.170 --> 02:47.100
Further down the road, you're going to go ahead and say, hey, I want to use the functionality which

02:47.100 --> 02:50.880
is built in, so I'll use the super keyword and I'll just pass on the message as it is.

02:50.880 --> 02:55.050
But further down the road, I'm going to go ahead and say, Hey, I'm creating a new property which

02:55.050 --> 02:58.290
is going to be filled up by the code that you are giving to me.

02:58.290 --> 02:58.890
So that's it.

02:58.890 --> 03:00.030
That's basically it.

03:00.150 --> 03:06.450
Now, whenever you want to use this one, this error message, since this is kind of a super or a parent

03:06.450 --> 03:10.800
of this error, this will definitely display as it is how they were displaying the message, but now

03:10.800 --> 03:12.330
they will display the code as well.

03:12.540 --> 03:13.830
That's basically it.

03:13.860 --> 03:19.290
Again, I don't see a point of doing this because already you can you have the option of raising the

03:19.290 --> 03:20.340
status inside.

03:20.340 --> 03:21.720
Whenever you send the request.

03:21.720 --> 03:24.630
You can also go ahead and use this error and message.

03:24.840 --> 03:29.850
But this is like a little bit of an overkill that I see a lot of places that that is exactly why I wanted

03:29.850 --> 03:31.200
to bring this to your attention.

03:31.230 --> 03:35.940
I will show you that how you can use this custom error as well as just the regular error class itself.

03:35.940 --> 03:39.690
And we will be deliberately creating some of these errors and working through with that.

03:39.690 --> 03:40.860
So that's basically it.

03:40.860 --> 03:43.260
That's exactly how you handle customer errors.

03:43.260 --> 03:45.750
And again, there are a lot more properties going up here.

03:45.780 --> 03:49.680
Maybe stack, trace, maybe you want to go with the error code itself.

03:49.680 --> 03:50.910
So this is exactly what we have.

03:51.270 --> 03:55.680
We have got an error code, we have got error message error stack.

03:55.680 --> 03:58.170
There is a whole lot you want to overwrite any of them.

03:58.170 --> 04:00.150
Go ahead, be my guest and do that.

04:00.150 --> 04:02.550
But at least not till the code is finished up.

04:02.550 --> 04:07.140
So this is basically your customized error handling in the Node.js personal recommendation.

04:07.140 --> 04:08.430
You go with the classic error.

04:08.430 --> 04:10.500
Let's go ahead and catch up in the next video.
