So for a while now, I've been saying that we're going to improve our air handling and that's what I
want to take care of in this section.
So if you recall a long time ago when we set up the app config, I put in this line in full log and
I said, we're not doing anything with it right now, but we'll deal with that or make it.
It'll be useful when we actually start our error handling, our improved error handling.
And this is the case right now.
So at the moment, we have used cash, template, cash in Fellag in production and session set in our
app config.
And I'm going to add another one and I'm going to call it error log.
And what we're going to do is have two things available to us in our application configuration that
allow us to write logs and handle errors more efficiently.
So I've set up another one.
So now I have info log, which is a pointer to log logger, an error log, which is also a pointer to
log longer and log longer is something built into the application, into goes standard library.
So now let's go back to our main function and do something with those.
So in my main function, I in under my run part right after I declare my app in production is equal
to fault's.
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
top of my run just after the app introduction is set to and I'm going to set up some orders.
So let's create a longer let's create one called Infl Log and that is going to be equal to I'll have
to declare it in a moment that I'll do that in a moment.
That is going to be equal to log, which is a standard package built right into the standard library.
Knew I'm creating a new logger and it's going to right.
Where does it.
Right.
Well I just want the info log right now to print to the terminal.
So I'll just say print it to OS Standard Act, which again is built right into the standard library
and that is our terminal window.
That's where it's going to print it.
So we're going to print it there.
And then the second argument is, do you want to prefect's this log with anything?
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
put a tab after it, which is the forward.
He just says write the word info and then put a tab in a bunch of blank spaces after that.
And then what do I want to write in there?
At the end of it, we have the flag, something that we can actually append to it or deal with it.
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
I'll say log all time.
And this allows me to use this info log variable, which I should declare.
So let's do that right now.
We'll just say var info log
is of type star logged logger and I'll do the air along to this.
We're going to be using that in a moment.
Error log.
So I've created my info log here.
Now I can make that part of my app wide variable abdon info log is equal to input.
I could have done this on one line and just said the info log is equal to all of this, but I think
this is more readable.
Now let's set up an error log and I want an error log.
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
worry about that right now.
I'll just make the error log.
Also write to the terminal, which is useful for development purposes, because I don't have to keep
opening an external file to see if there are any errors or to read it here.
So we'll make error log equal to log new.
And where do I want it to write right now?
I want to write to OS Standard out
and I want to prefix that with error and a tab and what do I want to write to it?
Well, I want to write something a little bit more sophisticated to the error.
Look, OK, so I'll make it right to, I'll have the log lda same as before and then a pipe and then
log all time, same as before.
But then I'll put at the end of that a little bit more information log l short file, which will actually
give me information about the error.
And now that I've created that I can actually put that in my site.
Right.
Config airlock is equal to here.
Look now the fact that I've done that, the fact that I've created those two things means it's available
to available to me anywhere I want to use it.
And there's lots of places I probably want to use it.
I could go back and just every time I want to write an error message instead of going log, print line
or format dot print line, I could use my error log or my info log.
But I want to be a little more sophisticated about this.
So what I'm going to do now is create a new package.
I'm going to go back to Internal because it's an internal package only useful to us in this application.
I'll create a new directory and I'll call that help.
And here's where I'm going to put things that are useful that I can use in various parts of my application
and just call them by calling the helpers package.
So let's create a new file on that, a new go file, which I'll just call helpers, helpers Dutko and
I will add it to the repository.
So what am I going to put in this helper's file?
Well, to start with, one of the things I want to have access to in the Helper's file is my at my site
white config.
So I'll create a new variable called app and that's going to be a pointer to config app config.
So I have that variable declared, but I need some method of actually populating it with the site where
I can fix.
So I'll create a new function func New Helper's.
And that will take one argument right now.
A Stargate app config app config.
So it takes we're going to pass this function, our application and we're not going to return anything
and all we'll do is set up equal to.
So when I call this, which I'm going to do right now, when I call this function, say, from our main
dock, go where I'm doing it everywhere else, I can do this, duplicate that and make this helper's
new helper's when I call it now when this application is set up, this run function is called, it will
actually populate that variable app in Helper's with a pointer to config.
So now I have access to it and I should give this a comment.
Sets up app config for helper's.
All right.
Now what I want to have in this helper's function.
Well there are what I'm trying to do in this, this section of the course is deal with errors.
And if you think about it logically, there are two kinds of errors that we can have on a regular basis.
We can have client errors where the client did something wrong, or we can have server errors where
despite our best efforts, something went wrong with the server.
So our code through an error, maybe it panicked and then recovered from it, whatever it may be.
But what I effectively have are two kinds of errors, func client error, and that's going to have to
take some parameters.
And what am I going to Pasek?
Well, when you have a client error, one of the things that's really useful is to have the response
writer.
So let's assume we're going to pass it.
The response Frieder HGP DOT response writer, because we might need to write something to the client.
And then when they gave the error, we will have a status.
We'll know what kind of error that was, whatever it may be, a bad request, 400 or a 403, whatever,
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
anything.
So I'm going to leave that empty for right now and I'll create the second useful function func server
error.
Something went wrong with the server.
And for here, in this case, I'm still going to want the response writer because I'm going to have
to write some kind of response to the client.
But rather than the status in this point, we're going to have an error.
I know I'm going to have an error when I do that.
An error is of type error.
So there are my two functions.
Let's deal with the first one first, the client error.
And what am I going to do here?
Well, what I'm going to do right now is throw in HTP error because of the client error.
I need to give them some kind of response and built into the standard library.
We have HTTP error, so I'll use an error and it takes two arguments.
It takes a response rate, which I have, because I'm taking that as one of the parameters for this.
And it also has an error string.
Well, I don't know what that is.
Yeah, I actually do.
I now have the status.
That's easy.
The third one is the status, so I know I can pass the status, but I can actually again use the built
in functionality of the standard library to say http status text and then pass it the status.
And the last thing is the end.
What kind of status am I going to be returning?
Well, I already have that.
I'm passing it as an argument status.
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
to the info log.
So what I want to do here, let's just say we're going to write to the envelope just for the sake of
argument.
I can now because I have that that application we can think I can say app dot info, log dot print line
and then I can put whatever I want in here.
So what am I going to put in here?
I can say client error with status of and then pass the status.
So now I can actually write to to a log file and that's not terribly useful, but that's just demonstrating
that we now have access to this info log in the same way if we actually have an error, I want as much
information as I can possibly get about that error.
So one of the things I can do is actually get a trace of the error using some of the.
Functionality built into the standard library, so I'm going to create a variable called Trece, which
is going to trace the nature of the error and that will hold a string.
And that string is going to be built up using our font format package formatted as print F.
And inside of that, I'm going to put in a string that consists of a series of placeholders.
The first one will be a string and will populate that in a moment.
The second one will be again, which gives me a new line, like he writes a tab and writes a new line
and then I'm going to put another string.
And so I have two placeholders to replace.
The first one will be the nature of the error itself.
Will I have that?
Because I've passed it as a parameter right here so I can go error error, which will print the error
message as a string.
And then I want the actual stack trace the detailed information about the nature of the error that took
place.
And I can get that from debug, which is built right into the standard library.
And the method or the function I want to call on that is stuck.
All right.
So now I've got this trace string, this variable trace that consists of the error message and then
a new line and then the stack trace associated with the error message.
And that's really useful information when you're trying to figure out what went wrong, you need that
information so we can say this right now, our error log, which we're going to use in a moment, just
writes it to the standard, writes it to the standard, out to the terminal.
But when we go into production, we can have our log right to a file and actually put a put a directive
in here that might send a text or send an email.
It can let someone know that something went wrong and then they can go open the log file and look at
the error message and go fix the problem.
But for right now, let's just write this error.
Log to the window.
Apter error log dot print line and I'll write my Trece and then I need to give some kind of feedback
to the user, which is why I'm actually having this response right here.
So let's again call htp dot error on error and it wants a response writer, which I have.
We want the status text in this case the status Texas internal server error.
But I'm going to be, you know, absolutely technically correct and use the status text function.
And what I'll pass it is the the code for internal server error, which is HDP status, internal server
error right there.
And then I need the last part for my third parameter to call error is, again, the code and it's http
dot status, internal server error and that will do it.
Now I have two functions available to be available to me, to helper functions where I can actually
print or handle an error in a useful fashion.
So I created this helper package.
I haven't done anything with it except to initialize the application config from Main Go in.
The next lecture will go put this in a few places where we can centralize our error handling.