This episode is for members only

Sign up to access "Build Your Own PHP Framework" right now.

Get started
Already a member? Sign in to continue
Playing
46. Creating an exception handler

Episodes

0%
Your progress
  • Total: 4h 45m
  • Played: 0m
  • Remaining: 4h 45m
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.

Episode summary

In this episode, we start exploring custom exception handling in our app. The main goal is to set things up so we can display a custom 404 page (or another error page) whenever something goes wrong, instead of just seeing a generic error message or stack trace.

We first look at how errors are currently handled—if we hit a route that doesn't exist, we get a default 'not found' exception. That's fine for debugging, but for production we want something much friendlier for users, like a tailored 404 page.

To make this happen, we head back to our App class and start organizing our error handling. Rather than crowding the main app file, we create a separate class called ExceptionHandler inside a new Exceptions directory. This new handler is hooked up to our container, so we can easily grab it where needed.

The plan is that whenever an error comes up, our app will send the error (along with the request and response objects) to this ExceptionHandler. For now, it just passes things along like before, but the structure is there so we'll easily be able to modify the response—like rendering a custom error page—in future episodes.

This sets us up nicely for the next step: actually rendering a custom view when an exception occurs. Stay tuned for that in the next episode!

Episode discussion

No comments, yet. Be the first!