-
Notifications
You must be signed in to change notification settings - Fork 210
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 (#251)
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 packages using newer Go versions and already relying on the errors package to get rid of the transitive dependency on golang.org/x/xerrors.
- Loading branch information
Showing
3 changed files
with
33 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,15 @@ | ||
// 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) | ||
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,18 @@ | ||
// 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 | ||
|
||
// TODO(≥go1.13): For support on <go1.13, we use the xerrors package. | ||
// Drop this file when we no longer support older Go versions. | ||
|
||
package cmpopts | ||
|
||
import "golang.org/x/xerrors" | ||
|
||
func compareErrors(x, y interface{}) bool { | ||
xe := x.(error) | ||
ye := y.(error) | ||
return xerrors.Is(xe, ye) || xerrors.Is(ye, xe) | ||
} |