mirror of
https://github.com/protocolbuffers/protobuf-go.git
synced 2025-01-18 13:11:16 +00:00
01b51b4f96
Add a sentinel proto.Error error which matches all errors returned by packages in this module. Document that protoregistry.NotFound is an exact sentinel value for performance reasons. Add a Wrap function to the internal/errors package and use it to wrap errors from outside sources (resolvers). Wrapped errors match proto.Error. Fixes golang/protobuf#1021. Change-Id: I45567df3fd6c8dc9a5caafdb55654827f6fb1941 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/215338 Reviewed-by: Joe Tsai <joetsai@google.com>
43 lines
955 B
Go
43 lines
955 B
Go
// Copyright 2020 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
// +build !go1.13
|
|
|
|
package errors
|
|
|
|
import "reflect"
|
|
|
|
// Is is a copy of Go 1.13's errors.Is for use with older Go versions.
|
|
func Is(err, target error) bool {
|
|
if target == nil {
|
|
return err == target
|
|
}
|
|
|
|
isComparable := reflect.TypeOf(target).Comparable()
|
|
for {
|
|
if isComparable && err == target {
|
|
return true
|
|
}
|
|
if x, ok := err.(interface{ Is(error) bool }); ok && x.Is(target) {
|
|
return true
|
|
}
|
|
// TODO: consider supporing target.Is(err). This would allow
|
|
// user-definable predicates, but also may allow for coping with sloppy
|
|
// APIs, thereby making it easier to get away with them.
|
|
if err = unwrap(err); err == nil {
|
|
return false
|
|
}
|
|
}
|
|
}
|
|
|
|
func unwrap(err error) error {
|
|
u, ok := err.(interface {
|
|
Unwrap() error
|
|
})
|
|
if !ok {
|
|
return nil
|
|
}
|
|
return u.Unwrap()
|
|
}
|