Handling errors LIKE a 10x ENGINEER in Golang - Golang Service Pattern

  Рет қаралды 21,309

samvcodes

samvcodes

Күн бұрын

In this video, I discuss handling errors when using the Service Pattern within Golang. Specifically, I talk about writing a generic service error package that can be used throughout your application to save from manually catching specific errors throughout your HTTP / gRPC / any other "adapter" your service may use.
This has personally saved me lots of pain with services that expose both an internet-facing HTTP endpoint and a gRPC internal method, saving me from having to write any code to convert errors to match the transport method.
Let me know what you think, and of course, let me know if you have any nice ideas on how you get around this problem in your Golang services.
Twitter: / samvcodes
Tiktok: / samvcodes

Пікірлер: 57
@matthew1106
@matthew1106 3 ай бұрын
Jesus dude I’ve been looking for Go content like this. I’m going to binge watch all your videos. Keep it going.
@samvcodes
@samvcodes 3 ай бұрын
Thanks for the comment! Much appreciated :) - More videos coming soon!
@packagebattos
@packagebattos 8 ай бұрын
underrated channel
@samvcodes
@samvcodes 8 ай бұрын
Ayyy thank you!
@cole.maxwell
@cole.maxwell 2 ай бұрын
So good! Thanks for being the senior I don't have
@esra_erimez
@esra_erimez 8 ай бұрын
Most impressive. Not only was this video clear and informative, I can tell you know what your talking about by how fluid you are in talking about it. Well done.
@samvcodes
@samvcodes 7 ай бұрын
Thank you very much for you comment!
@MatthiasFasching
@MatthiasFasching 4 ай бұрын
I'm not a big fan of having the service decide which HTTP error the handler returns if something goes wrong. Let's say we have a service that manages invoices and a method that returns a given invoice based on it's id. If that invoice doesn't exist in my opinion we should not return an error that is automatically mapped to 404 in a handler because depending on the context a 403 or even 5xx might be a better response. Errors returned by services should be specific to the service and the handler should decide how to map those errors to HTTP status codes not the service.
@samvcodes
@samvcodes 4 ай бұрын
I agree - I have since been bitten by this and now take the exact approach you’ve mentioned! I’ll make an updated video on this soon as I think it’s a very key point!
@leonardovillalobos5277
@leonardovillalobos5277 2 ай бұрын
Beautiful! Earned a sub, thanks bro!
@rampandey191
@rampandey191 8 ай бұрын
Thanks for the video will implement it in a current project
@samvcodes
@samvcodes 8 ай бұрын
Thanks for watching!😊
@M-xl1vx
@M-xl1vx 2 ай бұрын
Thats amazing
@Ishmeetsinghis
@Ishmeetsinghis 8 ай бұрын
Thanks, this is useful
@samvcodes
@samvcodes 8 ай бұрын
Thanks for watching!
@memeherp166
@memeherp166 8 ай бұрын
thank you, your channel make my go code more clean 😊
@samvcodes
@samvcodes 8 ай бұрын
Thank you for watching, I'm glad the videos are helping! :)
@afadeevz
@afadeevz 8 ай бұрын
Great and useful content as always
@samvcodes
@samvcodes 8 ай бұрын
Thanks again!
@konradpiotrowski9549
@konradpiotrowski9549 2 ай бұрын
Damn, this is nice. Imho it would be perfect if you finish it off with some dependency injection, specific error handler interface which is later on implemented by a few packages, http, grpc... So then when you switch from httperror to grpcerror you do it in one place by replacing the structure instead of refering to a different package everywhere :)
@user-fx5li1cy5t
@user-fx5li1cy5t 8 ай бұрын
Good job!
@samvcodes
@samvcodes 8 ай бұрын
Thanks! 😊
@matthewrawlings6763
@matthewrawlings6763 8 ай бұрын
This is a great pattern, I had recently just done something similar on a recent project
@samvcodes
@samvcodes 8 ай бұрын
Nothing better than handling errors nicely right!? - Thanks for watching :)
@TehGettinq
@TehGettinq 8 ай бұрын
I also end up doing similar stuff for generic-ish error handling. Sadly the only thing this truly highlight is how poorly designed go is.
@khushalgandhi5157
@khushalgandhi5157 5 ай бұрын
Which language do u think has better error handling ? What changes could be made in golang ?
@TehGettinq
@TehGettinq 5 ай бұрын
​@@khushalgandhi5157 i think pretty much all languages from the past 15years, rust would be a good mainstream example. As for what changes in golang well support for sum types would be the first thing for me. Then maybe optimized/release builds.
@khushalgandhi5157
@khushalgandhi5157 5 ай бұрын
@@TehGettinq I am a new golang dev working for past 5 months using fiber and gin framework Do u suggest any other language that I should learn on my own . I know the basics of Java
@TehGettinq
@TehGettinq 5 ай бұрын
@@khushalgandhi5157 That's great :) I would say 5 months is not a lot if you are new. If you enjoy golang I would say keep using it and make sure you understand and study all the technical details (go routines, channels, interfaces, packaging).. Then once you feel like you are extremely comfortable with these concepts you can move to something else!
@diehenne
@diehenne 5 ай бұрын
@TehGettinq Maybe I am missing something, but do you mean an option type that can return a Some or None, or what do you mean by sum type?
@Bashx1
@Bashx1 8 ай бұрын
Hey man, would you mind sharing your font and theme? Nice video. Thanks
@samvcodes
@samvcodes 8 ай бұрын
Thank you man! The theme is "gruvbox", but I also switch to "catppuccin". My go to font is "Monaco" :)
@Bashx1
@Bashx1 8 ай бұрын
Thank you very much. I will subscribe.Keep it up! @@samvcodes
@danimunf
@danimunf 8 ай бұрын
I'm kinda lost, why do you put errors.go under service? wouldn't it be more intuitive to put it under commons / helper? because it's the more generic error, am i right? thanka in advance
@Laflamablanca969
@Laflamablanca969 8 ай бұрын
Bruz… if that’s all you picked up from this video, you’ve got 100 problems
@filipboye-kofi3018
@filipboye-kofi3018 6 ай бұрын
depends, but pkg like commons and helpers are totaly useless name conventions saying nothing, in this case it could be at /service/errors/http.go when he was talking about rpc potentional implementing.
@justiceessiel6123
@justiceessiel6123 5 ай бұрын
Please make a video on mocking
@samvcodes
@samvcodes 5 ай бұрын
Will do!
@anhhaoam3379
@anhhaoam3379 6 ай бұрын
Where did you get the Join function? Because in errors there is no Join function.
@samvcodes
@samvcodes 6 ай бұрын
Hi! There is indeed a new `Join` method in the standard library `errors` package. pkg.go.dev/errors#example-Join But looking back on this code, error wrapping is probably nicer here. `fmt.Errorf("%w: %w", err1, err2)`.
@anhhaoam3379
@anhhaoam3379 6 ай бұрын
@@samvcodes tks
@justiceessiel6123
@justiceessiel6123 5 ай бұрын
Please drop the repo
@gouravkolhatkar3623
@gouravkolhatkar3623 8 ай бұрын
Mate we need full beginner friendly tutorial of go from you. Please 🚀❤.
@samvcodes
@samvcodes 8 ай бұрын
Coming soon😊 thanks!
@vikingthedude
@vikingthedude 8 ай бұрын
But also more intermediate/advanced stuff as well. Theres a huge supply of beginner content and not nearly enough more advanced content
@felixna5650
@felixna5650 8 ай бұрын
full beginner content is everywhere out there. intermediate stuff would be way more useful at this point
@oleksandr-petrovych
@oleksandr-petrovych 8 ай бұрын
get rid of the "SWITCH", it spoils everything
@samvcodes
@samvcodes 8 ай бұрын
Hello! Thanks for watching :) No need to use it if you'd like more control on an error by error basis from your services, however the switch for the HTTP codes is a mere util to return HTTP status codes from a server based on errors returned from your services - idea being saving repetitive response code handling code throughout your endpoints. Hopefully that makes sense :D
@oleksandr-petrovych
@oleksandr-petrovych 8 ай бұрын
@@samvcodes move the behavior to where it should be and do not write "SWITCH" where it is not needed
@clutchmadness
@clutchmadness 8 ай бұрын
Where is he supposed to move it? @@oleksandr-petrovych
@oleksandr-petrovych
@oleksandr-petrovych 8 ай бұрын
@@clutchmadness The second principle of SOLID or Open-closed Principle. If we add a new "Error" then we will have to modify this SWITCH as well. To avoid this necessity, use polymorphism instead of SWITCH. The behavior must be transferred to the method of the "Error" file, and instead of SWITCH, the method from these files will be called. It is necessary to save the condition of the presence of a certain method in each file, an interface can help with it.
@clutchmadness
@clutchmadness 8 ай бұрын
@@oleksandr-petrovych Thank you for explaining.
err != nil Is GOOD? (And Why)
7:19
ThePrimeTime
Рет қаралды 86 М.
Пробую самое сладкое вещество во Вселенной
00:41
Homemade Professional Spy Trick To Unlock A Phone 🔍
00:55
Crafty Champions
Рет қаралды 54 МЛН
ОДИН ДЕНЬ ИЗ ДЕТСТВА❤️ #shorts
00:59
BATEK_OFFICIAL
Рет қаралды 3,8 МЛН
#14 GROUP BY Nhiều Bảng Và Những Lưu Ý -- Sequelize v6
24:17
B21DCCN809- Lương Ngọc Yên
Рет қаралды 4
A Beautiful Way To Deal With ERRORS in Golang HTTP Handlers
8:42
Golang is OG?
5:16
Martin Baun
Рет қаралды 4,8 М.
The secret to making Golang error handling a breeze
13:46
Earthly
Рет қаралды 9 М.
Обработка ошибок в #Golang, до и после v1.20
21:29
THIS is the BEST Way to Write HTTP Services in Golang
13:53
How to EFFICIENTLY Handle Errors in Golang APIs
10:27
Tiago
Рет қаралды 4,2 М.
The Most Efficient Struct Configuration Pattern For Golang
11:10
Cadiz smart lock official account unlocks the aesthetics of returning home
0:30