WEBVTT 00:00:06.960 --> 00:00:15.180 Now we are going to move to another example and this time rather than just showing the error on the 00:00:15.180 --> 00:00:23.450 screen we are going to send an error log to a file or by e-mail if you want to. 00:00:23.750 --> 00:00:24.740 OK. 00:00:25.970 --> 00:00:33.960 So so we're going to create a function which is going to trigger an error but this time we are going 00:00:33.960 --> 00:00:36.220 to trigger the error ourselves. 00:00:36.240 --> 00:00:43.360 Normally when there is a runtime error in place the BHB engine is going to trigger an error by itself 00:00:43.650 --> 00:00:52.020 but we can also do that ourselves if we want to make our application more robust we can just trigger 00:00:52.620 --> 00:00:55.790 errors when we think there are some problems. 00:00:55.990 --> 00:00:56.660 OK. 00:00:57.000 --> 00:01:04.700 So our function is just going to calculate the file size of a file. 00:01:04.900 --> 00:01:16.410 So it's going to be let's call it calculates file size and we're going to give it's one arguments which 00:01:16.420 --> 00:01:20.680 is going to be the name or the path of the file. 00:01:20.700 --> 00:01:21.350 OK. 00:01:21.990 --> 00:01:28.730 So first we are going to check if the file exists or if the file doesn't exist. 00:01:28.850 --> 00:01:34.880 So the 70s commission Mark we're going to use the function file exists. 00:01:35.560 --> 00:01:36.890 OK. 00:01:37.440 --> 00:01:42.330 And we're going to give it the name or the path of the file. 00:01:42.660 --> 00:01:47.280 So if it exists if it doesn't exist then all we going to do. 00:01:47.850 --> 00:01:49.850 We are going to trigger an error. 00:01:50.170 --> 00:01:50.720 OK. 00:01:51.090 --> 00:01:53.490 So this time we trigger triggering the error ourselves. 00:01:53.610 --> 00:01:53.840 OK. 00:01:53.850 --> 00:01:59.040 So to trigger an arrow going to use the function trigger error. 00:01:59.590 --> 00:02:04.710 And if you want to explore details of all these functions control k. 00:02:07.830 --> 00:02:12.780 So it's going to take a couple of promises to the first one is going to be the error message and the 00:02:12.780 --> 00:02:14.560 second one is going to be the error. 00:02:14.870 --> 00:02:19.840 So for the error type we can use the user family of Constance. 00:02:19.990 --> 00:02:23.240 OK so by default it's going to be a user notice. 00:02:23.460 --> 00:02:26.570 Otherwise we can use other ones like a user wanting for example. 00:02:26.690 --> 00:02:27.450 OK. 00:02:27.750 --> 00:02:32.140 So let's give our function a couple of promises. 00:02:32.190 --> 00:02:43.470 So the first one is going to be always trying and let's say that to the file name followed by something 00:02:43.470 --> 00:02:46.490 like does not exist. 00:02:46.840 --> 00:02:47.640 And those 00:02:53.900 --> 00:02:58.060 sites can not be retrieved for example. 00:02:59.010 --> 00:03:00.350 OK. 00:03:00.420 --> 00:03:11.910 And a second prompt So we're going to give it's an easy user warning for example. 00:03:11.910 --> 00:03:13.520 All right. 00:03:13.520 --> 00:03:17.260 And then we're going to return false. 00:03:18.840 --> 00:03:19.210 OK. 00:03:19.260 --> 00:03:31.620 Otherwise if the file exists we are just going to return the size of the file using the file size function 00:03:31.840 --> 00:03:34.050 . 00:03:36.160 --> 00:03:36.720 Ok 00:03:41.140 --> 00:03:47.370 ok so that's our function which is going to trigger the error. 00:03:47.900 --> 00:03:53.490 Ok so now we are ready to create our error handler. 00:03:53.500 --> 00:04:03.870 OK so this time is going to be a function I'm going to call it's error handler too and you're going 00:04:03.870 --> 00:04:08.140 to give it all those promises as we did before. 00:04:09.050 --> 00:04:09.500 OK. 00:04:09.660 --> 00:04:12.030 Only the first two are required anyway. 00:04:12.770 --> 00:04:13.480 OK. 00:04:13.560 --> 00:04:18.420 And this time what were you going to do. 00:04:18.450 --> 00:04:24.040 We are going to create a vat a string variable. 00:04:24.450 --> 00:04:26.600 Ok log. 00:04:27.160 --> 00:04:30.830 Where are we going to store everything. 00:04:30.930 --> 00:04:40.190 We want about this era and then we'll see how we can log this into a log file or send the error by email 00:04:40.200 --> 00:04:41.010 if you want to. 00:04:41.250 --> 00:04:41.970 OK. 00:04:42.300 --> 00:04:50.490 So let's set our variable to something like So. 00:04:50.800 --> 00:04:53.640 So let's let's get the error number OK. 00:04:53.670 --> 00:04:55.590 And also the dates the error happened. 00:04:55.580 --> 00:04:58.190 So that's right error. 00:04:58.700 --> 00:04:59.390 OK. 00:04:59.400 --> 00:05:04.250 And then brackets that inside does get the error number. 00:05:05.320 --> 00:05:18.280 OK let's get the dates on then it's going to get the dates using the date function. 00:05:19.270 --> 00:05:31.580 And let's go for a format's day for slushed month for was last year and then the hour minutes and seconds 00:05:31.620 --> 00:05:32.890 . 00:05:33.980 --> 00:05:36.590 OK. 00:05:37.610 --> 00:05:42.140 Then let's have a return. 00:05:44.330 --> 00:05:48.840 So backslash backslides. 00:05:49.910 --> 00:05:50.820 OK. 00:05:51.290 --> 00:05:55.580 That's going to be the first line and then lays out another another line. 00:05:55.580 --> 00:05:56.120 So 00:06:01.400 --> 00:06:02.120 for example 00:06:05.930 --> 00:06:06.530 you can say 00:06:12.170 --> 00:06:14.180 this details. 00:06:14.440 --> 00:06:15.190 OK. 00:06:15.360 --> 00:06:17.270 I he's going to write our Arab spring here 00:06:20.190 --> 00:06:21.960 or the error message. 00:06:21.990 --> 00:06:23.960 All right. 00:06:24.200 --> 00:06:27.430 Followed by a thought. 00:06:28.500 --> 00:06:33.840 Let's have a line return your line. 00:06:36.310 --> 00:06:36.920 OK. 00:06:36.980 --> 00:06:41.660 Also we might want thee to see the location. 00:06:41.780 --> 00:06:48.190 So we're going to have the file name and also the the line where the error happened. 00:06:48.350 --> 00:06:51.380 OK so let's dance as well 00:06:56.750 --> 00:07:08.600 as Roy's location in its location going on in and that's going to be the error file. 00:07:08.980 --> 00:07:09.800 OK. 00:07:10.430 --> 00:07:16.690 Now we can say on line and that's going to be the light error light. 00:07:17.960 --> 00:07:21.350 All right. 00:07:21.340 --> 00:07:21.600 OK. 00:07:21.680 --> 00:07:26.730 And finally let's have eternal life. 00:07:28.070 --> 00:07:37.350 All right so now we might we might want to add the variable as well with the context. 00:07:37.400 --> 00:07:39.040 Also it's really a huge file. 00:07:39.050 --> 00:07:46.250 They are the huge string this Eric contex but let's try this to see what is called Insight. 00:07:46.520 --> 00:07:53.070 So let's add example as strings saying variables. 00:07:54.110 --> 00:07:56.200 OK. 00:07:56.210 --> 00:07:58.310 So this is going to be an array. 00:07:58.370 --> 00:07:59.540 So let's use the 00:08:02.240 --> 00:08:10.390 function Brenes and a score are OK to print it. 00:08:13.610 --> 00:08:20.240 OK so just one thing when we use the function preens underscore our is going to print stuff on the screen 00:08:20.310 --> 00:08:22.430 but we don't want to do that at the moment. 00:08:22.430 --> 00:08:27.950 We just we just want to capture the information or the outputs of the prints. 00:08:27.980 --> 00:08:29.920 At its core our function. 00:08:29.930 --> 00:08:33.590 So we're going to do with we're going to add a second barometer to this function. 00:08:34.030 --> 00:08:34.940 OK. 00:08:35.030 --> 00:08:35.980 True. 00:08:36.280 --> 00:08:44.510 And this means that we are just going to capture this output which is going to be a drink and it's to 00:08:44.680 --> 00:08:45.510 this string. 00:08:45.610 --> 00:08:46.280 OK. 00:08:46.400 --> 00:08:49.440 So that's really important locaters just read my mind. 00:08:49.550 --> 00:08:50.450 So. 00:08:50.700 --> 00:08:54.440 Well then after that we can add a 00:08:57.080 --> 00:08:58.570 return. 00:08:59.030 --> 00:09:04.590 OK so now we've got our log string ready. 00:09:04.610 --> 00:09:05.110 OK. 00:09:05.330 --> 00:09:16.910 So to lock our string to a file We're going to use a function that we call error lock. 00:09:17.570 --> 00:09:27.720 OK so this function is going to take a few parameters so if you do control k you can see here that is 00:09:27.760 --> 00:09:32.780 going to take as a first promiser a string which is going to be our message. 00:09:33.050 --> 00:09:37.190 And then the second one is going to be the message type and you got different message types. 00:09:37.190 --> 00:09:44.630 So for example if you want to look the details onto a file on a log file We're going to use number three 00:09:45.080 --> 00:09:47.960 if you want to send an email going to use number one for example. 00:09:48.260 --> 00:09:50.850 OK. 00:09:51.300 --> 00:09:55.780 And the other part I would say it's going to be the destination. 00:09:56.000 --> 00:10:02.510 And then any extra headers we need and this nation is going to be the boss of the log file or the email 00:10:02.510 --> 00:10:05.920 address if we're sending an email. 00:10:06.050 --> 00:10:09.210 So let's let's look this to a file. 00:10:09.650 --> 00:10:10.730 OK. 00:10:10.730 --> 00:10:16.740 So what I'm going to do I'm going to create a file using FCP. 00:10:16.880 --> 00:10:18.320 Let's create a folder 00:10:22.070 --> 00:10:30.830 called it's looks and let's create our file inside this folder 00:10:36.060 --> 00:10:44.310 it's going to call it's error handling errors. 00:10:45.090 --> 00:10:47.130 We're going to give us an extension dot log. 00:10:47.340 --> 00:10:50.040 OK so it is going to be a log file error handling 00:10:53.910 --> 00:10:54.440 errors. 00:10:54.450 --> 00:10:55.100 But look. 00:10:55.230 --> 00:10:55.840 OK 00:11:02.640 --> 00:11:06.310 so now let's give our error log function. 00:11:06.660 --> 00:11:17.490 All the needed arguments so first one is going to be our string log and then the message type is 3. 00:11:18.810 --> 00:11:24.610 Ok 4 for logging into a file and then the path of our file is is logs. 00:11:24.740 --> 00:11:28.540 OK forward slash error handling 00:11:31.080 --> 00:11:38.170 errors doats lock OK and also. 00:11:38.340 --> 00:11:44.820 So we want to send the lock to this file and also we want to send an e-mail as well. 00:11:44.820 --> 00:11:45.870 So let's try that as well. 00:11:46.020 --> 00:11:57.090 So we're going to use the same function at this time we are going to use message type 1 for email and 00:11:57.090 --> 00:11:59.040 then we're going to write the e-mail address. 00:11:59.040 --> 00:12:00.810 We'd like to send the error to. 00:12:01.080 --> 00:12:05.400 OK. 00:12:05.400 --> 00:12:10.770 I'm going to send it to us. 00:12:15.790 --> 00:12:15.940 OK 00:12:15.960 --> 00:12:23.850 . 00:12:31.560 --> 00:12:38.550 OK so finally we're going to stop our script using the bi function and we're just going to write something 00:12:38.550 --> 00:12:47.970 on the screen saying this paragraph for example is going to say that's an error. 00:12:47.980 --> 00:12:51.930 Could you please try again. 00:12:53.820 --> 00:12:54.800 OK. 00:12:54.950 --> 00:13:01.650 We're going to have already sent all the information to the file or the e-mail address. 00:13:02.400 --> 00:13:12.520 Ok so after we have finished our function we are going to use the set error handler handler file. 00:13:12.680 --> 00:13:14.880 Ok to set our handler 00:13:17.920 --> 00:13:19.360 sets a function. 00:13:19.370 --> 00:13:21.690 Two sets are handler so 00:13:25.210 --> 00:13:30.220 name handler error handler to be inside the string. 00:13:30.570 --> 00:13:32.100 Ok let's 00:13:36.420 --> 00:13:40.680 let's generate an error by calling our function calculates 00:13:44.730 --> 00:13:47.220 the file size 00:13:49.830 --> 00:13:51.550 of an existing file. 00:13:51.550 --> 00:14:01.240 So it's it's an existing file that's text. 00:14:04.250 --> 00:14:07.550 So let's refresh the page. 00:14:09.800 --> 00:14:12.210 OK so missing is very common. 00:14:12.830 --> 00:14:15.040 So is refreshing. 00:14:15.570 --> 00:14:17.480 OK so saying here an error occurred. 00:14:17.520 --> 00:14:22.530 Please try again please try again. 00:14:23.400 --> 00:14:26.960 OK so now let's check our log file. 00:14:27.020 --> 00:14:36.160 Go to the log file and check if we of the error in it's so control. 00:14:36.600 --> 00:14:39.310 When it opened using tax notepad. 00:14:39.330 --> 00:14:44.000 OK so now we can see our log string. 00:14:44.220 --> 00:14:46.000 Ok that we send to this file. 00:14:46.350 --> 00:14:46.690 Ok 00:14:50.190 --> 00:14:51.660 I can see all the details. 00:14:51.680 --> 00:14:54.590 So what caused the error a number there. 00:14:54.600 --> 00:14:55.220 OK. 00:14:55.350 --> 00:15:00.960 The date time OK the message. 00:15:01.250 --> 00:15:08.480 OK the location of our file the line where it's happened and so on. 00:15:08.810 --> 00:15:10.500 OK. 00:15:10.560 --> 00:15:17.760 And you can see the context array as well. 00:15:18.030 --> 00:15:23.800 So we've got only one variable here the file with the file name. 00:15:24.060 --> 00:15:25.970 OK. 00:15:27.020 --> 00:15:28.320 Let's check the e-mail as well 00:15:28.330 --> 00:15:38.120 .