-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror_code.go
More file actions
67 lines (57 loc) · 1.72 KB
/
error_code.go
File metadata and controls
67 lines (57 loc) · 1.72 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
package ppcerrors
import "strings"
type (
// ErrorCoder interface defines the methods that an error code must implement.
ErrorCoder interface {
Name() string
Code() int
Msg() string
}
// errorCode defines an error with a name, code, and message.
errorCode struct {
name string
code int
msg string
}
)
// NewErrorCode creates and returns a pointer to an error code instance.
func NewErrorCode(name string, code int, msg string) *errorCode {
return &errorCode{name: name, code: code, msg: msg}
}
func (c *errorCode) Name() string {
return c.name
}
func (c *errorCode) Code() int {
return c.code
}
func (c *errorCode) Msg() string {
return c.msg
}
// New creates a new error with the given messages and associates it with the error code.
// It returns an error that implements the `error` interface,
// when Config.Caller == true, pc records the function name, file, and line number of the method that called this method.
func (c *errorCode) New(messages ...string) error {
return &withErrorCode{
errCode: c,
msg: strings.Join(messages, Config.MessagesSeparator),
pc: getPCFromCaller(),
}
}
// Wrap wraps the given error with additional context and returns a new error.
// If the cause is nil, it returns nil.
// The additional context is specified by the messages parameter, which is joined
// using the Config.MessagesSeparator. The function also captures the program counter (PC)
// of the caller using the getPCFromCaller function.
func (c *errorCode) Wrap(cause error, messages ...string) error {
if cause == nil {
return nil
}
return &withCause{
error: &withErrorCode{
errCode: c,
msg: strings.Join(messages, Config.MessagesSeparator),
pc: getPCFromCaller(),
},
cause: cause,
}
}