GoRoutines and Error Handling: Essential Techniques for Software Engineers

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] let's talk about what the responsibility of this Handler function is because that's critical the Handler function is at the app layer and we said that the app layer's job is to receive external input validate that input call into the business layer and then send that response back out if it's okay but I don't want the Handler dealing with eror handling I'd rather push the erir handling back into the business layer if I could or somewhere outside of the Handler for consistency why am I saying that you know one time I was working with a a web API That was supposed to return Json and it started returning XML one time I had to build a front-end tool against a web API and every single call had a different data model for the error this was a nightmare situation for me there was no consistency in anything related to these apis plus I want a little bit of logging that goes on when we're when we're doing something if I rely on developers to add logging into these handlers it's going to be missed or it's going to be different and so what I want to do is try to find a way engineering in Wise to make sure that anybody who's writing a Handler only has to do the pieces that are very specific to that Handler and have a way of all the other stuff like logging air handling metrics panics authentication all these other things be taken care of in a consistent way behind the scenes now out of the box this Handler function doesn't really support that why because it's at the outermost layer of our function call what we really need what we really need or let's just say what I want is I would like this Handler function to be able to return an error I would love it to not have to process errors but return an error back to something I would also love the first parameter to be a context and to get it out of the request immediately and to keep this sort of go idiom that I think is a really really good idiom remember that they added context back in version 1.8 of go so they had to integrate context into the request concrete type and the API feels a little clumsy when you're working with it if we could get the context out super clean uh early then we can have a more idiomatic sort of Handler function I think if there was ever a go 2.0 or the context was part of go 1.0 this is what your Handler functions would have looked like like this but I want that anyway this is what I need this is the real problem look let me just diagram it for you so you can understand what the problem is remember what we have here I've got this go routine that started the mck now when we bind that route like hack think of that Circle as the fun function hack so if somebody comes in over the wire and says I want to run hack the mck sees that route and then calls this function that's what we have right now here's the problem if I were to return an error who am I returning the error back to at this point I'd be returning the error back to the m it has no idea how to process that error it's not even its job so this is the the problem the problem is is that our hack function is this most outer layer function is connected directly to the MX if we want to be able to return errors to something we have to change the model so there is at least other functions that get called before hack in other words I need hack in the middle of this situation not at the outer layer if I can find a way to move the hack function in the middle then I could find a way to wrap more functions around it then what the mck is doing is essentially making a call to this most outer layer function eventually passing through hack and coming back which now means that if hack returns an error this could handle the error this could handle logging this can handle whatever I need a little bit of infrastructure to make sure that hack isn't at the most outer layer of the M with the mck but ends up being in the center of everything now we can do this if we write our own mucks for sure but how many people want to write their own mucks I don't want to write my own mucks not at all so instead of writing a new MX I think what we should do is try to steal the HTTP tree steal as much of its functionality as possible and tweak it just a little bit so we can get to this we can get to this we have complete control over everything we want to do here and we can end up with Handler functions that look like this okay so how do we go about doing that well we're going to come into our foundation layer and we're going to add a new package that I'm going to call web and the web package is going to give us the tiniest little web framework you've ever seen just has to be tiny because we're not writing a new web framework or MX we're stealing one let's break that down so under Foundation web we're going to have our web. go and one of the first things that I want you to see one of the first things I'm going to add right now where we need it all right is this what I'm going to say is the app actually let's do it this way to start this is cooler what I want to do is Define this typ named app and what I'm going to do is embed the mck inside of it appoint it to the MX because that's how it gets constructed by embedding the mck in the app that means the app is everything a m is its entire API becomes right promotes up to app this is how we steal the mck this embedding Is Us stealing and then we can extend and add to it so just this alone we can do some interesting things here look let's create our new function all right let's create our new function and all the new function has to do is construct let's do it this way so what do we do because this is in a web package I'm not calling this new I'm calling it new app because we're not returning a web we're returning an app if I wanted that type on line 12 to be web this function would just be new that's sort of the idiom Define a type named after the package and then you can have a factory function like new I didn't like the idea of this being called Web because it's more of the web app so I defined that type as app I call this new app but notice we're constructing this app and because we're embedding the context MX here watch what we can do just with this little bit of code just like this all right let's go back to um V1 instead of constructing a MX here a tree Mox we can construct a new app I'm going to call this app which eventually we have to make some changes for that as well but now what we do is return a web. apppp Now new app needs the shutdown Channel at least that's where we're starting so we get that there so now by constructing a web new app we've sort of hidden the implementation of the mucks we're using this will let us later on rip out tree mucks and you can use the standard Library mucks later on because we're going to create this abstraction notice we're not creating an abstraction through an interface we're creating an abstraction through a concrete type I can't stress that enough you will see me throughout this code base create abstractions not through interfaces but through concrete types don't need interfaces most of the time you don't and the embedding is giving us that app is a concrete type okay so let's go back now our route Adder interface that we added right it can't take that MX anymore it's got to take a web app now the interface has to take a web app because it's the web app that now is everything the mck is okay that that's sort of cool now we've got to implement that again right so we're going to change this now our Imports are all clean because everything's going down and this is the beautiful part notice no errors there a web app is now every thing a context MX is because of the embedding we just stole the entire MX with one line of code which was the embedding of that type and now Maine can construct the routes the routes can call the hack group routes with the web app the web app can call handle because they got it for free through the embedding of this we're on our way now on being able to take the existing mck and make this work in fact if I push this code up to you now and once you get it if we do another Dev update everything should work exactly the way it did our web app or app type is now 100% the mck through that promotion of the embedding make Dev update make curl boom still works just like before nothing's changed all we did is added at this point a concrete type named app with the embedding promoted the entire MX mx's API app is now a MX now we can customize the MX to be what we want it to be does everybody see the code change that we did how powerful that embedding can be and what we've done now is we're using a concrete type it's part of this project under foundation and we're not necessarily leaking the mck that we're using now we could replace this mux with any mux you want and it doesn't break anything above as long as we maintain the same apis here
Info
Channel: Ardan Labs
Views: 1,002
Rating: undefined out of 5
Keywords:
Id: 8eZZEEEuudo
Channel Id: undefined
Length: 13min 28sec (808 seconds)
Published: Mon Jun 17 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.