diff options
| author | Carlos Alexandro Becker <caarlos0@gmail.com> | 2020-09-13 02:12:02 +0000 |
|---|---|---|
| committer | Emmanuel Odeke <emm.odeke@gmail.com> | 2020-09-13 03:19:24 +0000 |
| commit | 95bb00d1088767ed14e3bd1a5f533a690d619a5f (patch) | |
| tree | 8f1d284be08a69c25adf7d3280034044b8dc22a9 /src/encoding/json/decode.go | |
| parent | b3ef90ec7304a28b89f616ced20b09f56be30cc4 (diff) | |
| download | go-95bb00d1088767ed14e3bd1a5f533a690d619a5f.tar.xz | |
encoding/json: implement Is on all errors
Allows users to check:
errors.Is(err, &UnmarshalTypeError{})
errors.Is(err, &UnmarshalFieldError{})
errors.Is(err, &InvalidUnmarshalError{})
errors.Is(err, &UnsupportedValueError{})
errors.Is(err, &MarshalerError{})
which is the recommended way of checking for kinds of errors.
SyntaxError.Is was implemented in CL 253037.
As and Unwrap relevant methods will be added in future CLs.
Change-Id: I1f8a503b8fdc0f3afdfe9669a91f3af8d960e028
GitHub-Last-Rev: 930cda5384c987a0b31f277ba3b4ab690ea74ac3
GitHub-Pull-Request: golang/go#41360
Reviewed-on: https://go-review.googlesource.com/c/go/+/254537
Run-TryBot: Emmanuel Odeke <emm.odeke@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com>
Trust: Emmanuel Odeke <emm.odeke@gmail.com>
Diffstat (limited to 'src/encoding/json/decode.go')
| -rw-r--r-- | src/encoding/json/decode.go | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/src/encoding/json/decode.go b/src/encoding/json/decode.go index 86d8a69db7..1b006ffb17 100644 --- a/src/encoding/json/decode.go +++ b/src/encoding/json/decode.go @@ -136,6 +136,12 @@ func (e *UnmarshalTypeError) Error() string { return "json: cannot unmarshal " + e.Value + " into Go value of type " + e.Type.String() } +// Is returns true if target is a UnmarshalTypeError. +func (e *UnmarshalTypeError) Is(target error) bool { + _, ok := target.(*UnmarshalTypeError) + return ok +} + // An UnmarshalFieldError describes a JSON object key that // led to an unexported (and therefore unwritable) struct field. // @@ -150,12 +156,24 @@ func (e *UnmarshalFieldError) Error() string { return "json: cannot unmarshal object key " + strconv.Quote(e.Key) + " into unexported field " + e.Field.Name + " of type " + e.Type.String() } +// Is returns true if target is a UnmarshalFieldError. +func (e *UnmarshalFieldError) Is(target error) bool { + _, ok := target.(*UnmarshalFieldError) + return ok +} + // An InvalidUnmarshalError describes an invalid argument passed to Unmarshal. // (The argument to Unmarshal must be a non-nil pointer.) type InvalidUnmarshalError struct { Type reflect.Type } +// Is returns true if target is a InvalidUnmarshalError. +func (e *InvalidUnmarshalError) Is(target error) bool { + _, ok := target.(*InvalidUnmarshalError) + return ok +} + func (e *InvalidUnmarshalError) Error() string { if e.Type == nil { return "json: Unmarshal(nil)" |
