WEBVTT

00:00.590 --> 00:08.750
Okay, so I think that we can just finish the whole problem of error handling in this video.

00:09.140 --> 00:14.060
We are missing just one piece, which is rendering an error page.

00:14.060 --> 00:19.700
If something really bad happens and we are running in the context of a web server.

00:19.730 --> 00:22.460
Why won't we do it right now?

00:22.490 --> 00:27.530
This means we need to add this render error page method.

00:28.550 --> 00:34.130
And what I'm going to do is I will copy the render CLI error.

00:34.160 --> 00:38.000
The reason is this will be very similar.

00:38.030 --> 00:41.690
We just have some slight differences.

00:42.800 --> 00:45.980
So the name should be Render error page.

00:46.010 --> 00:49.430
Let me change that right here.

00:49.730 --> 00:54.830
So we also need to know if we are in the debug mode.

00:55.070 --> 01:02.540
This would be very useful on the web because this is where other people are seeing our project and our

01:02.540 --> 01:03.530
website.

01:03.530 --> 01:10.660
So it's crucial not to reveal some insights that should not really be shown to anyone.

01:11.170 --> 01:16.660
And in here we are going to simplify the error message a bit.

01:16.690 --> 01:18.970
Also removing all the colors.

01:19.000 --> 01:21.670
This doesn't make sense on the web.

01:22.870 --> 01:25.480
This would be our error message.

01:26.140 --> 01:34.390
And also in case of debug mode being disabled we don't need any colors.

01:34.720 --> 01:42.430
Okay, so essentially I think the top of this method can be exactly like this.

01:42.700 --> 01:49.570
The things that are different is how do we present this message, this error message.

01:49.570 --> 01:53.470
So the error codes don't make any sense on the web.

01:53.470 --> 01:56.530
This is only for command line programs.

01:56.530 --> 02:01.360
And now obviously this block needs to be removed as well.

02:01.390 --> 02:06.400
For now let's just simply return a response code.

02:06.430 --> 02:13.120
Unhandled Exception means that something terribly bad has happened.

02:13.150 --> 02:17.110
Otherwise we would have handled an expected error.

02:17.140 --> 02:21.040
That's why we return the 500 code.

02:21.430 --> 02:24.670
Then we're just going to echo the response.

02:24.670 --> 02:29.020
And we're going to use the view class render method.

02:29.020 --> 02:34.990
And we are going to render a template that we're going to prepare.

02:35.020 --> 02:37.720
Let's call it just 500.

02:39.640 --> 02:41.410
Does it have any data.

02:41.410 --> 02:44.590
Well yes we do have some data.

02:45.340 --> 02:54.490
First the error message this is our error message then the trace.

02:56.410 --> 03:02.710
And finally whether we are in the debug mode or not.

03:06.220 --> 03:07.120
Okay.

03:07.120 --> 03:09.160
We need a semicolon in here.

03:09.160 --> 03:15.750
And Optionally, we can supply a layout.

03:16.020 --> 03:17.430
Maybe we can do that.

03:18.570 --> 03:21.150
This would be layouts main.

03:23.370 --> 03:26.670
So now we need to add a view.

03:26.700 --> 03:32.670
So in this views folder in the app directory let's make a new one.

03:32.670 --> 03:36.330
So I think we've called that errors.

03:38.370 --> 03:45.090
And in here we are going to add a view for every possible type of error.

03:45.090 --> 03:49.590
For now we are only handling the 500.

03:49.620 --> 03:52.170
Let it be just this for now.

03:53.640 --> 04:02.760
So let me add the header saying oops, something went wrong.

04:04.380 --> 04:08.850
Then let's add a paragraph outputting the error message.

04:08.880 --> 04:12.750
Escaping it using HTML special chars.

04:15.210 --> 04:18.000
So here we have access to a variable.

04:18.000 --> 04:20.640
So remember how templates work.

04:20.670 --> 04:26.760
Everything we are passing through the data array is becoming a variable in the template.

04:27.840 --> 04:35.790
And next up we can check for the debug mode using is debug and alternative template syntax.

04:35.790 --> 04:42.390
So this is an H2 element where we are going to display the stack trace.

04:43.350 --> 04:53.010
Then use the pre element to have some basic formatting and also escape stuff using HTML special chars.

04:53.820 --> 04:58.020
The trace is a string so we can directly output it.

05:00.390 --> 05:04.500
And the if statement needs to be closed with an endif.

05:04.500 --> 05:09.420
Now finally, let's give our users a chance to recover.

05:09.420 --> 05:17.520
So to go someplace where things should potentially work, we would not like to leave the users hanging

05:17.520 --> 05:20.600
without knowing what they should do next.

05:21.110 --> 05:29.330
That's why I think we should give them a link, maybe to the main page, and just say that they should

05:29.330 --> 05:33.320
try and return to the home page.

05:34.910 --> 05:35.390
All right.

05:35.390 --> 05:39.380
I think we don't need to do anything else.

05:40.730 --> 05:48.320
And now we should just make sure that this works.

05:48.320 --> 05:52.310
I think we should jump to the home page.

05:52.310 --> 05:58.400
And again, inside the index action, we should just throw a new exception.

05:58.400 --> 06:03.560
So we are triggering those errors so we can see if the error handling works.

06:03.650 --> 06:08.600
So this has happened on the web.

06:09.050 --> 06:16.670
Now that's important as we need to check if our error handler properly displays a page.

06:16.670 --> 06:25.510
If the error happens on the web and that it reports this error to the error log in all cases.

06:25.810 --> 06:28.960
Now we should start a server.

06:29.350 --> 06:35.200
So this is php s localhost.

06:35.380 --> 06:39.100
Let me use port 8000 because I know it's not taken.

06:39.100 --> 06:42.280
And let's start it in the public directory.

06:42.580 --> 06:46.120
And in a second we're going to see how does it look like.

06:46.720 --> 06:51.550
So it seems that the error page is rendered almost properly.

06:51.550 --> 06:57.550
The exception is that we seem to have wrong format for the message.

06:57.580 --> 07:03.250
Not all the info is displayed, but you can see the title, the page and the stacktrace.

07:03.940 --> 07:06.880
So everything started with router dispatch.

07:06.880 --> 07:13.960
Then we had call action with all the parameters and the error has happened inside home controller index.

07:13.960 --> 07:16.810
So that is a stack trace.

07:16.810 --> 07:18.760
That's super useful info.

07:18.790 --> 07:24.760
If you need to debug the problems and see what was the flow of the program.

07:24.760 --> 07:28.120
So now let's take a look at the problem.

07:28.120 --> 07:34.480
So I think that we might be missing one of the.

07:34.510 --> 07:35.290
Yes.

07:35.890 --> 07:38.500
So we should start with the date.

07:38.500 --> 07:44.020
And I think that we are just missing one of those placeholders.

07:44.020 --> 07:46.300
We should have five, not four.

07:47.020 --> 07:57.460
And after this small change, when I refresh I should see the date and where this happened and on which

07:57.460 --> 07:59.890
line, which is exactly what has happened.

07:59.920 --> 08:02.590
Now let's also take a look at the error log.

08:02.590 --> 08:05.500
And obviously we've got everything logged.

08:05.500 --> 08:07.630
So this worked perfectly.

08:07.630 --> 08:16.270
So now that we have most of our framework built and the generic code is almost done, we might have

08:16.270 --> 08:21.220
to do some tiny changes or add something here or there.

08:21.250 --> 08:25.420
But I think we should now focus on doing the fun stuff.

08:25.420 --> 08:29.710
Building our blog application with a dashboard.
