Playing
04. Error reporting with Ignition

Episodes

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

Transcript

00:00
Now, of course, with everything at some point, we're going to get some sort of error while we're building this out.
00:05
So we need a way to show errors in a clean, nice way so we can respond to them and fix stuff up. Let's go ahead and cause an error inside of run and see what happens by default. So I'm just going to go ahead and var dump out something that doesn't exist, a property that doesn't exist within this class. So if we go ahead and just give this a refresh.
00:25
Yeah, sure enough, we get a warning here. We have an undefined property inside of our app class. Now, instead of seeing this default PHP error, we're going to go ahead and pull in ignition, which is a package that displays errors in a really nice way in your application.
00:41
And that's going to really help us going forward. So let's go ahead and pull this in. And if you are very new to PHP, this will give you a lesson on requiring in stuff from Composer as well. So let's go down to the installation section and you can see that we've got this command here.
00:55
Composer require sparse ignition. Now, what this will do is it will go ahead and pull this package down into your application. We've already looked at creating our scaffolded composer.json file when we looked at our auto loading. This will go ahead and register this package and it will add it to our list of packages within composer.json.
01:14
Let's just go ahead and run this and see what this looks like. So we're going to paste this in to the root of our project and that will go ahead and install that package for us. Download any additional packages that that dependency requires. And if we head over to our composer.json file, you can see that this added this require section just here.
01:33
So this package is now required into our project. We also have a composer.lock file, which locks our dependencies down unless we do a composer update. But we can talk about that a little bit later. OK, so inside of vendor, this will now be included inside of here, as well as any of the other dependencies that this package needs.
01:53
And now that we've got this, we can just start to use this much like we could just go ahead and access our app class and new that up and go ahead and run it. OK, so inside of bootstrap and app, we're going to do this at the very top. But after we've required in our auto loader, otherwise this class won't be found. We're also going to change this up later to not use ignition to display errors if we're not in a production environment.
02:19
So locally, while we're developing, of course, we're going to need to show these. But when we push this up to production, we're going to add config to say that we don't want to see these. OK, so let's go and use ignition. So we're going to pull in ignition from Sparky ignition.
02:34
My editor has pulled in the namespace at the top here. But if that's not the case for you, you just want to go ahead and manually add that in. And we're going to call the make method on here and then we're going to say register. That is all we need to do for the very basics of getting ignition to work.
02:50
OK, so now that we've done this, let's head over to our app, give this a refresh. And there we go. So now we've got a much nicer page. Everything has been done for us behind the scenes and we can see the error really clearly.
03:02
We've got a nice stack trace here as well. So we can see exactly where the error occurred. And this is going to be really helpful going forward. So when we do get any errors, we can see exactly where the problem is.
03:13
So, like I said, we're going to move this over later because this now will be registered, even if we push this to production and people are using our app, which is not what we want. What we could also do here as well, just for production, is go ahead and disable errors. So we could say error reporting and we'll set this to zero.
03:31
And at runtime, that will get rid of our default PHP errors. Ignition still works, which we're going to toggle. But even if we got rid of this now, you can see that we just don't get any errors out in here at all. OK, let's go ahead and pull Ignition back and we have a nice error page that we can work with going forward.

Episode summary

In this episode, we tackle error reporting in our PHP project. It's a pretty common thing to bump into errors while building, so it's important to have a neat way to see what went wrong. We start by intentionally causing an error to see what PHP's default error screen looks like — not super helpful or pretty!

To improve this, we introduce the Ignition package, which gives us a much better error page when something goes wrong. It's a popular tool for debugging in Laravel, but works great outside of it too. You'll see how to require the package in using Composer, and get a quick refresher on how packages get added to composer.json and managed with composer.lock.

Once Ignition is installed, we walk through how to register it in our bootstrap process (right after the autoloader). You'll literally see the ugly error page turn into a super clear, useful Ignition page with stack traces and more, making it way easier to find and fix bugs as you build.

There's also a bit of chat about not showing these detailed error pages in production (to keep your app secure), and how you can turn off error reporting completely for live environments. All in all, by the end of the episode, you'll be set up to catch and fix errors much more efficiently as you develop your app.

Episode discussion

No comments, yet. Be the first!