Why Golang HTTP Handlers Should Return An Error

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
ret turning errors from uh HTP handlers is very important why am I going to do this well because I made a video why the goang 1.22 HTTP router Is Not Great these are actually responses I get right and I was telling that basically the HTP handle needs to return nrow and um I was a little bit flabbergasted by the responses by people right they hey everybody can leave their opinion that's perfectly fine but I didn't thought there was so many people that say Hey listen I don't agree that the SD Handler should return an error how would that error be handled functions run as go routines yeah of course there is no point in returning errors there are so trust me any rather that that here the SD lip um a created Handler doesn't return an error the error comes from the service that's called within the Handler in any case so people I have the feeling that people do have no clue why that's why we're going to do this right now and you imagine you can follow right let me actually just get usage let me let me demote this thing to the to the to the B right so we have actually let's make it complete how do we do this uh router handle Funk uh how this oh huh get is it this one US it ID not quite sure I think it's something like that handle get user by ID and then copy paste this in get you something like that you know what I mean handle get you list user by the way list users presenter this is it this is it right very simple but can you imagine you're going to have a big list a big API get user get admin y yada uh a big API imagine that we have two examples but imagine we have way more handl as this right all right so let's not um okay so how are we going first of all how are we going to make sure that we have if you want if you want to have a logging system like we want to log every error that occurs in our system how do we do that that's very simple right so here is an error so we're going to do an as lock error right I'm going to say um HTTP Handler error right we could do Handler and we could say it's going to be the SL handle get user by ID something like that and I'm going to say the eror is this thing right so we need to do the same thing for this other guy here right boom same thing but it's going to be handlist users right users something like that you know is it always a maybe you could do something like the status code here right status you can see already the problem coming right you doing HP man this is so huge status that is bad uh Gateway it's fine the quest actually let us be because otherwise Reddit will Pitchfork me and yeah cancel me and all that stuff so you can see the problem right you need to do this for all your handlers and besides that this is a very simple Handler but you can imagine that you're going to have multiple goals so you're going to have multiple of these things right right it could be another arrow it could be some something else you need to check boom and you have the same thing right and then again somewhere in there boom and you have the same of this of this of the shenanigans going on in your cat very dirty but that's fine I'm dirty I like dirty but there's a problem because what if you want to implement something like um grafana logging or like a little elastic search or something else you cannot swap it then you need to this is our two this is one Handler these are two handlers imagine you have 50 of these handlers and you're going to change your logging system you are [ __ ] let us be honest you are screwed right so I cannot imagine why people say to me but it needs to be like that no it's not Timmy it's not this is garbage don't do that right what you need to do is you need to make sure that every Handler is returning an error right here for example we're going to change this up this needs to return an error also this bad boy needs to return an error right yeah but you handled in go routines yeah so what you know what I mean I don't understand I don't need I I basically from all the comments they they they comment on this previous video I have there's no legit comment that I say hey actually you're right no you're all wrong in this case right so what is this going to do was is very simple because right now we can delete this uh and we can actually just do something like we can return this right Json um yeah and we can return this thing easy peasy no logging needed we just return what we need to return uh and that's done uh what we also can do is actually to be honest we don't even need we don't even need to return right Json you could do this right you could say type uh API error is going to be a structure right then we going to say for example a status it's going to be an integer and then uh maybe an an error itself I don't know or or a message or whatever you guys be creative uh it could be a string right and then we do this funk uh e API errors errors string and then we just return uh this return e message or something right boom and now we can actually delete this and we can do something because maybe we want to write this and maybe we have a specific way because this is right Json right look at this guys it's very it's actually very important [ __ ] I cannot go back in history uh because then this is gone wait return e message people this is so important actually this is going to write your error in a specific way right it's going to use this format right the map string any and it's just going to do an errow these right but imagine you have 50 of those 50 handlers but you want to change you you and your front end team you decided hey we're going to change up our errors we want some extra information from the back end so we can do some cool stuff in the front end good [ __ ] luck why because you need to go through all of your handlers here and change this thing but what if you do what if you make an API error here which is a structure which implements the error interface this this thing is an interface right so we could actually return this uh hey stop uh this API erors and let's say uh it's going to be a status I don't know the HTTP status bad bad request and then the message is going to be timy something like that right and that's the only thing we need to do and everything works pretty fine and we don't we don't here we don't need to care how this is going to be marshaled that's for later on you know what I mean um okay the same thing goes here right uh let me actually let's copy the whole ship bang here why not let's paste that in let's delete this right let's delete these arrows because we don't need them anymore uh let's return right boom okay so on the next step is to basically uh now we're going to have an arrow here right at the top because this needs to have this has a wrong signature so we going to fix that um how we going to fix this uh make handlers this is where I [ __ ] up all the time let's do something like a Type U API Funk or something I can't see man I can't see Aros that's that's our Handler so we're going to say FN or Handler uh it's going to be an API think just like that and we're going to return HTP do Handler think or something is that true no clue we're going to see right and now let me actually do this uh paste back in paste it in here return this delete this eror because we don't need it do this this and this if F it's going to be h w at not n Save uh right Json boom W the status what we could do here is actually assert this Aros uh how can we do that is it like that can we do something like if okay that is going to be an API error okay and then we can do e e status e just too good I'm just too good let us be honest here y y yada right and you can and if it's not an API error it's going to be some some some other error maybe you have a special error special API error or maybe you have just standard error and you can based on that error you can do what ever you want right that's that and of course now you can do an as here uh error and you can do whatever you want right you could say uh API errors um you could also basically if it's if it's an uh an an internal server error you can actually um do a special message so you don't leak critical information for example right very important you don't want to log out database information cannot find y y okay so uh you could do that the error is basically the error here actually e right and you could do actually you could do this here right or doesn't really matter status the that is yada yada Y and whatever you want to lock you're going to lock here right so now it's completely separated so now you need to actually explain to me because I'm going to cut this video out and it's going to be on the internet explain to me why have why not actually before we do this let's make it complete uh make Handler why is vs why don't vs code no what I want make Handler Handler man skill issues insane uh let's do make hand I want to be fast because we don't have much time isn't it well now we're done right so why is returning an errow from your handlers actually not the correct play here can somebody please explain me please
Info
Channel: Anthony GG
Views: 6,670
Rating: undefined out of 5
Keywords: golang, programming, golang 1.22, golang 1.22 router, golang http, golang http handler, golang tutorial, learn golang, golang for beginners, golang course, golang tutorials, golang beginner tutorial, golang programming, golang concurrency, why learn golang, go tutorial, go programming, golang introduction, vim, vscode, http, software engineering, golang error handling, golang handlers
Id: g-gsmike7qc
Channel Id: undefined
Length: 11min 43sec (703 seconds)
Published: Mon Apr 01 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.