|
| 1 | +//go:build ignore |
| 2 | + |
| 3 | +// Copyright 2018 The Go Authors. All rights reserved. |
| 4 | +// Use of this source code is governed by a BSD-style |
| 5 | +// license that can be found in the LICENSE file. |
| 6 | + |
| 7 | +package module |
| 8 | + |
| 9 | +import ( |
| 10 | + "errors" |
| 11 | + "fmt" |
| 12 | +) |
| 13 | + |
| 14 | +// A ModuleError indicates an error specific to a module. |
| 15 | +type ModuleError struct { |
| 16 | + Path string |
| 17 | + Version string |
| 18 | + Err error |
| 19 | +} |
| 20 | + |
| 21 | +// VersionError returns a ModuleError derived from a Version and error, |
| 22 | +// or err itself if it is already such an error. |
| 23 | +func VersionError(v Version, err error) error { |
| 24 | + var mErr *ModuleError |
| 25 | + if errors.As(err, &mErr) && mErr.Path == v.Path && mErr.Version == v.Version { |
| 26 | + return err |
| 27 | + } |
| 28 | + return &ModuleError{ |
| 29 | + Path: v.Path, |
| 30 | + Version: v.Version, |
| 31 | + Err: err, |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +func (e *ModuleError) Error() string { |
| 36 | + if v, ok := e.Err.(*InvalidVersionError); ok { |
| 37 | + return fmt.Sprintf("%s@%s: invalid %s: %v", e.Path, v.Version, v.noun(), v.Err) |
| 38 | + } |
| 39 | + if e.Version != "" { |
| 40 | + return fmt.Sprintf("%s@%s: %v", e.Path, e.Version, e.Err) |
| 41 | + } |
| 42 | + return fmt.Sprintf("module %s: %v", e.Path, e.Err) |
| 43 | +} |
| 44 | + |
| 45 | +func (e *ModuleError) Unwrap() error { return e.Err } |
| 46 | + |
| 47 | +// An InvalidVersionError indicates an error specific to a version, with the |
| 48 | +// module path unknown or specified externally. |
| 49 | +// |
| 50 | +// A ModuleError may wrap an InvalidVersionError, but an InvalidVersionError |
| 51 | +// must not wrap a ModuleError. |
| 52 | +type InvalidVersionError struct { |
| 53 | + Version string |
| 54 | + Pseudo bool |
| 55 | + Err error |
| 56 | +} |
| 57 | + |
| 58 | +// noun returns either "version" or "pseudo-version", depending on whether |
| 59 | +// e.Version is a pseudo-version. |
| 60 | +func (e *InvalidVersionError) noun() string { |
| 61 | + if e.Pseudo { |
| 62 | + return "pseudo-version" |
| 63 | + } |
| 64 | + return "version" |
| 65 | +} |
| 66 | + |
| 67 | +func (e *InvalidVersionError) Error() string { |
| 68 | + return fmt.Sprintf("%s %q invalid: %s", e.noun(), e.Version, e.Err) |
| 69 | +} |
| 70 | + |
| 71 | +func (e *InvalidVersionError) Unwrap() error { return e.Err } |
| 72 | + |
| 73 | +// An InvalidPathError indicates a module, import, or file path doesn't |
| 74 | +// satisfy all naming constraints. See CheckPath, CheckImportPath, |
| 75 | +// and CheckFilePath for specific restrictions. |
| 76 | +type InvalidPathError struct { |
| 77 | + Kind string // "module", "import", or "file" |
| 78 | + Path string |
| 79 | + Err error |
| 80 | +} |
| 81 | + |
| 82 | +func (e *InvalidPathError) Error() string { |
| 83 | + return fmt.Sprintf("malformed %s path %q: %v", e.Kind, e.Path, e.Err) |
| 84 | +} |
| 85 | + |
| 86 | +func (e *InvalidPathError) Unwrap() error { return e.Err } |
0 commit comments