WEBVTT 00:00:07.530 --> 00:00:11.960 In this video we're going to learn how to handle errors using BHB. 00:00:11.970 --> 00:00:18.120 So first of all let's write a simple BHP statement that we know is going to trigger an error. 00:00:18.180 --> 00:00:24.570 So for example let's echo the file size of a file that we know doesn't exist. 00:00:24.740 --> 00:00:25.330 OK. 00:00:25.350 --> 00:00:31.210 So for example a file called an existing file. 00:00:31.590 --> 00:00:32.900 Those texts. 00:00:32.960 --> 00:00:35.160 OK so we know the file doesn't exist. 00:00:35.160 --> 00:00:39.110 So we know that if we refresh our page we're going to get an error. 00:00:39.650 --> 00:00:40.600 OK. 00:00:41.580 --> 00:00:48.560 So this is not a syntax error but this is what we call a runtime error which has been generated by the 00:00:48.630 --> 00:00:51.190 BHB engine. 00:00:51.300 --> 00:00:57.190 So in this first example we are going to see how to customize the way we showed this error to our user 00:00:57.190 --> 00:00:57.250 . 00:00:57.240 --> 00:01:01.500 So we want to show the error in a more user friendly way. 00:01:01.500 --> 00:01:02.840 OK. 00:01:03.840 --> 00:01:08.620 So we're going to do we going to create something we call an error handler. 00:01:08.730 --> 00:01:17.520 It's going to be a function OK which is going to process some promises and he's going to show the error 00:01:17.820 --> 00:01:19.010 in the way we like. 00:01:19.190 --> 00:01:19.590 OK. 00:01:19.710 --> 00:01:22.220 So this creates this function. 00:01:22.830 --> 00:01:28.290 So this function is we need to create it before we run the statements which is going to give us an error 00:01:28.300 --> 00:01:28.380 . 00:01:28.470 --> 00:01:29.120 OK. 00:01:29.340 --> 00:01:37.310 So let's call this function for example error handler. 00:01:37.420 --> 00:01:42.950 One they've been going to create another one in the second example. 00:01:43.080 --> 00:01:53.300 So this function function is going to take a few parameters and then it's going to run some code. 00:01:53.340 --> 00:02:01.010 So the barometers we're going to give to this function are going to be hours following. 00:02:01.010 --> 00:02:08.650 So the first Parminter which is a required Parmitano we're going to call it error number. 00:02:08.910 --> 00:02:13.760 OK you can call it anything you want but the first bar is a, is going to be referring to the error number 00:02:14.340 --> 00:02:20.670 and the error number in BHB refers to the error level without many error levels. 00:02:20.960 --> 00:02:22.070 OK. 00:02:22.530 --> 00:02:29.760 As an example error level 1 means that we are dealing with a fatal error and our great script is going 00:02:29.760 --> 00:02:31.060 to stop immediately. 00:02:31.080 --> 00:02:31.770 OK. 00:02:31.840 --> 00:02:34.660 At this stage we get the error. 00:02:35.450 --> 00:02:40.940 Ok there are other error numbers. 00:02:41.100 --> 00:02:45.590 Level 2 for example is a runtime warning. 00:02:45.660 --> 00:02:55.080 OK so is this type of error is not fatal and basically the the execution of our scripts is not going 00:02:55.080 --> 00:02:55.730 to be stopped. 00:02:55.770 --> 00:02:56.270 OK. 00:02:56.430 --> 00:03:01.820 And this is an example of an era of level 2 or warning. 00:03:01.950 --> 00:03:02.950 OK. 00:03:03.390 --> 00:03:09.310 So that's going to be the first parameter error them. 00:03:09.660 --> 00:03:14.290 The second Prometa is going to be the Arab Spring. 00:03:15.210 --> 00:03:16.020 We're going to call it. 00:03:16.110 --> 00:03:19.810 Error. 00:03:21.520 --> 00:03:22.260 OK. 00:03:22.440 --> 00:03:26.730 We can call it you can call it anything you want but it's going to be the second barometer. 00:03:27.680 --> 00:03:32.430 And the Arab Spring is just the error message that we get. 00:03:32.790 --> 00:03:35.160 So in our case this is our Arab Spring. 00:03:35.650 --> 00:03:37.150 OK. 00:03:38.250 --> 00:03:43.740 OK so these are the the required permits for any error handler. 00:03:43.740 --> 00:03:45.740 There are other other barometers we can use. 00:03:45.750 --> 00:03:46.240 OK. 00:03:46.290 --> 00:03:48.060 They are not required. 00:03:48.060 --> 00:03:49.980 Nobody's going to add them for now. 00:03:49.980 --> 00:03:52.020 So we are going to have 00:03:56.490 --> 00:04:03.110 the Arab file which is just the place we found as we are that is running our scripts. 00:04:03.180 --> 00:04:03.660 OK. 00:04:03.810 --> 00:04:12.080 So in our case it's our file that came with the complete part of the file and we can see it here. 00:04:12.900 --> 00:04:13.610 OK. 00:04:14.100 --> 00:04:20.350 And then the next parameter is going to be the airline. 00:04:22.020 --> 00:04:25.920 So the airline is the line where the error happened. 00:04:26.460 --> 00:04:29.730 And in our case it's happened in 9:34. 00:04:29.960 --> 00:04:30.570 OK. 00:04:30.900 --> 00:04:37.470 And finally we've got a variable or parameter error context. 00:04:39.020 --> 00:04:48.090 OK so Eric contex is just an array which will tell us all the variables that we had at the time our 00:04:48.150 --> 00:04:49.270 error happened. 00:04:49.560 --> 00:04:49.980 OK. 00:04:50.110 --> 00:04:52.350 We'll see in the bits where you can find them. 00:04:52.370 --> 00:04:58.650 Basically in the context you can find anything including the post variable the Gets variable the cookie 00:04:58.650 --> 00:04:59.820 variable and so on. 00:04:59.950 --> 00:05:04.830 OK OK so now what we want to do. 00:05:04.860 --> 00:05:11.790 We would like to echo our error in a nice readable way. 00:05:12.030 --> 00:05:17.330 So we're just going to echo a paragraph Okay. 00:05:17.370 --> 00:05:18.720 And inside our paragraph 00:05:21.760 --> 00:05:30.770 let's rights era and then after that let's write the error number between brackets. 00:05:30.870 --> 00:05:32.110 OK. 00:05:33.440 --> 00:05:46.920 So it is going to be this terrible error number and then let's have the Arab spring rise out of us which 00:05:46.920 --> 00:05:48.630 is this problem. 00:05:49.330 --> 00:05:50.190 OK. 00:05:50.280 --> 00:05:51.780 Then. 00:05:52.230 --> 00:05:56.370 So that's how we want to show our error on the screen. 00:05:57.140 --> 00:06:02.110 OK so now AAVSO we have defined our error handler. 00:06:02.160 --> 00:06:06.860 Now we need to use another function OK to set our error handler. 00:06:07.050 --> 00:06:11.150 This function is going to be set error handler. 00:06:11.350 --> 00:06:15.120 OK so this function is just going to set the error handler for us 00:06:18.080 --> 00:06:21.240 . 00:06:21.570 --> 00:06:24.910 OK so it's going to take as a parameter a string. 00:06:25.630 --> 00:06:29.960 Well we're going to place the name of our error handler. 00:06:30.510 --> 00:06:41.340 So our error handler is going to handle all the errors which are triggered after the setting of the 00:06:41.340 --> 00:06:42.180 error handler. 00:06:42.260 --> 00:06:42.660 OK. 00:06:42.900 --> 00:06:49.050 So if we save and refresh now we can see how our error is displayed this time. 00:06:49.240 --> 00:06:53.650 OK so we can see our paragraph echoed code. 00:06:53.820 --> 00:06:57.470 So you've got the error number. 00:06:57.700 --> 00:06:59.750 And of course our errors drink. 00:07:00.030 --> 00:07:03.910 OK let's just make a quick change inside our program. 00:07:03.960 --> 00:07:14.440 So let's make the area look bold by putting it inside the strong stacks. 00:07:16.140 --> 00:07:18.460 OK now 00:07:21.450 --> 00:07:22.690 that looks better. 00:07:23.000 --> 00:07:23.430 OK. 00:07:23.640 --> 00:07:27.010 So that's how we can handle errors using BHB