-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
113 lines (95 loc) · 2.53 KB
/
errors.go
File metadata and controls
113 lines (95 loc) · 2.53 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package errors
import (
"fmt"
"github.com/pkg/errors"
)
// Base does not implement causer.
// It's the base for custom errors defined with this package
type Base struct {
// msg is the error value
msg string
}
func (e Base) Error() string {
return e.msg
}
func NewCause(msg string) error {
return Base{msg: msg}
}
// EmptyCause for matching errors created without a cause
var EmptyCause = NewCause("")
// Custom type for defining errors
type Custom struct {
// code is the unique code for the error,
// it's also the format string used to create the msg
code string
// msg is the error value
msg string
// cause is an optional group for errors,
// use empty value if not applicable
cause string
}
// New creates a new error with msg
func New(msg string) error {
return Newf(msg)
}
// New creates a new error with formatted msg
func Newf(format string, a ...any) error {
return Custom{
code: format,
msg: fmt.Sprintf(format, a...),
cause: "",
}
}
// NewWithCause creates a new error with cause and msg
func NewWithCause(cause error, msg string) error {
return NewWithCausef(cause, msg)
}
// NewType creates a new error with cause and formatted msg
func NewWithCausef(cause error, format string, a ...any) error {
return Custom{
code: format,
msg: fmt.Sprintf(format, a...),
// Error message is the cause value
cause: cause.Error(),
}
}
func (e Custom) Error() string {
return e.msg
}
// Is returns true if, the type of the error is Custom,
// and code matches the format string passed into the constructor.
// Do not call errors.Wrap or WithStack when creating errors below
func (e Custom) Is(err error) bool {
v, ok := err.(Custom)
if !ok {
// Error not defined with this package
return false
}
// Code must match
return v.code == e.code
}
// Cause can be used to determine the cause of errors.
// Useful for grouping related errors,
// e.g. custom errors defined in the same package
func (e Custom) Cause() error {
return NewCause(e.cause)
}
// Code is the format string used to create the message value
func (e Custom) Code() string {
return e.code
}
// Is returns true for custom errors defined with this package,
// if the format code matches
func Is(err, target error) bool {
return errors.Is(err, target)
}
// WithStack may be used to annotate err with a stack trace,
// at the point where it is called.
// Don't use this when defining custom errors
func WithStack(err error) {
errors.WithStack(err)
}
// Cause returns the underlying cause of the error
func Cause(err error) error {
return errors.Cause(err)
}