mirror of
https://github.com/golang/go.git
synced 2025-11-09 05:01:01 +00:00
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: 930cda5384
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>
This commit is contained in:
parent
b3ef90ec73
commit
95bb00d108
4 changed files with 84 additions and 1 deletions
|
|
@ -245,6 +245,12 @@ func (e *UnsupportedValueError) Error() string {
|
|||
return "json: unsupported value: " + e.Str
|
||||
}
|
||||
|
||||
// Is returns true if target is a UnsupportedValueError.
|
||||
func (e *UnsupportedValueError) Is(target error) bool {
|
||||
_, ok := target.(*UnsupportedValueError)
|
||||
return ok
|
||||
}
|
||||
|
||||
// Before Go 1.2, an InvalidUTF8Error was returned by Marshal when
|
||||
// attempting to encode a string value with invalid UTF-8 sequences.
|
||||
// As of Go 1.2, Marshal instead coerces the string to valid UTF-8 by
|
||||
|
|
@ -279,6 +285,12 @@ func (e *MarshalerError) Error() string {
|
|||
// Unwrap returns the underlying error.
|
||||
func (e *MarshalerError) Unwrap() error { return e.Err }
|
||||
|
||||
// Is returns true if target is a MarshalerError.
|
||||
func (e *MarshalerError) Is(target error) bool {
|
||||
_, ok := target.(*MarshalerError)
|
||||
return ok
|
||||
}
|
||||
|
||||
var hex = "0123456789abcdef"
|
||||
|
||||
// An encodeState encodes JSON into a bytes.Buffer.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue