Indeed I learned this from https://blog.golang.org/defer-panic-and-recover 
and they mention this is done in the json module. And I couldn't adhere 
more. It's basically the same as try-catch from other languages. 

Doing this systematically in a Rest server for errors of type 502, 400, 404 
(in the sens route exists but not the resource with the given id) etc is 
still considered a good thing by go programmers?

On Thursday, October 8, 2020 at 5:18:11 AM UTC+2 skinne...@gmail.com wrote:

> This looks like something I might have done. However, my preference would 
> be to write a wrapper MustGetParameter receives string field, calls 
> store.GetParameter(field), returns parameter or panics. 
>
> You may then use
>     defer func() { globalErr = recover()} 
>     tenantID, err1 := MustGetParameter("TENANT_ID")
>     clientID, err2 := MustGetParameter("CLIENT_ID")
>     clientSecret, err3 := MustGetParameter("CLIENT_SECRET")
>
>     return connection{
>         tenantID,
>         clientID,
>         clientSecret,
>     }, globalErr
>
> If an error can be handled, it should be handled, but if you are just 
> cascading the reports of errors, use defer recover as a try and let the 
> calling function handle it. Of course some panics really should be a panic 
> so you might want to have your defer func check the panic error to see if 
> it is expected or something that really is a panic.
>
> When I do an API I may have a Doit and a MustDoit, one returns an error 
> and the other panics. My way of keeping the code clean unless I can 
> actually do something about the error.
>
> On Wednesday, October 7, 2020 at 1:36:07 PM UTC-5 johan.ma...@dexyos.fr 
> wrote:
>
>> Hi, I'm looking for thoughts from some experienced go programmers on a 
>> technique to reduce error handling verbosity. 
>>
>> The basic idea is to be optimistic over a few instructions, and then 
>> combine many errors in to one. This gist and explains the idea (note the 
>> absence of if err != nil {} )
>>
>>         tenantID, err1 := store.GetParameter("TENANT_ID")
>> clientID, err2 := store.GetParameter("CLIENT_ID")
>> clientSecret, err3 := store.GetParameter("CLIENT_SECRET")
>>
>> globalErr := multierr.Combine(err1, err2, err3)
>> return connection{
>> tenantID,
>> clientID,
>> clientSecret,
>> }, globalErr
>>
>> There's some more detail in a post 
>> http://martinsson-johan.blogspot.com/2020/10/less-error-handling-noice-in-go.html.
>>  
>> I'm sure someone else has already proposed this, but I wasn't able to find 
>> it. Grateful for pointers
>>  
>> While it seems sound to me I'm a very interested in what people from the 
>> community thinks.
>>
>> Cheers
>> Johan Martinsson
>>
>>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/2254efb0-be1d-4e05-bbad-4602317ba078n%40googlegroups.com.

Reply via email to