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

preview_player
Показать описание
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.

Рекомендации по теме
Комментарии
Автор

Jesus dude I’ve been looking for Go content like this. I’m going to binge watch all your videos. Keep it going.

matthew
Автор

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.

MatthiasFasching
Автор

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.

esra_erimez
Автор

So good! Thanks for being the senior I don't have

cole.maxwell
Автор

how do you handle db transaction in service pattern? like using 2 separate services in the handler function.

hectorcast
Автор

Thanks for the video will implement it in a current project

rampandey
Автор

This is a great pattern, I had recently just done something similar on a recent project

matthewrawlings
Автор

Hey man, would you mind sharing your font and theme? Nice video. Thanks

Bashx
Автор

thank you, your channel make my go code more clean 😊

memeherp
Автор

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.

TehGettinq
Автор

Where did you get the Join function? Because in errors there is no Join function.

anhhaoam
Автор

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

danimunf
Автор

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 :)

konradpiotrowski
Автор

Mate we need full beginner friendly tutorial of go from you. Please 🚀❤.

gouravkolhatkar
Автор

So much code and you are still going to end up with thousand lines of "if err != nill"
When people are asking for better error handling I am not sure that we worry about how to transfer error details, but rather how to make the code cleaner and less repetitive.

nooblero
Автор

get rid of the "SWITCH", it spoils everything

oleksandr-petrovych