-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.go
More file actions
74 lines (60 loc) · 1.54 KB
/
error.go
File metadata and controls
74 lines (60 loc) · 1.54 KB
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
package yammy
import (
"errors"
"fmt"
)
var _ error = (*wrappedError)(nil)
// Err is an error occurrs in yammy.
var Err = defineError("yammy error", nil)
// ErrIO is an error ralated to io.
var ErrIO = defineError("io error", Err)
// ErrYAML is an error related to YAML parsing.
var ErrYAML = defineError("yaml error", Err)
// ErrDirective is an error related to yammy directives.
var ErrDirective = defineError("directive error", Err)
// ErrVarNotFound is an error that means variables used in a
// YAML not found.
var ErrVarNotFound = defineError("variable not found", Err)
type wrappedError struct {
message string
parent *wrappedError
cause error
}
func defineError(message string, parent *wrappedError) *wrappedError {
return &wrappedError{
message: message,
parent: parent,
}
}
func (e *wrappedError) New(message string, cause error, args ...any) *wrappedError {
if len(args) != 0 {
message = fmt.Sprintf(message, args...)
}
return &wrappedError{
message: message,
parent: e,
cause: cause,
}
}
func (e *wrappedError) Is(other error) bool {
return e != nil && (e == other || errors.Is(e.parent, other) || errors.Is(e.cause, other))
}
func (e *wrappedError) Error() string {
ret := e.message
if e.cause != nil {
ret = fmt.Errorf("%s: %w", e.message, e.cause).Error()
}
if e.parent != nil {
ret = e.parent.message + ": " + ret
for p := e.parent.parent; p != nil; p = p.parent {
ret = ret + ": " + p.message
}
}
return ret
}
func (e *wrappedError) Unwrap() error {
if e == nil {
return nil
}
return e.cause
}