This episode is for members only

Level up with a premium membership

Join now
Already a member? Sign in to continue
Playing
46. Creating an exception handler

Episodes

0%
Your progress
  • Total: 4h 32m
  • Played: 0m
  • Remaining: 4h 32m
Join or sign in to track your progress

Transcript

00:00
In the next few episodes, we're going to be talking about custom exception handling.
00:04
And really, the main goal here is to do something like render out a custom 404 page for any of the errors that we might get in our application.
00:13
Let's just remind ourselves what this actually looks like. So let's try and access a route that doesn't exist. And we know that we get this not found exception thrown by leaked router.
00:22
And of course, it's rendered out really nicely. But if we don't have debugging enabled, we want to show a nice custom 404. So where do we do this?
00:33
Well, let's go all the way back to our app that we created earlier, our app class. So let's pull this open, app.php, under our core. And let's go down to where we dispatch this in here.
00:46
And we saw that we could catch an error or an exception inside of here. Now, what we want to do is inside of here, not specifically inside of here, we're
00:56
going to build out a separate class to handle this so we don't have too much going on in here. But we basically want to check the status code. And if we have a status code, we want
01:06
to respond with a view rather than just throw the exception. And then any other exceptions that we're not handling, we'll just rethrow them so we can see them on the page. So first of all, let's build out a custom exception handler
01:19
that will handle this for us. OK, so I'm going to create our new directory in the route here called exceptions. And let's go ahead and create our exception handler in here.
01:29
So let's call this exception handler. And let's go and grab this out of our container with auto wiring inside of app. So to do this, we're going to go and grab this container.
01:43
So we've got that available in here. And we'll just get our exception handler out. And then we want to call some sort of method on it, like handle. We want to pass some data down to this, like the request,
01:54
so we can access what the status code is and all that kind of stuff. And what we really want in here is to pass the response as well so we can, within our handler, write to the response.
02:05
And we know that writing to the response means we can render out a view. So we also want to pass the response in there. And last and more importantly, we
02:14
will pass the exception in so we can rethrow that in there if we want. So let's just refactor this just to move it over to a class. And then we'll see how we can do views a little bit later.
02:25
OK, so we want to create a handle method inside of here. So let's go ahead and create a handle method out in here. And we know that we are accepting in a request, a response, and an exception.
02:38
And then we just want to rethrow the error just for now. So we'll get our request in here, which is our server request interface request. And we'll have a response interface
02:48
as well, which will be our response that we can write to. And then we'll have that throwable exception in here. So this should just work as normal. If we go ahead and hit this, you can
02:58
see that we get exactly the same thing. What we can do now, though, in here is anything else that we want to do. And what we really want to do is overwrite the response
03:07
and return it if we want to decorate the response. So if we want to render a view, otherwise, we just want to throw this error. So now we have created a custom exception handler
03:17
inside of here, which deals with any of our exceptions. So we can add if statements to this if we need to check certain things about the request and handle the errors accordingly.
03:28
Let's go over to the next episode and see that now that we've got this exception handler, how we can render out a view rather than just see this exception here.
50 episodes4 hrs 32 mins

Course overview

Starting completely from scratch, build a modern PHP framework with all the features you’d expect.

Whether you’re new to PHP or not, this is a great exercise for learning what happens under the hood, arming you with knowledge you can apply anywhere you use PHP.

We’ll cover routing, controllers, views, the container, accessing the database, models, authentication, config, CSRF protection, exception handling, pagination, validation, flashing messages and much more.

Let’s dive in and build a PHP framework, step-by-step!

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Comments

No comments, yet. Be the first to leave a comment.