This repository has been archived by the owner on Jan 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
97 lines (86 loc) · 2.19 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package errors
import (
"fmt"
"runtime"
"strings"
)
type ErrorKind string
const (
// default
ExecutionError ErrorKind = "ExecutionError"
ConfigError ErrorKind = "ConfigError"
NotImplemented ErrorKind = "NotImplemented"
)
// Error is exported so you can easily check types, but should be not used
// directly. Call New instead.
type Error struct {
kind ErrorKind
msg string
stack []string
}
func (e *Error) Error() string {
stack := strings.Join(e.stack, "\n")
return fmt.Sprintf("%s: %s\n---\n%s", e.kind, e.msg, stack)
}
// ensure type Error fulfills error interface
var _ error = &Error{}
// New creates an error of ExecutionError kind. A message is mandatory. Stack
// of function calls will be attached automatically.
func New(msg string, args ...interface{}) error {
return &Error{
kind: ExecutionError,
msg: fmt.Sprintf(msg, args...),
stack: []string{identifyCaller()},
}
}
// NewOfKind creates an error of a predefined or custom kind, much like
// New.
func NewOfKind(kind ErrorKind, msg string, args ...interface{}) error {
return &Error{
kind: kind,
msg: fmt.Sprintf(msg, args...),
stack: []string{identifyCaller()},
}
}
// Mask helps convert generic errors into *Error type. You can use it to
// pin-point breaking calls in the code - it automatically appends a call
// stack.
func Mask(e error) error {
switch v := e.(type) {
case *Error:
stack := append(v.stack, identifyCaller())
return &Error{
kind: v.kind,
msg: v.msg,
stack: stack,
}
default:
return &Error{
kind: ExecutionError,
msg: e.Error(),
stack: []string{identifyCaller()},
}
}
}
// Maskf does exactly what Mask does, but you can add a custom message.
func Maskf(e error, msg string, args ...interface{}) error {
switch v := e.(type) {
case *Error:
stack := append(v.stack, identifyCaller())
return &Error{
kind: v.kind,
msg: fmt.Sprintf(msg, args...) + ": " + v.msg,
stack: stack,
}
default:
return &Error{
kind: ExecutionError,
msg: fmt.Sprintf(msg, args...) + ": " + e.Error(),
stack: []string{identifyCaller()},
}
}
}
func identifyCaller() string {
_, filename, line, _ := runtime.Caller(2)
return fmt.Sprintf("%s:%d", filename, line)
}