-
Notifications
You must be signed in to change notification settings - Fork 80
/
errors.go
83 lines (68 loc) · 1.71 KB
/
errors.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package jar
import (
"errors"
"fmt"
)
// LocalError is a type for errors returned from this package.
type localError struct {
inner error
msg string
}
// These are sentinel errors that can be used with errors.Is.
var (
ErrUnidentified = errors.New("unidentified jar")
ErrNotAJar = errors.New("does not seem to be a jar")
)
func (e *localError) Error() string {
switch {
case e.inner == nil && e.msg == "":
panic("programmer error: no error or message")
case e.inner == nil && e.msg != "":
return "jar: " + e.msg
case e.inner != nil && e.msg == "":
return fmt.Sprintf("jar: %v", e.inner)
case e.inner != nil && e.msg != "":
return fmt.Sprintf("jar: %s: %v", e.msg, e.inner)
}
panic("unreachable")
}
func (e *localError) Unwrap() error {
return e.inner
}
func mkErr(msg string, err error) *localError {
return &localError{msg: msg, inner: err}
}
func archiveErr(m srcPath, err error) *localError {
return &localError{
msg: fmt.Sprintf("at %q", m.String()),
inner: err,
}
}
type errUnidentified struct {
name string
}
func unidentified(n string) *errUnidentified {
return &errUnidentified{n}
}
func (e *errUnidentified) Is(target error) bool {
return target == ErrUnidentified || target == e
}
func (e *errUnidentified) Error() string {
return fmt.Sprintf("unidentified jar: %s", e.name)
}
type errNotAJar struct {
inner error
name string
}
func notAJar(p srcPath, reason error) *errNotAJar {
return &errNotAJar{name: p.String(), inner: reason}
}
func (e *errNotAJar) Error() string {
return fmt.Sprintf("%q not a jar: %v", e.name, e.inner)
}
func (e *errNotAJar) Unwrap() error {
return e.inner
}
func (e *errNotAJar) Is(target error) bool {
return target == ErrNotAJar || target == e
}