-
Notifications
You must be signed in to change notification settings - Fork 209
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmp/cmpopts: use errors.Is with ≥go1.13 in compareErrors
Use the standard definition of errors.Is to implement compareErrors with ≥go1.13. Retain the implementation using golang.org/x/xerrors for versions <go1.13. This will allow people using newer Go versions to get rid of the transitive dependency on golang.org/x/xerrors.
- Loading branch information
Showing
3 changed files
with
32 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Copyright 2021, 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 cmpopts | ||
|
||
import "errors" | ||
|
||
func compareErrors(x, y interface{}) bool { | ||
xe := x.(error) | ||
ye := y.(error) | ||
// Use standard definition of errors.Is with ≥go1.13. | ||
return errors.Is(xe, ye) || errors.Is(ye, xe) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Copyright 2021, 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 cmpopts | ||
|
||
import "golang.org/x/xerrors" | ||
|
||
func compareErrors(x, y interface{}) bool { | ||
xe := x.(error) | ||
ye := y.(error) | ||
// Use xerrors with <go1.13. | ||
return xerrors.Is(xe, ye) || xerrors.Is(ye, xe) | ||
} |