Okay.
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
exception to our client.
And the idea of this is we're just going to format the response so that we can send the exception back
to the client whilst we're in developer mode.
We wouldn't send this level of detail back in production mode.
And that is the default behavior.
In fact, in our code, if we go and take a look at the program class, it's kind of hidden now in modern
versions of.
Net to hide or remove some of the boilerplate that we would see in our code and what we do have here
inside the middleware, although we don't actually see it, is we have this developer exception page,
and that's just automatically added unless we override it with our own exception or our own exception
middleware.
But this is what's going on, even though we don't see it in our code.
And that's responsible for sending back what we see in the response in postman.
Now we're going to take a look at using middleware so that we can send back our own version of list.
What I'd like it to be is a JSON formatted response, so that we can use this more easily in our client
application.
What we've currently got is just text that's coming back.
I would like this to be JSON, so we'll take a look at how we can do that.
And it gives us an opportunity to look at middleware as well.
As far as the other responses goes about the other HTTP response errors, I'm okay with them.
We can handle them inside our client just as they are, and then take whatever action we need to take
based on what type of HTTP error response it is.
But we will take a look at middleware initially, and I've just come to the ASP.Net documentation about
middleware, just to discuss what this actually is and what it does for us.
So as a request comes in to our API server, we have our request and it goes through a series of middleware
steps in what's referred to as a pipeline.
So the request comes in, it hits the first bit of middleware.
And then any logic inside this middleware can be executed against that request.
It can manipulate the request.
It can authorize the request.
It can do whatever we need to do with that request.
And once that has executed, we then call the next method and it gets passed to the next piece of middleware.
Logic gets executed in there, and then the next method is called again.
And then we go through the middleware pipeline.
And then we can do something with the middleware or the request on its way in through the pipeline,
or also on its way out before getting to our response.
Now, at any point during this journey, it's possible an exception can be thrown by any of the logic
in any of these bits of middleware, and if an exception is thrown, then the goal of this is to have
something that can catch that exception.
What we have currently is the developer exception.
But what we're going to do is create our own middleware, and exception handling middleware lives at
the very top of the middleware tree, because when an exception happens, it will be thrown and it's
going to keep being thrown back up the middleware tree until it gets to a point where something can
catch it.
So we're going to catch the exception at the highest possible level and then handle that exception.
So that's the goal of what we're about to do.
So inside VS code.
Let's close all of this stuff down and we will open up in the API folder.
We'll create a new folder and we'll call it middle.
And inside our middleware folder we'll create a new file that's going to be a class.
And we'll call it exception middleware and press return.
And inside here we're going to use an interface called I middleware.
And when we use an interface then we can use this to populate its required methods.
And I'll just use the quick fix.
And we can implement this interface.
And what we use here is the invoke Async method, and what's passed into this invoke async method as
an argument is the HTTP context.
So we have access to that and also the request delegate.
So we can call next and pass this on to the next piece of middleware.
Now we'll also add a couple of parameters arguments to this as well.
And we want to inject the environment that we're running in.
And also a logger so we can log information to our console.
So we're going to inject the host environment into this and call it env.
And we're also going to inject an ilogger just so we can write stuff out to the console.
And the type parameter for the logger is the name of the class that we're using this in.
So I'll specify exception middleware and we'll just call it logger.
And I'll just bring this down so it fits on the screen.
And this is a public task.
So we're going to make this method async.
And inside here instead of throwing a new exception we're going to use a try catch block so we can use
a snippet for that.
So I'm just going to select try for try catch.
And great.
Now we've got our try catch block.
So in the try block we're not going to do anything here apart from call the next method.
So that this goes to the next piece of middleware.
And we need to pass in the Httpcontext for this method.
And then we're going to catch the exception.
I'll remove system from there and just say exception x.
And then I'm going to say await and handle exception.
And we're going to pass in to this method that we don't have yet but we will do.
I'm going to pass in as arguments the context and the exception to this method.
Put my cursor inside here, and then I'm just going to generate the method handle exception.
So I'll split this lesson into two parts.
And in the next lesson we'll populate what we're doing inside the handle exception.
And also take a look at the program class where we use this middleware.
And we'll take a look at that next.