Re: [go-nuts] How can I check error types of gRPC calls?

2021-05-19 Thread burak serdar
Here is something I did to pass custom error information:

Based on the grpc code, if an error type has GRPCStatus()
*status.Status function, then grpc gets the status information using
that method. For the error types that can go through grpc, I have this
method below:

func (e MyError) GRPCStatus() *status.Status {
var code codes.Code
switch e.HTTPStatus {
  case http.StatusBadRequest:
code = codes.InvalidArgument
  case http.StatusUnauthorized:
 code = codes.Unauthenticated
  default:
 code = codes.Unknown
 }
 x, _ := json.Marshal(e)
 return status.New(code, string(x))
}


In this example, the error type includes an HTTP status code, and the
grpc status is decided based on that. The grpc error contains a JSON
marshaled error information.

On the receiving end, I have this function:

func FromGRPCError(err error) (MyError, bool) {
  if x, ok := status.FromError(err); ok {
var e MyError
if json.Unmarshal([]byte(x.Message()), ) == nil {
return e, true
   }
  }
  return MyError{}, false
}

This decodes the error information from the incoming error.

This scheme can be extended to deal with multiple error types.

On Tue, May 18, 2021 at 8:45 AM 'Axel Wagner' via golang-nuts
 wrote:
>
> You cant to use status.Code(err), which gives you the gRPC status code.
>
>
> On Tue, May 18, 2021 at 3:08 PM cpu...@gmail.com  wrote:
>>
>>
>> Thank you for the pointer. After looking at the API, I'm still confused how 
>> to use it. I can status.Convert(err) to get a status object but couldn't 
>> really figure how how to use that to extract the actual underlying base 
>> error?
>> On Saturday, May 15, 2021 at 2:12:28 PM UTC+2 kortschak wrote:
>>>
>>> On Sat, 2021-05-15 at 04:47 -0700, cpu...@gmail.com wrote:
>>> > In my local code, I'm using things like
>>> >
>>> > if errors.Is(err, api.ErrMustRetry) { ... }
>>> >
>>> > How would I achieve the same on errors returned by the gRCP
>>> > interface? I've noticed these are wrapped:
>>> >
>>> > rpc error: code = Unknown desc = must retry rpc error: code = Unknown
>>> > desc = must retry
>>> >
>>> > I assume the errors package won't work here as type information is
>>> > not carried across gRPC: What is the best practice here: unwrap the
>>> > root error from the RPC result (how) and perform string comparison?
>>> >
>>>
>>> There is the status package which provides tools for examining the gRPC
>>> errors, https://pkg.go.dev/google.golang.org/grpc/status.
>>>
>>>
>> --
>> 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/8ecd4024-a846-4657-8e61-15c16289ccd2n%40googlegroups.com.
>
> --
> 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/CAEkBMfERsv28%2BJV-EVYqhZraHbyYmPftQV6V0i55zVaK1R13HQ%40mail.gmail.com.

-- 
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/CAMV2RqrL%3DkqGH2_RSpKuF8ppT-JCfGL9XSFkE63WRX8Eraw3OQ%40mail.gmail.com.


Re: [go-nuts] How can I check error types of gRPC calls?

2021-05-18 Thread 'Axel Wagner' via golang-nuts
*want, sorry, typo :)

On Tue, May 18, 2021 at 4:44 PM Axel Wagner 
wrote:

> You cant to use status.Code(err)
> , which gives you
> the gRPC status code.
>
>
> On Tue, May 18, 2021 at 3:08 PM cpu...@gmail.com 
> wrote:
>
>>
>> Thank you for the pointer. After looking at the API, I'm still confused
>> how to use it. I can status.Convert(err) to get a status object but
>> couldn't really figure how how to use that to extract the actual underlying
>> base error?
>> On Saturday, May 15, 2021 at 2:12:28 PM UTC+2 kortschak wrote:
>>
>>> On Sat, 2021-05-15 at 04:47 -0700, cpu...@gmail.com wrote:
>>> > In my local code, I'm using things like
>>> >
>>> > if errors.Is(err, api.ErrMustRetry) { ... }
>>> >
>>> > How would I achieve the same on errors returned by the gRCP
>>> > interface? I've noticed these are wrapped:
>>> >
>>> > rpc error: code = Unknown desc = must retry rpc error: code = Unknown
>>> > desc = must retry
>>> >
>>> > I assume the errors package won't work here as type information is
>>> > not carried across gRPC: What is the best practice here: unwrap the
>>> > root error from the RPC result (how) and perform string comparison?
>>> >
>>>
>>> There is the status package which provides tools for examining the gRPC
>>> errors, https://pkg.go.dev/google.golang.org/grpc/status.
>>>
>>>
>>> --
>> 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/8ecd4024-a846-4657-8e61-15c16289ccd2n%40googlegroups.com
>> 
>> .
>>
>

-- 
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/CAEkBMfEk%3DvhxW%2BY7MV0TdKX_pk6GCcDB7XNc9KTPQWyr_AZmdg%40mail.gmail.com.


Re: [go-nuts] How can I check error types of gRPC calls?

2021-05-18 Thread 'Axel Wagner' via golang-nuts
You cant to use status.Code(err)
, which gives you
the gRPC status code.


On Tue, May 18, 2021 at 3:08 PM cpu...@gmail.com  wrote:

>
> Thank you for the pointer. After looking at the API, I'm still confused
> how to use it. I can status.Convert(err) to get a status object but
> couldn't really figure how how to use that to extract the actual underlying
> base error?
> On Saturday, May 15, 2021 at 2:12:28 PM UTC+2 kortschak wrote:
>
>> On Sat, 2021-05-15 at 04:47 -0700, cpu...@gmail.com wrote:
>> > In my local code, I'm using things like
>> >
>> > if errors.Is(err, api.ErrMustRetry) { ... }
>> >
>> > How would I achieve the same on errors returned by the gRCP
>> > interface? I've noticed these are wrapped:
>> >
>> > rpc error: code = Unknown desc = must retry rpc error: code = Unknown
>> > desc = must retry
>> >
>> > I assume the errors package won't work here as type information is
>> > not carried across gRPC: What is the best practice here: unwrap the
>> > root error from the RPC result (how) and perform string comparison?
>> >
>>
>> There is the status package which provides tools for examining the gRPC
>> errors, https://pkg.go.dev/google.golang.org/grpc/status.
>>
>>
>> --
> 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/8ecd4024-a846-4657-8e61-15c16289ccd2n%40googlegroups.com
> 
> .
>

-- 
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/CAEkBMfERsv28%2BJV-EVYqhZraHbyYmPftQV6V0i55zVaK1R13HQ%40mail.gmail.com.


Re: [go-nuts] How can I check error types of gRPC calls?

2021-05-18 Thread Brian Candler
It's an alias 
 for 
this:
https://pkg.go.dev/google.golang.org/grpc/internal/status#Status
So it should have Code() and Detail() methods.

-- 
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/04b38691-532a-4cab-a3a6-48f3546c00e1n%40googlegroups.com.


Re: [go-nuts] How can I check error types of gRPC calls?

2021-05-18 Thread cpu...@gmail.com

Thank you for the pointer. After looking at the API, I'm still confused how 
to use it. I can status.Convert(err) to get a status object but couldn't 
really figure how how to use that to extract the actual underlying base 
error?
On Saturday, May 15, 2021 at 2:12:28 PM UTC+2 kortschak wrote:

> On Sat, 2021-05-15 at 04:47 -0700, cpu...@gmail.com wrote:
> > In my local code, I'm using things like
> >
> > if errors.Is(err, api.ErrMustRetry) { ... }
> >
> > How would I achieve the same on errors returned by the gRCP
> > interface? I've noticed these are wrapped:
> >
> > rpc error: code = Unknown desc = must retry rpc error: code = Unknown
> > desc = must retry
> >
> > I assume the errors package won't work here as type information is
> > not carried across gRPC: What is the best practice here: unwrap the
> > root error from the RPC result (how) and perform string comparison?
> >
>
> There is the status package which provides tools for examining the gRPC
> errors, https://pkg.go.dev/google.golang.org/grpc/status.
>
>
>

-- 
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/8ecd4024-a846-4657-8e61-15c16289ccd2n%40googlegroups.com.


Re: [go-nuts] How can I check error types of gRPC calls?

2021-05-15 Thread 'Axel Wagner' via golang-nuts
Generally, I would argue that the grpc package maintainers should, at some
point, support native wrapping of errors.
In the meantime, they are providing this package:
https://pkg.go.dev/google.golang.org/grpc/status
It predates (by a long time) the errors.Is APIs, which is why I assume they
haven't yet implemented support

On Sat, May 15, 2021 at 1:47 PM cpu...@gmail.com  wrote:

> In my local code, I'm using things like
>
> if errors.Is(err, api.ErrMustRetry) { ... }
>
> How would I achieve the same on errors returned by the gRCP interface?
> I've noticed these are wrapped:
>
> rpc error: code = Unknown desc = must retry rpc error: code = Unknown desc
> = must retry
>
> I assume the errors package won't work here as type information is not
> carried across gRPC: What is the best practice here: unwrap the root error
> from the RPC result (how) and perform string comparison?
>
> Thanks,
> Andreas
>
> --
> 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/c3053cb0-d739-496d-a804-7f533ba7a6cbn%40googlegroups.com
> 
> .
>

-- 
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/CAEkBMfEZ1Ge53nrLAZ7h_tJqrXRTUaBYZg7EV7yi7wzEGwC5Qw%40mail.gmail.com.


Re: [go-nuts] How can I check error types of gRPC calls?

2021-05-15 Thread 'Dan Kortschak' via golang-nuts
On Sat, 2021-05-15 at 04:47 -0700, cpu...@gmail.com wrote:
> In my local code, I'm using things like
>
> if errors.Is(err, api.ErrMustRetry) { ... }
>
> How would I achieve the same on errors returned by the gRCP
> interface? I've noticed these are wrapped:
>
> rpc error: code = Unknown desc = must retry rpc error: code = Unknown
> desc = must retry
>
> I assume the errors package won't work here as type information is
> not carried across gRPC: What is the best practice here: unwrap the
> root error from the RPC result (how) and perform string comparison?
>

There is the status package which provides tools for examining the gRPC
errors, https://pkg.go.dev/google.golang.org/grpc/status.


-- 
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/1ab47e32ab024c2637e199d85b49601b35e54c52.camel%40kortschak.io.