WEBVTT

00:00.410 --> 00:00.770
Okay.

00:00.770 --> 00:05.810
So what we're going to take a look at in this lesson is we're going to deal with how we send back an

00:05.810 --> 00:07.370
exception to our client.

00:07.370 --> 00:13.190
And the idea of this is we're just going to format the response so that we can send the exception back

00:13.190 --> 00:15.590
to the client whilst we're in developer mode.

00:15.590 --> 00:19.040
We wouldn't send this level of detail back in production mode.

00:19.040 --> 00:20.870
And that is the default behavior.

00:20.900 --> 00:26.780
In fact, in our code, if we go and take a look at the program class, it's kind of hidden now in modern

00:26.780 --> 00:27.710
versions of.

00:27.950 --> 00:34.820
Net to hide or remove some of the boilerplate that we would see in our code and what we do have here

00:34.850 --> 00:41.120
inside the middleware, although we don't actually see it, is we have this developer exception page,

00:41.120 --> 00:48.380
and that's just automatically added unless we override it with our own exception or our own exception

00:48.380 --> 00:49.040
middleware.

00:49.040 --> 00:52.250
But this is what's going on, even though we don't see it in our code.

00:52.250 --> 00:57.050
And that's responsible for sending back what we see in the response in postman.

00:57.050 --> 01:02.390
Now we're going to take a look at using middleware so that we can send back our own version of list.

01:02.420 --> 01:09.250
What I'd like it to be is a JSON formatted response, so that we can use this more easily in our client

01:09.250 --> 01:09.850
application.

01:09.850 --> 01:12.280
What we've currently got is just text that's coming back.

01:12.310 --> 01:16.000
I would like this to be JSON, so we'll take a look at how we can do that.

01:16.000 --> 01:19.240
And it gives us an opportunity to look at middleware as well.

01:19.450 --> 01:24.970
As far as the other responses goes about the other HTTP response errors, I'm okay with them.

01:24.970 --> 01:30.670
We can handle them inside our client just as they are, and then take whatever action we need to take

01:30.670 --> 01:34.600
based on what type of HTTP error response it is.

01:34.600 --> 01:40.990
But we will take a look at middleware initially, and I've just come to the ASP.Net documentation about

01:40.990 --> 01:45.280
middleware, just to discuss what this actually is and what it does for us.

01:45.280 --> 01:53.740
So as a request comes in to our API server, we have our request and it goes through a series of middleware

01:53.770 --> 01:57.070
steps in what's referred to as a pipeline.

01:57.070 --> 02:00.160
So the request comes in, it hits the first bit of middleware.

02:00.160 --> 02:05.530
And then any logic inside this middleware can be executed against that request.

02:05.530 --> 02:06.820
It can manipulate the request.

02:06.820 --> 02:08.380
It can authorize the request.

02:08.380 --> 02:11.590
It can do whatever we need to do with that request.

02:11.590 --> 02:19.240
And once that has executed, we then call the next method and it gets passed to the next piece of middleware.

02:19.630 --> 02:24.280
Logic gets executed in there, and then the next method is called again.

02:24.280 --> 02:27.130
And then we go through the middleware pipeline.

02:27.130 --> 02:32.980
And then we can do something with the middleware or the request on its way in through the pipeline,

02:32.980 --> 02:37.390
or also on its way out before getting to our response.

02:37.450 --> 02:44.140
Now, at any point during this journey, it's possible an exception can be thrown by any of the logic

02:44.140 --> 02:51.370
in any of these bits of middleware, and if an exception is thrown, then the goal of this is to have

02:51.370 --> 02:53.980
something that can catch that exception.

02:53.980 --> 02:56.950
What we have currently is the developer exception.

02:56.950 --> 03:01.840
But what we're going to do is create our own middleware, and exception handling middleware lives at

03:01.840 --> 03:09.090
the very top of the middleware tree, because when an exception happens, it will be thrown and it's

03:09.090 --> 03:14.550
going to keep being thrown back up the middleware tree until it gets to a point where something can

03:14.550 --> 03:15.480
catch it.

03:15.480 --> 03:21.630
So we're going to catch the exception at the highest possible level and then handle that exception.

03:21.630 --> 03:24.030
So that's the goal of what we're about to do.

03:24.060 --> 03:26.130
So inside VS code.

03:26.130 --> 03:33.810
Let's close all of this stuff down and we will open up in the API folder.

03:33.810 --> 03:36.810
We'll create a new folder and we'll call it middle.

03:36.840 --> 03:43.140
And inside our middleware folder we'll create a new file that's going to be a class.

03:43.140 --> 03:48.570
And we'll call it exception middleware and press return.

03:48.720 --> 03:54.900
And inside here we're going to use an interface called I middleware.

03:55.110 --> 04:00.060
And when we use an interface then we can use this to populate its required methods.

04:00.060 --> 04:01.650
And I'll just use the quick fix.

04:01.650 --> 04:05.130
And we can implement this interface.

04:05.130 --> 04:13.040
And what we use here is the invoke Async method, and what's passed into this invoke async method as

04:13.040 --> 04:15.110
an argument is the HTTP context.

04:15.140 --> 04:18.620
So we have access to that and also the request delegate.

04:18.620 --> 04:23.390
So we can call next and pass this on to the next piece of middleware.

04:23.420 --> 04:28.340
Now we'll also add a couple of parameters arguments to this as well.

04:28.340 --> 04:33.440
And we want to inject the environment that we're running in.

04:33.530 --> 04:38.150
And also a logger so we can log information to our console.

04:38.180 --> 04:42.800
So we're going to inject the host environment into this and call it env.

04:43.160 --> 04:48.590
And we're also going to inject an ilogger just so we can write stuff out to the console.

04:48.590 --> 04:54.050
And the type parameter for the logger is the name of the class that we're using this in.

04:54.050 --> 04:57.830
So I'll specify exception middleware and we'll just call it logger.

04:57.830 --> 05:01.250
And I'll just bring this down so it fits on the screen.

05:01.250 --> 05:03.860
And this is a public task.

05:03.860 --> 05:06.110
So we're going to make this method async.

05:06.410 --> 05:11.940
And inside here instead of throwing a new exception we're going to use a try catch block so we can use

05:11.940 --> 05:13.140
a snippet for that.

05:13.620 --> 05:16.080
So I'm just going to select try for try catch.

05:16.080 --> 05:16.950
And great.

05:16.980 --> 05:18.870
Now we've got our try catch block.

05:18.870 --> 05:23.280
So in the try block we're not going to do anything here apart from call the next method.

05:23.280 --> 05:27.330
So that this goes to the next piece of middleware.

05:27.330 --> 05:32.010
And we need to pass in the Httpcontext for this method.

05:32.010 --> 05:34.860
And then we're going to catch the exception.

05:34.860 --> 05:38.820
I'll remove system from there and just say exception x.

05:39.210 --> 05:44.670
And then I'm going to say await and handle exception.

05:45.090 --> 05:49.200
And we're going to pass in to this method that we don't have yet but we will do.

05:49.230 --> 05:54.510
I'm going to pass in as arguments the context and the exception to this method.

05:54.510 --> 06:00.090
Put my cursor inside here, and then I'm just going to generate the method handle exception.

06:00.090 --> 06:02.250
So I'll split this lesson into two parts.

06:02.250 --> 06:06.330
And in the next lesson we'll populate what we're doing inside the handle exception.

06:06.330 --> 06:10.950
And also take a look at the program class where we use this middleware.

06:10.950 --> 06:13.380
And we'll take a look at that next.
