1
00:00:05,410 --> 00:00:10,300
Welcome back and this lecture we're going to take a quick little detour to talk about how you can create

2
00:00:10,300 --> 00:00:12,300
your own error handling pages.

3
00:00:12,460 --> 00:00:17,800
That is if you have a four or four error instead of showing that default not found four or four page

4
00:00:17,800 --> 00:00:24,490
that we saw earlier in the section we can show our own four or four HTL template so to set up the handlers

5
00:00:24,490 --> 00:00:28,680
for that in the error pages directory and then link those using a blueprint.

6
00:00:28,930 --> 00:00:32,260
Let's head back to our text editor and show you how it's done.

7
00:00:32,350 --> 00:00:32,680
All right.

8
00:00:32,680 --> 00:00:39,370
Here we are back at our text editor what I want to do is run our application as it is right now and

9
00:00:39,370 --> 00:00:43,570
see what happens when we try to reach a page that isn't available to us.

10
00:00:43,630 --> 00:00:48,300
So if we take a look here at our Web site we have the full navigation bar.

11
00:00:48,460 --> 00:00:54,730
And if I go to forward slash info I see info about our company but let's go to forward slash information

12
00:00:55,200 --> 00:00:57,120
of you that we know doesn't exist.

13
00:00:57,130 --> 00:01:03,110
So if I hit enter I right now get this default for 0 for error saying not found the requests are always

14
00:01:03,130 --> 00:01:07,680
not found that our server where we're always to do this play our own template file.

15
00:01:07,690 --> 00:01:12,370
So you've definitely worked for other Web sites that show their own customized four or four page.

16
00:01:12,370 --> 00:01:17,500
We're now going to show you how you can do that with flask in order to make that happen.

17
00:01:17,520 --> 00:01:20,990
I'm going to stop serving this and come back.

18
00:01:21,000 --> 00:01:27,210
Remember we have a directory called Air underscore pages underneath that directory by convention.

19
00:01:27,210 --> 00:01:29,080
We call it handlers dot pi.

20
00:01:29,100 --> 00:01:33,080
So we're going to create a new file which is essentially a view up high file.

21
00:01:33,090 --> 00:01:34,680
But for the error handling.

22
00:01:34,890 --> 00:01:41,670
So in order to distinguish it from just typical views we're going to call it handlers dot pi.

23
00:01:41,670 --> 00:01:47,300
So again this file that I'm at right now is called handlers dot pi.

24
00:01:47,640 --> 00:01:52,920
And what this is going to do is it's actually going to serve the views of those particular error pages

25
00:01:53,670 --> 00:02:02,830
sort of say from flask import blueprints and also render template and then we'll say error underscore

26
00:02:02,830 --> 00:02:06,300
pages is equal to a blueprint.

27
00:02:06,670 --> 00:02:15,020
And then we're going to call this error pages and then passen the builtin name variable.

28
00:02:15,020 --> 00:02:20,520
Now we're going to do is say at error pages.

29
00:02:20,560 --> 00:02:30,690
So instead of saying dot route we're going to call dot app underscore error handler because this isn't

30
00:02:30,690 --> 00:02:35,970
just a basic routing since we don't know what the user is going to provide for it for or for air.

31
00:02:35,990 --> 00:02:41,900
Instead we're using the app underscore air handler to connect it to a general air and then we pass in

32
00:02:41,970 --> 00:02:44,070
the error code that we want to connect it to.

33
00:02:44,070 --> 00:02:49,250
So in this case we're going to connect to a 4 0 for air and you can google around for the various h

34
00:02:49,260 --> 00:02:55,760
T.M. codes so forfour is just one type of air which is kind of a not found error.

35
00:02:56,010 --> 00:03:04,930
And then we'll see if error underscore for 0 4 and then we pass in the error that was actually found.

36
00:03:05,190 --> 00:03:09,990
So this is the view that's going to happen when you get a page that isn't found.

37
00:03:09,990 --> 00:03:16,860
And we're going to return render a template and we're actually going to set this not underneath the

38
00:03:16,950 --> 00:03:18,450
regular templates folder.

39
00:03:18,450 --> 00:03:24,100
Instead we're going to have the error templates set apart in their own error pages directory.

40
00:03:24,360 --> 00:03:34,050
So the template is going to come from error underscore pages forward slash 4 0 4 h m l and then I'm

41
00:03:34,050 --> 00:03:36,760
going to say comma for 0 4.

42
00:03:37,020 --> 00:03:38,620
So notice what's happening here.

43
00:03:38,660 --> 00:03:44,550
I'm not just returning this render template function I'm actually returning a tuple where the first

44
00:03:44,550 --> 00:03:49,090
part of it is the rendered template and then I say comma for 0 4.

45
00:03:49,140 --> 00:03:53,330
So keep that in mind we haven't actually done something like this before because we haven't use that

46
00:03:53,400 --> 00:03:53,690
app.

47
00:03:53,730 --> 00:03:59,550
Underscore error handler before but this is the main approach for setting up your own templates to be

48
00:03:59,550 --> 00:04:02,510
served for a particular errors happen.

49
00:04:02,580 --> 00:04:08,640
So now we need to do is actually set up this error underscore pages directory within our original templates

50
00:04:08,640 --> 00:04:09,640
directory.

51
00:04:09,660 --> 00:04:12,550
So if I expand templates right now I see Base 8 team.

52
00:04:12,660 --> 00:04:14,590
In this index page to melt info.

53
00:04:14,640 --> 00:04:23,480
Tim L. I'm going to create a new folder here called error underscore pages and this will allow me to

54
00:04:23,480 --> 00:04:28,330
separate my specific error templates from my other templates.

55
00:04:28,340 --> 00:04:35,840
So now underneath error pages I'm going to create a new file called for 0 4 h T.M..

56
00:04:35,930 --> 00:04:40,600
So this is for 0 for the H Tim L and it's underneath the error pages.

57
00:04:40,610 --> 00:04:47,090
So directory of templates and what you can do is basically write out whatever you want to show up for

58
00:04:47,210 --> 00:04:48,520
the four or four page.

59
00:04:48,530 --> 00:04:54,830
What's nice about this is I can then extend from base H.T. mail that way even if a forward for error

60
00:04:54,830 --> 00:04:58,810
occurs the user still has access to the navigation bar.

61
00:04:58,880 --> 00:05:06,110
So say extends from base T.M. and then we'll set up our block called content and here I'm going to have

62
00:05:06,110 --> 00:05:13,160
a Jumbotron that basically tells the user let's say four or four page not found

63
00:05:16,090 --> 00:05:18,400
so if you ever want to add in more errors.

64
00:05:18,430 --> 00:05:22,810
All you need to do is come back to the handlers thought py file.

65
00:05:22,810 --> 00:05:28,470
So essentially the views file for errors and then you just add in whatever errors you want.

66
00:05:28,780 --> 00:05:34,660
So for example if you're interested in a 403 error so that's the kind of error that occurs when something

67
00:05:34,660 --> 00:05:35,570
is forbidden.

68
00:05:35,590 --> 00:05:39,270
So maybe you're trying to update someone else's blog posts without having permission.

69
00:05:39,430 --> 00:05:40,080
That's a 4.

70
00:05:40,090 --> 00:05:41,190
0 3 error.

71
00:05:41,410 --> 00:05:44,070
The way you would do that is pretty much exactly the same.

72
00:05:44,110 --> 00:05:45,530
You just change the code.

73
00:05:45,820 --> 00:05:53,650
You would say error underscore pages dot app underscore error handler passen that particular code.

74
00:05:53,680 --> 00:06:05,550
So if 403 is forbidden access and then we say DPF error 0 3 you pass on the actual error and then will

75
00:06:05,550 --> 00:06:11,380
we say here is return render template and then we would just set up another template for that.

76
00:06:11,380 --> 00:06:19,500
So I'll say error pages 4 or 3 that each T.M. and then we say comma and we actually pass in the integer

77
00:06:19,500 --> 00:06:20,610
code for that.

78
00:06:20,610 --> 00:06:23,450
So that basically connects us to that actual air.

79
00:06:23,510 --> 00:06:27,560
OK so since we're saying four or three H-2A l Let's go ahead and create one more template.

80
00:06:27,870 --> 00:06:31,610
We'll say new file for over 3.

81
00:06:31,650 --> 00:06:37,020
The H team L and this is actually actually going to look pretty much exactly like the Frot for.

82
00:06:37,040 --> 00:06:41,170
So going to copy and paste this except I'll say it's sort of four or four page not found.

83
00:06:41,440 --> 00:06:53,640
I will just type out for three forbidden access also access for bin and then the last thing for us to

84
00:06:53,640 --> 00:06:59,700
do before we actually test this out is make sure we actually link this blueprint to our in it that PI

85
00:06:59,700 --> 00:07:00,330
file.

86
00:07:00,540 --> 00:07:05,640
So we have this blueprint called error pages and it's underneath this handlers that pile underneath

87
00:07:05,670 --> 00:07:08,160
err pages underneath puppy company blog.

88
00:07:08,160 --> 00:07:13,350
So I'll come over to it thought pie and we're going to do a very similar thing to what we did here.

89
00:07:14,620 --> 00:07:23,350
We're going to say from puppy company blog dot and in this case it's called Air underscore pages will

90
00:07:23,350 --> 00:07:32,420
say handlers import error underscore pages and then we're just going to register that will say take

91
00:07:32,420 --> 00:07:39,290
the app and register that blueprint of error pages and save those changes.

92
00:07:39,290 --> 00:07:44,830
So now that will link those two additional views 4 4 0 3 or 4 0 for errors.

93
00:07:44,870 --> 00:07:45,680
So let's check it out.

94
00:07:45,680 --> 00:07:50,260
We're going to be will check the 404 error right now because we don't have any logging permissions set

95
00:07:50,260 --> 00:07:50,490
up.

96
00:07:50,540 --> 00:07:55,020
But as long as that works we should be able to see the idea of it.

97
00:07:55,090 --> 00:07:56,530
So I'm running my application.

98
00:07:56,560 --> 00:07:58,650
Let's go ahead and go to the home page.

99
00:07:58,660 --> 00:08:00,140
Let me bring in my browser.

100
00:08:00,340 --> 00:08:04,510
So here's my browser Papy company blog about everything looks good.

101
00:08:04,660 --> 00:08:07,060
Let's go ahead and go to a page that we know this exists.

102
00:08:07,060 --> 00:08:13,630
I was going to type in some random letters here and now I get back my custom template for 0 4 page not

103
00:08:13,630 --> 00:08:14,340
found.

104
00:08:14,640 --> 00:08:15,130
OK.

105
00:08:15,340 --> 00:08:18,860
So that's how you can perform error handling of your own custom templates.

106
00:08:19,000 --> 00:08:25,050
Just to review what we did here is recreated underneath this error pages directory a handlers that pi

107
00:08:25,060 --> 00:08:25,560
file.

108
00:08:25,600 --> 00:08:30,090
So you could call those views up high but by convention people call it handlers up high.

109
00:08:30,160 --> 00:08:36,370
We set up our blueprints connecting it and then we call error pages dots and then notice here that difference

110
00:08:36,390 --> 00:08:42,610
and set of that route we say app underscore error handler pasand the actual HTML error you're looking

111
00:08:42,610 --> 00:08:45,030
for then passen that error.

112
00:08:45,020 --> 00:08:50,980
Connect it to a template and then also remember repassing 8 were returning a tuple really for this app

113
00:08:51,000 --> 00:08:55,930
error handler to handle in the back and you'll say comma and then four or four or whatever the code

114
00:08:55,930 --> 00:08:57,210
happens to be.

115
00:08:57,220 --> 00:09:01,110
So here flask is actually performing a lot on the back end for you.

116
00:09:01,330 --> 00:09:07,960
Essentially all you need to remember is to call app underscore air handler and to return the tuple including

117
00:09:07,960 --> 00:09:13,770
the rendered template and the actual code beyond that flask is doing a lot more for you on the backend.

118
00:09:13,990 --> 00:09:19,440
Then we just create our HTL templates here of whatever you want to display for any given error.

119
00:09:19,480 --> 00:09:23,760
And then remember we wanted to link the actual blueprints.

120
00:09:23,770 --> 00:09:24,460
All right.

121
00:09:24,460 --> 00:09:25,810
That's it for handling errors.

122
00:09:25,810 --> 00:09:27,550
I hope that was useful to you.

123
00:09:27,550 --> 00:09:31,900
Coming up next we're continue on with the main part of the project we'll see at the next lecture.
