-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
75 lines (70 loc) · 1.64 KB
/
errors.go
File metadata and controls
75 lines (70 loc) · 1.64 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
package errors
import (
"fmt"
"github.com/golage/errors/stacktrace"
)
// New returns new instance fundamental error with args
func New(code Code, message string, args ...interface{}) Fundamental {
if code == CodeNil {
return nil
}
return &fundamental{
code: code,
message: fmt.Sprintf(message, args...),
stackTrace: stacktrace.Capture(1),
}
}
// Wrap returns new instance fundamental error with args from error cause
func Wrap(cause error, code Code, message string, args ...interface{}) Fundamental {
parsed, _ := Parse(cause)
if parsed == nil {
return nil
}
if code == CodeNil {
return nil
}
fnd := &fundamental{
code: code,
message: fmt.Sprintf("%v: %v", fmt.Sprintf(message, args...), parsed.Message()),
stackTrace: parsed.StackTrace(),
}
if fnd.stackTrace == nil {
fnd.stackTrace = stacktrace.Capture(1)
}
return fnd
}
// Cast returns new instance fundamental error with error cause and code
func Cast(err error, code Code) Fundamental {
parsed, _ := Parse(err)
if parsed == nil {
return nil
}
if code == CodeNil {
return nil
}
fnd := &fundamental{
code: code,
message: parsed.Message(),
stackTrace: parsed.StackTrace(),
}
if fnd.stackTrace == nil {
fnd.stackTrace = stacktrace.Capture(1)
}
return fnd
}
// Parse returns fundamental error and code from all of error types
func Parse(err error) (Fundamental, Code) {
switch err := err.(type) {
case nil:
return nil, CodeNil
case Fundamental:
return err, err.Code()
case stackTracer:
fnd := parseStackTracer(err)
return fnd, fnd.Code()
default:
fnd := new(fundamental)
fnd.Unmarshal(err.Error())
return fnd, fnd.code
}
}