A Beautiful Way To Deal With ERRORS in Golang HTTP Handlers

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what is up guys this video is going to be about error handling uh in goang especially from uh API so it's a discussion that's basically ongo for quite a while um like how do we need to actually return errors do handlers actually need to return errors in the first place I think it doeses and and this is the reason why so I am building this new service for uh my company leue and for the people that are interested I'm building the service completely Live recorded uh on the total coder right so if you want to know how a million doll comp is getting multi-million dollar company is getting built you can check the total coder all the views are there right uh so basically we have this handle create customer right and um so what's happening basically we have a create customer request this is very beginning code right this is not the complete service already I'm just building this right now so uh hey give me some credits um so we have this create customer request we are going to basically decode um the create customer request uh from uh the response body right so what can happen here uh basically if you decode that stuff well uh couple things can happen right if if if if people post invalid Json you basically need to handle that right you need to return a decent error message right but what can also happen is that this request if you validate this uh it can be that um yeah that some fields of that request are basically invalid some people forgot to uh provide a value for the field maybe the field is too short maybe it's too long or whatever right so you need to handle that and I see people um do nasty stuff in their handlers and it's very hard to debug and it's not a pleasure to read so um goang erors is basically an interface so you can do whatever you want with that right and you can see what's happening here so for example uh if there is a bad Json if it's a Mal for J ason I just return inv valid Json and how does it actually work first of all let me actually show you here is this running yes it is let me show you this so for example if I post to customer with no Json at all boom you're going to see that we have this beautiful let me zoom in a little bit more uh you're going to see that we have a status code 400 right it's a bad request and we have a message here which is invalid Json request data just like that could be another message so really matter right uh but if I actually provide some values for example I'm going to say name and let's do some one letter which basically is invalid right it will not um pass the validate request method so if I sent that boom you're going to see that we have a status unprocessable entity and the reason why I do this is because you can see that we have instead of single string message we have an object right so now the front end can see okay it's an unprocessable entity that basically means that we're going to have um an object passed into this message why because let's say a uh a customer has a name an email and a lot of other fields uh you don't want to return hey the name is invalid because then he's going to the user is going to update his name he's going to send again it's going to get another error yo your email is invalid you want to basically validate your object and return all the errors at once so the front end can uh notify the user that he needs to update all these fields for example of course you're going to have some front end validation but you also need to have a good backend validation that's very important as well so you can see that uh this completely changes right unprocessable entity and if we check our code here there is no special logic handling that it's just this function and even if we go look into this function here for example uh let's take a look at invalid Json right so it all starts I'm going to zoom a little bit out here so it all starts with an API error structure right it's going to have a status code and it's going to have a message right then we have this beautiful uh interface method which is basically needed because otherwise this API error um cannot be uh passed as an error because it does not implement the error uh method right so this is a new API error this is a generic uh simple function that basically constructs a new API errors but the cool stuff is that we have this for example invalid request data right and an invalid request data is going to uh receive a map uh of string string which is basically going to be the key and the actual error message for example name name is too short email invalid email address invalid address something like that right uh so you can see then we return this like that invalid Json just the same thing but different right in this case we return a new API error so we take this function you see we I'm actually constructing composing all these things together right it's just an that is bad request and we're going to return this error in valent request data right and of course this is not final but I'm just willing to show you that you can do a lot of stuff with these errors making it just very easy to read in your handlers and you need to write less code uh to just have the same effect so the question rather is how is this error being handled where is the magic happening well that's basically in uh Disney right this make is basically a rapper function it's a higher order function it's a decorated function whatever you want to call that and we are going to do a little check here right we're going to pause in this Handler which is basically our uh API Funk which is our representation of the Handler right it's this signature right and it's going to return an HTP Handler Funk which is goang standard Library representation of a Handler okay so we're going to check here we're going to we're going to uh get the error and we're going to check if this is an API error we are going to write it as Json exactly how we have set that Ator up at the at the at above here right above boom just like that we're going to write this Json thing out okay but if it's basically not an uh API error we are going to return an internal server error right so if it's an API error we're going to return the presentation of that API error which is our own custom error if it's not of the type API error we know it's just a it's going to be an error returned without not our error the standard calling error we just going return an internal server error why is that well let's say so uh you're going to see this code here right let me see here for example this is a customer right and this account ID is basically uh a foreign key to uh a reference to the account table in the database at this point in time there is no account in the database so what basically going to happen is that this is going to give us an SQL error like hey uh you're trying to reference account that basically does not exist right that's why we why we use SQL to get this type safety right we do not want to return that to the user we want to log that internally but we do not want to return that to the user at all because that's basically security wise could be a problem because yeah do not do that at all right so let me show you here right so for example I'm going to give a good name um let's say it's going to be I don't know F bu or something right so this is a valid data I'm going to post that boom and you see it's going to be let me zoom in again it's going to be an internal server eror that is 500 we don't want to have anything uh returned to the user over the net but if we see here this is going to make a nice lock in our backend application so we can actually see what's going on in our logs right so that's basically how this getting handled right and it's nothing more to that so let me know what you think about that a lot of people are still a little bit skeptical about returning errors in your handlers uh but I think it's just a very good way to do that uh very handy way uh to uh construct your applications right especially if you can return your own custom erors let me know what you think about that and if you're interested in these kind of videos check out the total coder in the link down below don't forget to subscribe to the channel and I see you in the next video bye-bye
Info
Channel: Anthony GG
Views: 12,482
Rating: undefined out of 5
Keywords: go programming, programming, golang tutorial, golang http handler, learn golang, go programming language, software engineering, golang tutorial for beginners, golang http, golang http server, golang error handling, go programming tutorial, golang course, software developer, go programming examples, go tutorial for beginners, golang error
Id: aS1cJfQ-LrQ
Channel Id: undefined
Length: 8min 42sec (522 seconds)
Published: Wed May 08 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.