WEBVTT

00:00.620 --> 00:04.310
So let's continue on the topic of error handling.

00:04.340 --> 00:13.010
So previously in this schema load script, we have thrown this exception explicitly outside the main

00:13.010 --> 00:21.380
logic of the script so that we can see how the errors look like in the terminal in the command line

00:21.380 --> 00:22.400
interface.

00:22.520 --> 00:26.270
So obviously this should not stay here.

00:26.750 --> 00:34.100
But what we can do additionally is in our scripts we can just skip wrapping everything inside.

00:34.130 --> 00:35.420
Try catch blocks.

00:35.450 --> 00:41.060
It probably would simplify our life quite a lot.

00:43.670 --> 00:49.280
So now if the exception is thrown, the execution will just stop.

00:49.280 --> 00:56.510
Nothing else will happen, and you're gonna see an error in the terminal wherever you run this command.

00:56.540 --> 00:58.910
But there is another thing.

00:58.910 --> 01:02.090
So let's go back to the error handler.

01:02.690 --> 01:10.940
We've got a note here that we should additionally log the error into a file so we can review that later,

01:10.940 --> 01:16.850
or maybe someone else can review it later to figure out what has happened.

01:18.110 --> 01:24.380
So the first thing to do here in this handle exception method is to log the error.

01:25.160 --> 01:27.800
We should be calling another method for that.

01:28.010 --> 01:34.850
We can call that log error and we are just passing an exception to it.

01:36.260 --> 01:38.210
So this obviously does not exist.

01:38.240 --> 01:40.340
We need to add it.

01:40.490 --> 01:43.130
This method will be private.

01:43.130 --> 01:46.460
So let me add it somewhere on the bottom.

01:48.020 --> 01:54.170
That's private static function log error.

01:54.410 --> 02:04.130
It accepts a throwable type and it's void doesn't return anything.

02:05.240 --> 02:08.120
And I'd like it to be pretty simple.

02:08.120 --> 02:17.480
We should just format the error message sensible way without any colors whatsoever.

02:17.840 --> 02:21.380
Because in the log files you don't have colors.

02:21.530 --> 02:27.590
This is to be consumed by graphical user interfaces that let you present logs.

02:27.590 --> 02:35.390
There is software that does exactly this, or it needs to be consumed manually by someone reading through

02:35.390 --> 02:36.470
the file.

02:37.130 --> 02:43.760
That's why we should just log this as simple as possible.

02:44.180 --> 02:46.220
But we can reuse some of the code.

02:46.250 --> 02:46.730
Maybe.

02:46.730 --> 02:49.820
Let's begin with this message.

02:50.960 --> 02:55.100
So the first thing is to generate the log message.

02:56.300 --> 02:58.940
And as I've said we don't need colors.

02:58.940 --> 03:06.260
That's why we need to strip this part that turns everything into red.

03:07.190 --> 03:10.040
And that part that resets the color.

03:10.520 --> 03:13.190
So just the message is enough.

03:13.850 --> 03:19.370
So we can use an error log function to store this error.

03:20.450 --> 03:25.220
The first argument is the message we've got the log message variable.

03:26.120 --> 03:28.970
The next one is the message type.

03:29.180 --> 03:31.640
This says where the error should go.

03:31.640 --> 03:37.730
And in the PHP documentation page you can see what are the possible values for it.

03:37.730 --> 03:43.760
I'm just going to say it right away that if you type free, this means please store it in a specific

03:43.760 --> 03:46.730
destination, which is a file.

03:47.180 --> 03:50.060
Now in our case we need to point to this file.

03:50.060 --> 03:54.440
So I'm using the current directory which is inside the core directory.

03:54.470 --> 03:57.200
And I need to move up.

03:58.460 --> 04:03.830
So I'm going one directory up to the logs directory.

04:04.160 --> 04:05.780
Error log.

04:06.050 --> 04:09.620
Our job would be to create this file.

04:09.620 --> 04:13.040
Otherwise we might get an error.

04:13.160 --> 04:15.020
and you add a semicolon.

04:15.020 --> 04:22.880
And at this point I need to create this logs directory.

04:25.220 --> 04:31.340
And inside it I am going to create an empty error log file.

04:31.940 --> 04:41.060
Next time anything goes wrong in our app, whether that's just a notice or an error, we should get

04:41.060 --> 04:43.580
an entry inside this file.

04:43.730 --> 04:51.020
This would be very useful to later review it and see what are the potential problems in our app.

04:51.050 --> 04:56.270
Even if they are not straight on errors, maybe there are notices.

04:56.300 --> 04:59.720
That's something you should always try to fix.

05:00.920 --> 05:07.970
Maybe I'm just going to go back to our command and throw this exception again.

05:08.720 --> 05:16.850
This would be throw new exception saying this should go into the logs.

05:16.880 --> 05:17.540
All right.

05:17.570 --> 05:21.440
At this point, let's run this command.

05:23.990 --> 05:27.050
So again, we just throw an exception here.

05:28.100 --> 05:29.120
Nothing new.

05:29.120 --> 05:31.910
But now let's take a look at the log file.

05:31.910 --> 05:33.800
And this is how it looks like.

05:33.830 --> 05:41.180
All the new errors would be appended to this file with the date, with the time, and with the type

05:41.180 --> 05:45.170
of exception that has happened message and all the other info.

05:45.200 --> 05:52.610
This is super useful to review what has happened in your app, and what were the errors that maybe happened

05:52.610 --> 05:53.480
previously.

05:53.510 --> 06:00.860
Sometimes it just happens that you just figure out the recent issue in your application, and you just

06:00.860 --> 06:08.420
need to go back to see when it started, or maybe what change has caused this issue.

06:08.450 --> 06:12.770
So log files are super useful.

06:14.330 --> 06:19.310
Okay, now I think we should clean up and remove this error.
