Skip to content
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

Add example of addings details to errors #1915

Merged
merged 5 commits into from
Mar 21, 2018
Merged

Add example of addings details to errors #1915

merged 5 commits into from
Mar 21, 2018

Conversation

enocom
Copy link
Contributor

@enocom enocom commented Mar 13, 2018

Fixes #478

@enocom
Copy link
Contributor Author

enocom commented Mar 13, 2018

This is an update of #506 using status.WithDetails and status.Details on the server and client respectively.

@dfawley dfawley self-assigned this Mar 15, 2018
@dfawley dfawley self-requested a review March 15, 2018 20:12
Copy link
Member

@dfawley dfawley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this is great!

I think the "usage" part of the doc should ideally go into the Documentation directory under grpc-go. We don't have any documentation about errors/statuses yet, but a single doc to cover the basics and also adding details would be great. If you have time, please expand on this and add something there. Otherwise, we can leave it here for now and file a bug to make sure we eventually cover errors in the main docs and move this section into it.

Output:

```
2018/03/12 19:39:35 ResourceInfo details: violations:<subject:"name:world" description:"Limit one greeting per person" >
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"ResourceInfo"? I think the code has been changed since this was generated.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. I'll fix this as well.

if err != nil {
s := status.Convert(err)
for _, d := range s.Details() {
info := d.(*epb.QuotaFailure)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better practice would be to check ", ok" of the type assertion, or do a type switch:

switch info := d.(type) {
  case *epb.QuotaFailure:
    log.Printf("Error details: %s", info)
  }
  // case ....
}

(Interestingly, though, the type switch isn't necessary if you're just logging it.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I'll update the example to include the ok on the type assertion.

@dfawley dfawley assigned enocom and unassigned dfawley Mar 19, 2018
@enocom
Copy link
Contributor Author

enocom commented Mar 19, 2018

@dfawley Thank you for the review. I'll expand on the usage as suggested and move the addition to the Documentation directory.

@enocom
Copy link
Contributor Author

enocom commented Mar 20, 2018

@dfawley I've made the discussed updates. Let me know if you would like me to expand on the documentation.

@dfawley dfawley assigned dfawley and unassigned enocom Mar 20, 2018

gRPC servers return a single `error` in response to client requests. The error
value contains a code and a description. In some cases, though, it may be
necessary to add details about the particular error. gRPC includes a
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes it sound like gRPC has status.Status only because of the details. All servers must return errors implemented by the status package or else clients will only see unknown status codes.

Would you mind renaming this file to rpc-status.md or rpc-errors.md and either:

  1. Add something to the effect of: All service method handlers should return nil or status errors (or be coerced to UNKNOWN); clients will see those ~directly. Maybe an example of how to use it (status.Error(codes.Whatever, "description")) as well.
  2. Leave a placeholder for us to fill in later.

Either way, "Error Details" should ideally be just a sub-section of a bigger "Errors" doc.

Copy link
Contributor Author

@enocom enocom Mar 20, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think rpc-errors.md sounds good. I'll make the changes and update the PR, adding an initial, general discussion of errors.

@enocom
Copy link
Contributor Author

enocom commented Mar 21, 2018

@dfawley There are likely additional improvements to the wording. What do you think?

Copy link
Member

@dfawley dfawley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, thanks!

@dfawley dfawley merged commit 5415d18 into grpc:master Mar 21, 2018
@enocom enocom deleted the error-details branch March 21, 2018 16:05
@menghanl menghanl added the Type: Documentation Documentation or examples label Mar 27, 2018
@menghanl menghanl added this to the 1.11 Release milestone Mar 27, 2018
@lock lock bot locked as resolved and limited conversation to collaborators Sep 26, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Type: Documentation Documentation or examples
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants