Skip to content

Commit

Permalink
add API to make it easier to work with ErrorDetail if you have/want a…
Browse files Browse the repository at this point in the history
…n Any (#409)

If your code already has an instance of `Any`, it's a pain (and not even
guaranteed to be possible at runtime!) to unmarshal the message therein
only to re-wrap it in order to put it in an `ErrorDetail`.

Similarly, if you can't unmarshal the contained type, it may be useful
to access the `Any` envelope.

This adds two new APIs for just this.
  • Loading branch information
jhump authored Dec 1, 2022
1 parent cbfd23f commit e9ebb9c
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion error.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,14 @@ type ErrorDetail struct {
wireJSON string // preserve human-readable JSON
}

// NewErrorDetail constructs a new error detail.
// NewErrorDetail constructs a new error detail. If msg is an *[anypb.Any] then
// it is used as is. Otherwise, it is first marshalled into an *[anypb.Any]
// value. This returns an error if msg cannot be marshalled.
func NewErrorDetail(msg proto.Message) (*ErrorDetail, error) {
// If it's already an Any, don't wrap it inside another.
if pb, ok := msg.(*anypb.Any); ok {
return &ErrorDetail{pb: pb}, nil
}
pb, err := anypb.New(msg)
if err != nil {
return nil, err
Expand Down

0 comments on commit e9ebb9c

Please sign in to comment.