1
00:00:01,050 --> 00:00:05,580
So for a while now, I've been saying that we're going to improve our air handling and that's what I

2
00:00:05,580 --> 00:00:07,030
want to take care of in this section.

3
00:00:07,740 --> 00:00:14,040
So if you recall a long time ago when we set up the app config, I put in this line in full log and

4
00:00:14,040 --> 00:00:17,550
I said, we're not doing anything with it right now, but we'll deal with that or make it.

5
00:00:17,580 --> 00:00:21,290
It'll be useful when we actually start our error handling, our improved error handling.

6
00:00:21,720 --> 00:00:23,730
And this is the case right now.

7
00:00:24,090 --> 00:00:29,820
So at the moment, we have used cash, template, cash in Fellag in production and session set in our

8
00:00:29,820 --> 00:00:30,480
app config.

9
00:00:30,630 --> 00:00:33,840
And I'm going to add another one and I'm going to call it error log.

10
00:00:34,380 --> 00:00:41,940
And what we're going to do is have two things available to us in our application configuration that

11
00:00:41,940 --> 00:00:45,360
allow us to write logs and handle errors more efficiently.

12
00:00:45,570 --> 00:00:46,800
So I've set up another one.

13
00:00:46,810 --> 00:00:52,410
So now I have info log, which is a pointer to log logger, an error log, which is also a pointer to

14
00:00:52,410 --> 00:00:59,820
log longer and log longer is something built into the application, into goes standard library.

15
00:01:00,030 --> 00:01:02,830
So now let's go back to our main function and do something with those.

16
00:01:03,210 --> 00:01:10,800
So in my main function, I in under my run part right after I declare my app in production is equal

17
00:01:10,800 --> 00:01:11,310
to fault's.

18
00:01:11,550 --> 00:01:15,600
I'm going to set up right here and it can be anywhere in this, but I'm going to put it right at the

19
00:01:15,600 --> 00:01:21,800
top of my run just after the app introduction is set to and I'm going to set up some orders.

20
00:01:21,810 --> 00:01:28,350
So let's create a longer let's create one called Infl Log and that is going to be equal to I'll have

21
00:01:28,350 --> 00:01:30,210
to declare it in a moment that I'll do that in a moment.

22
00:01:30,420 --> 00:01:35,610
That is going to be equal to log, which is a standard package built right into the standard library.

23
00:01:35,940 --> 00:01:39,810
Knew I'm creating a new logger and it's going to right.

24
00:01:39,810 --> 00:01:40,230
Where does it.

25
00:01:40,230 --> 00:01:40,540
Right.

26
00:01:40,730 --> 00:01:40,980
Right.

27
00:01:41,100 --> 00:01:44,660
Well I just want the info log right now to print to the terminal.

28
00:01:44,670 --> 00:01:52,590
So I'll just say print it to OS Standard Act, which again is built right into the standard library

29
00:01:53,100 --> 00:01:55,580
and that is our terminal window.

30
00:01:55,620 --> 00:01:56,620
That's where it's going to print it.

31
00:01:57,150 --> 00:01:58,440
So we're going to print it there.

32
00:01:58,620 --> 00:02:02,450
And then the second argument is, do you want to prefect's this log with anything?

33
00:02:02,460 --> 00:02:07,620
And I do I want to prefix it with the word info so I know that it's an information and then I'll just

34
00:02:07,620 --> 00:02:10,020
put a tab after it, which is the forward.

35
00:02:10,440 --> 00:02:15,670
He just says write the word info and then put a tab in a bunch of blank spaces after that.

36
00:02:16,440 --> 00:02:18,150
And then what do I want to write in there?

37
00:02:18,150 --> 00:02:22,370
At the end of it, we have the flag, something that we can actually append to it or deal with it.

38
00:02:22,500 --> 00:02:30,510
I'll put in the log l date, which is a date in a nice readable format, and then I'll put a pipe and

39
00:02:30,510 --> 00:02:32,430
I'll say log all time.

40
00:02:32,880 --> 00:02:36,760
And this allows me to use this info log variable, which I should declare.

41
00:02:36,780 --> 00:02:37,890
So let's do that right now.

42
00:02:38,250 --> 00:02:41,130
We'll just say var info log

43
00:02:44,430 --> 00:02:48,870
is of type star logged logger and I'll do the air along to this.

44
00:02:49,110 --> 00:02:50,520
We're going to be using that in a moment.

45
00:02:50,970 --> 00:02:51,630
Error log.

46
00:02:53,940 --> 00:02:55,820
So I've created my info log here.

47
00:02:56,190 --> 00:03:03,420
Now I can make that part of my app wide variable abdon info log is equal to input.

48
00:03:04,410 --> 00:03:08,490
I could have done this on one line and just said the info log is equal to all of this, but I think

49
00:03:08,490 --> 00:03:09,240
this is more readable.

50
00:03:09,690 --> 00:03:13,710
Now let's set up an error log and I want an error log.

51
00:03:13,960 --> 00:03:18,120
I actually when I go to a final version of this, I want to read it to a file, but I'm not going to

52
00:03:18,120 --> 00:03:19,170
worry about that right now.

53
00:03:19,200 --> 00:03:20,610
I'll just make the error log.

54
00:03:20,610 --> 00:03:25,170
Also write to the terminal, which is useful for development purposes, because I don't have to keep

55
00:03:25,170 --> 00:03:28,200
opening an external file to see if there are any errors or to read it here.

56
00:03:28,680 --> 00:03:32,340
So we'll make error log equal to log new.

57
00:03:32,730 --> 00:03:34,410
And where do I want it to write right now?

58
00:03:34,410 --> 00:03:36,030
I want to write to OS Standard out

59
00:03:41,940 --> 00:03:49,580
and I want to prefix that with error and a tab and what do I want to write to it?

60
00:03:49,590 --> 00:03:53,160
Well, I want to write something a little bit more sophisticated to the error.

61
00:03:53,170 --> 00:04:04,350
Look, OK, so I'll make it right to, I'll have the log lda same as before and then a pipe and then

62
00:04:04,350 --> 00:04:06,600
log all time, same as before.

63
00:04:06,780 --> 00:04:12,720
But then I'll put at the end of that a little bit more information log l short file, which will actually

64
00:04:12,720 --> 00:04:14,940
give me information about the error.

65
00:04:15,600 --> 00:04:19,490
And now that I've created that I can actually put that in my site.

66
00:04:19,490 --> 00:04:19,640
Right.

67
00:04:19,650 --> 00:04:23,220
Config airlock is equal to here.

68
00:04:23,220 --> 00:04:29,580
Look now the fact that I've done that, the fact that I've created those two things means it's available

69
00:04:29,580 --> 00:04:32,580
to available to me anywhere I want to use it.

70
00:04:33,030 --> 00:04:35,670
And there's lots of places I probably want to use it.

71
00:04:35,940 --> 00:04:40,890
I could go back and just every time I want to write an error message instead of going log, print line

72
00:04:41,130 --> 00:04:45,660
or format dot print line, I could use my error log or my info log.

73
00:04:46,140 --> 00:04:49,020
But I want to be a little more sophisticated about this.

74
00:04:49,020 --> 00:04:51,720
So what I'm going to do now is create a new package.

75
00:04:52,230 --> 00:04:57,480
I'm going to go back to Internal because it's an internal package only useful to us in this application.

76
00:04:57,870 --> 00:04:59,910
I'll create a new directory and I'll call that help.

77
00:05:00,350 --> 00:05:05,570
And here's where I'm going to put things that are useful that I can use in various parts of my application

78
00:05:06,440 --> 00:05:08,650
and just call them by calling the helpers package.

79
00:05:08,840 --> 00:05:16,460
So let's create a new file on that, a new go file, which I'll just call helpers, helpers Dutko and

80
00:05:16,460 --> 00:05:17,820
I will add it to the repository.

81
00:05:18,500 --> 00:05:20,210
So what am I going to put in this helper's file?

82
00:05:20,420 --> 00:05:26,150
Well, to start with, one of the things I want to have access to in the Helper's file is my at my site

83
00:05:26,150 --> 00:05:26,810
white config.

84
00:05:26,810 --> 00:05:31,820
So I'll create a new variable called app and that's going to be a pointer to config app config.

85
00:05:32,240 --> 00:05:39,440
So I have that variable declared, but I need some method of actually populating it with the site where

86
00:05:39,440 --> 00:05:40,040
I can fix.

87
00:05:40,040 --> 00:05:43,130
So I'll create a new function func New Helper's.

88
00:05:43,640 --> 00:05:45,470
And that will take one argument right now.

89
00:05:45,470 --> 00:05:50,740
A Stargate app config app config.

90
00:05:50,750 --> 00:05:55,160
So it takes we're going to pass this function, our application and we're not going to return anything

91
00:05:55,460 --> 00:05:57,230
and all we'll do is set up equal to.

92
00:05:58,730 --> 00:06:03,890
So when I call this, which I'm going to do right now, when I call this function, say, from our main

93
00:06:03,890 --> 00:06:09,920
dock, go where I'm doing it everywhere else, I can do this, duplicate that and make this helper's

94
00:06:11,630 --> 00:06:19,880
new helper's when I call it now when this application is set up, this run function is called, it will

95
00:06:19,880 --> 00:06:25,040
actually populate that variable app in Helper's with a pointer to config.

96
00:06:25,400 --> 00:06:27,950
So now I have access to it and I should give this a comment.

97
00:06:30,230 --> 00:06:33,830
Sets up app config for helper's.

98
00:06:34,850 --> 00:06:35,210
All right.

99
00:06:35,470 --> 00:06:37,630
Now what I want to have in this helper's function.

100
00:06:37,910 --> 00:06:42,920
Well there are what I'm trying to do in this, this section of the course is deal with errors.

101
00:06:43,340 --> 00:06:48,110
And if you think about it logically, there are two kinds of errors that we can have on a regular basis.

102
00:06:48,620 --> 00:06:55,130
We can have client errors where the client did something wrong, or we can have server errors where

103
00:06:55,130 --> 00:06:59,030
despite our best efforts, something went wrong with the server.

104
00:06:59,040 --> 00:07:03,500
So our code through an error, maybe it panicked and then recovered from it, whatever it may be.

105
00:07:03,950 --> 00:07:09,860
But what I effectively have are two kinds of errors, func client error, and that's going to have to

106
00:07:09,860 --> 00:07:10,790
take some parameters.

107
00:07:10,790 --> 00:07:11,720
And what am I going to Pasek?

108
00:07:11,720 --> 00:07:17,870
Well, when you have a client error, one of the things that's really useful is to have the response

109
00:07:17,870 --> 00:07:18,150
writer.

110
00:07:18,200 --> 00:07:20,600
So let's assume we're going to pass it.

111
00:07:20,600 --> 00:07:25,700
The response Frieder HGP DOT response writer, because we might need to write something to the client.

112
00:07:26,240 --> 00:07:29,570
And then when they gave the error, we will have a status.

113
00:07:29,570 --> 00:07:36,110
We'll know what kind of error that was, whatever it may be, a bad request, 400 or a 403, whatever,

114
00:07:36,110 --> 00:07:40,760
but you're going to get a status and that's what that'll be in the form of an ENT and this won't return

115
00:07:40,760 --> 00:07:41,140
anything.

116
00:07:41,270 --> 00:07:46,730
So I'm going to leave that empty for right now and I'll create the second useful function func server

117
00:07:46,730 --> 00:07:47,120
error.

118
00:07:48,170 --> 00:07:49,850
Something went wrong with the server.

119
00:07:50,780 --> 00:07:56,270
And for here, in this case, I'm still going to want the response writer because I'm going to have

120
00:07:56,270 --> 00:07:58,280
to write some kind of response to the client.

121
00:07:58,640 --> 00:08:01,220
But rather than the status in this point, we're going to have an error.

122
00:08:01,770 --> 00:08:03,710
I know I'm going to have an error when I do that.

123
00:08:03,710 --> 00:08:05,930
An error is of type error.

124
00:08:07,820 --> 00:08:10,130
So there are my two functions.

125
00:08:10,160 --> 00:08:12,620
Let's deal with the first one first, the client error.

126
00:08:13,250 --> 00:08:15,280
And what am I going to do here?

127
00:08:15,290 --> 00:08:19,550
Well, what I'm going to do right now is throw in HTP error because of the client error.

128
00:08:19,550 --> 00:08:24,620
I need to give them some kind of response and built into the standard library.

129
00:08:24,620 --> 00:08:31,730
We have HTTP error, so I'll use an error and it takes two arguments.

130
00:08:31,880 --> 00:08:36,320
It takes a response rate, which I have, because I'm taking that as one of the parameters for this.

131
00:08:37,070 --> 00:08:39,310
And it also has an error string.

132
00:08:39,560 --> 00:08:41,210
Well, I don't know what that is.

133
00:08:41,360 --> 00:08:42,650
Yeah, I actually do.

134
00:08:42,740 --> 00:08:43,940
I now have the status.

135
00:08:43,940 --> 00:08:44,520
That's easy.

136
00:08:44,540 --> 00:08:49,790
The third one is the status, so I know I can pass the status, but I can actually again use the built

137
00:08:49,790 --> 00:08:57,230
in functionality of the standard library to say http status text and then pass it the status.

138
00:08:58,340 --> 00:08:59,790
And the last thing is the end.

139
00:08:59,810 --> 00:09:01,790
What kind of status am I going to be returning?

140
00:09:01,800 --> 00:09:02,750
Well, I already have that.

141
00:09:02,750 --> 00:09:04,610
I'm passing it as an argument status.

142
00:09:05,480 --> 00:09:14,120
So there is one thing I can do a cleaner, but I can also if I want to write to the error log or write

143
00:09:14,120 --> 00:09:15,230
to the info log.

144
00:09:15,470 --> 00:09:19,520
So what I want to do here, let's just say we're going to write to the envelope just for the sake of

145
00:09:19,520 --> 00:09:19,910
argument.

146
00:09:20,060 --> 00:09:28,250
I can now because I have that that application we can think I can say app dot info, log dot print line

147
00:09:28,490 --> 00:09:30,260
and then I can put whatever I want in here.

148
00:09:30,620 --> 00:09:31,910
So what am I going to put in here?

149
00:09:31,910 --> 00:09:39,050
I can say client error with status of and then pass the status.

150
00:09:40,700 --> 00:09:45,170
So now I can actually write to to a log file and that's not terribly useful, but that's just demonstrating

151
00:09:45,170 --> 00:09:51,530
that we now have access to this info log in the same way if we actually have an error, I want as much

152
00:09:51,530 --> 00:09:54,820
information as I can possibly get about that error.

153
00:09:55,340 --> 00:09:59,720
So one of the things I can do is actually get a trace of the error using some of the.

154
00:09:59,790 --> 00:10:04,560
Functionality built into the standard library, so I'm going to create a variable called Trece, which

155
00:10:04,560 --> 00:10:08,280
is going to trace the nature of the error and that will hold a string.

156
00:10:08,280 --> 00:10:13,770
And that string is going to be built up using our font format package formatted as print F.

157
00:10:14,310 --> 00:10:19,280
And inside of that, I'm going to put in a string that consists of a series of placeholders.

158
00:10:19,290 --> 00:10:22,160
The first one will be a string and will populate that in a moment.

159
00:10:23,340 --> 00:10:31,560
The second one will be again, which gives me a new line, like he writes a tab and writes a new line

160
00:10:32,040 --> 00:10:34,150
and then I'm going to put another string.

161
00:10:35,220 --> 00:10:37,350
And so I have two placeholders to replace.

162
00:10:37,350 --> 00:10:39,930
The first one will be the nature of the error itself.

163
00:10:39,930 --> 00:10:40,620
Will I have that?

164
00:10:40,620 --> 00:10:46,860
Because I've passed it as a parameter right here so I can go error error, which will print the error

165
00:10:46,860 --> 00:10:48,000
message as a string.

166
00:10:48,300 --> 00:10:54,660
And then I want the actual stack trace the detailed information about the nature of the error that took

167
00:10:54,660 --> 00:10:55,110
place.

168
00:10:55,320 --> 00:10:59,550
And I can get that from debug, which is built right into the standard library.

169
00:10:59,880 --> 00:11:02,580
And the method or the function I want to call on that is stuck.

170
00:11:03,930 --> 00:11:04,340
All right.

171
00:11:04,470 --> 00:11:11,100
So now I've got this trace string, this variable trace that consists of the error message and then

172
00:11:11,100 --> 00:11:14,510
a new line and then the stack trace associated with the error message.

173
00:11:14,520 --> 00:11:19,500
And that's really useful information when you're trying to figure out what went wrong, you need that

174
00:11:19,500 --> 00:11:24,780
information so we can say this right now, our error log, which we're going to use in a moment, just

175
00:11:24,780 --> 00:11:29,400
writes it to the standard, writes it to the standard, out to the terminal.

176
00:11:29,940 --> 00:11:36,330
But when we go into production, we can have our log right to a file and actually put a put a directive

177
00:11:36,330 --> 00:11:38,490
in here that might send a text or send an email.

178
00:11:38,820 --> 00:11:44,370
It can let someone know that something went wrong and then they can go open the log file and look at

179
00:11:44,370 --> 00:11:46,230
the error message and go fix the problem.

180
00:11:46,480 --> 00:11:48,660
But for right now, let's just write this error.

181
00:11:48,660 --> 00:11:50,370
Log to the window.

182
00:11:50,400 --> 00:12:00,330
Apter error log dot print line and I'll write my Trece and then I need to give some kind of feedback

183
00:12:00,330 --> 00:12:03,900
to the user, which is why I'm actually having this response right here.

184
00:12:03,900 --> 00:12:11,460
So let's again call htp dot error on error and it wants a response writer, which I have.

185
00:12:11,610 --> 00:12:15,930
We want the status text in this case the status Texas internal server error.

186
00:12:15,930 --> 00:12:21,600
But I'm going to be, you know, absolutely technically correct and use the status text function.

187
00:12:22,020 --> 00:12:30,300
And what I'll pass it is the the code for internal server error, which is HDP status, internal server

188
00:12:30,300 --> 00:12:31,770
error right there.

189
00:12:32,520 --> 00:12:38,970
And then I need the last part for my third parameter to call error is, again, the code and it's http

190
00:12:38,970 --> 00:12:43,560
dot status, internal server error and that will do it.

191
00:12:43,710 --> 00:12:48,810
Now I have two functions available to be available to me, to helper functions where I can actually

192
00:12:49,080 --> 00:12:51,810
print or handle an error in a useful fashion.

193
00:12:52,860 --> 00:12:54,810
So I created this helper package.

194
00:12:54,810 --> 00:13:00,960
I haven't done anything with it except to initialize the application config from Main Go in.

195
00:13:00,960 --> 00:13:06,390
The next lecture will go put this in a few places where we can centralize our error handling.
