-
Notifications
You must be signed in to change notification settings - Fork 4.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support error matching (errors.Is) and error wrapping (status.Errorf("%w")) #3616
Comments
This seems to be dup of #3115. |
@dfawley : Thoughts? |
We cannot wrap errors inside a status, since they may not be serializable, as mentioned in #3115. |
@dfawley grpc-go/internal/status/status.go Lines 61 to 69 in 6c9e30c
I suggested that we might want to keep track of the error in Nevertheless, my proposal was to make |
Right, and because it cannot round-trip, this wrapping will not be allowed. You can use the If you really wanted, you could make your own type, compatible with our
This would be compatible with the gRPC library -- i.e. your service handlers could return an error of this type and the |
I understand why we want
Maybe you are thinking that my feature request is to magically tunnel a wrapped server error to the client? This is not what I’m asking for. Obviously, wrapped errors cannot survive the trip between the server and the client: no Go program can send errors across process boundaries. To clarify, if this proposal were implemented, we should add doc comments to explain that if a client calls This feature request is for the gRPC library to support wrapped errors within the same process. This is useful for:
Unfortunately, this does not solve the problem listed in the description. Even if I made a custom error type, In our case, the client wants to distinguish between a |
In this case you should be able to simply test |
These are not intended use cases for status errors. There are other ways to do what you want. E.g. you can return any custom error type from your method handlers if you have an interceptor that needs to interpret them.
This sounds more like #3328. Since it is not possible to distinguish these cases, you could make your server return a different status code -- one the gRPC library cannot produce: https://github.com/grpc/grpc/blob/master/doc/statuscodes.md Or, as @puellanivis suggested, you could check the context directly in this case. |
This feature request is related to #2934, but does not suggest that
status.Status
implement theerror
interface.Use case(s) - what problem will this feature solve?
internal/status.Errorf
,status.Errorf
, functions wrap an error with its text message usingfmt.Sprintf
:grpc-go/internal/status/status.go
Lines 66 to 69 in 42e450f
grpc-go/status/status.go
Lines 61 to 64 in 754ee59
Unfortunately, by discarding the error, it makes
errors.Is
tests impossible to use:Proposed Solution
internal/status.Error
should implement theerrors.Unwrap
interface:Implementing
errors.Is
might be a bit subtle, because the existing behavior only cares that the*spb.Status
field contain the same protobuf representations:Obviously,
status.Errorf
should support the %w verb to matchfmt.Errorf
, such that:This means that
internal/status.Errorf
needs to properly wrap the error:and that
status.Errorf
should delegate to this function:Embedding errors in status.Status
Note that uses of
status.FromError
andstatus.FromContextError
probably need to be slightly rewritten. For example:grpc-go/clientconn.go
Lines 546 to 563 in 4eb418e
needs to be rewritten so that:
However, this seems a bit clunky because
internal/status.Status.Err
creates a new error each time.We might consider embedding an error in
internal/status.Status
if it was created from an error:The text was updated successfully, but these errors were encountered: