1
0
mirror of https://github.com/golang/go synced 2024-11-27 05:21:22 -07:00

cmd/go/internal/module: in VersionError, do not wrap an existing ModuleError

VersionError wraps the given error in a ModuleError struct.

If the given error is already a ModuleError for the same path and
version, we now return it directly instead of wrapping.
This makes it safer to call VersionError if we don't know whether
a given error is already wrapped.

Updates #30748

Change-Id: I41b23f6c3ead0ec382e848696da51f478da1ad35
Reviewed-on: https://go-review.googlesource.com/c/go/+/189781
Run-TryBot: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Jay Conrod <jayconrod@google.com>
This commit is contained in:
Bryan C. Mills 2019-08-09 17:31:23 -04:00
parent 1581bb9843
commit 3d522b1088

View File

@ -50,8 +50,13 @@ type ModuleError struct {
Err error
}
// VersionError returns a ModuleError derived from a Version and error.
// VersionError returns a ModuleError derived from a Version and error,
// or err itself if it is already such an error.
func VersionError(v Version, err error) error {
var mErr *ModuleError
if errors.As(err, &mErr) && mErr.Path == v.Path && mErr.Version == v.Version {
return err
}
return &ModuleError{
Path: v.Path,
Version: v.Version,