forked from chararch/gobatch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
112 lines (101 loc) · 2.37 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package batch
import (
"fmt"
"github.com/pkg/errors"
"io"
)
//BatchError represent an error during GoBatch executing
type BatchError interface {
//Code code of the error
Code() string
//Message readable message of the error
Message() string
//Error error interface
Error() string
//StackTrace goroutine stack trace
StackTrace() string
}
type stackTracer interface {
StackTrace() errors.StackTrace
}
type causer interface {
Cause() error
}
type batchError struct {
code string
msg string
err error
}
func (err *batchError) Code() string {
return err.code
}
func (err *batchError) Message() string {
return err.msg
}
func (err *batchError) Error() string {
if err.err.Error() == "" {
return fmt.Sprintf("BatchError[%s]: %v", err.code, err.msg)
}
return fmt.Sprintf("BatchError[%s]: %v cause: %v", err.code, err.msg, err.err)
}
func (err *batchError) StackTrace() string {
return fmt.Sprintf("%+v", err)
}
func (err *batchError) Format(s fmt.State, verb rune) {
switch verb {
case 'v':
if s.Flag('+') {
if e, ok := err.err.(causer); ok {
fmt.Fprintf(s, "%+v\n", e.Cause())
}
fmt.Fprintf(s, "BatchError[%s]: %v", err.code, err.msg)
if st, ok := err.err.(stackTracer); ok {
traces := st.StackTrace()
if len(traces) > 0 {
traces = traces[1:]
}
for _, t := range traces {
fmt.Fprintf(s, "\n%+v", t)
}
}
return
}
fallthrough
case 's':
io.WriteString(s, err.Error())
case 'q':
fmt.Fprintf(s, "%q", err.Error())
}
}
//NewBatchError new instance
func NewBatchError(code string, msg string, args ...interface{}) BatchError {
var err error
if len(args) > 0 {
lastArg := args[len(args)-1]
if e, ok := lastArg.(error); ok {
args = args[0 : len(args)-1]
if len(args) > 0 {
msg = fmt.Sprintf(msg, args...)
}
err = errors.WithStack(e)
} else {
msg = fmt.Sprintf(msg, args...)
err = errors.New("")
}
} else {
err = errors.New("")
}
return &batchError{code: code, msg: msg, err: err}
}
const (
//ErrCodeRetry an error indicating the caller should retry
ErrCodeRetry = "retry"
//ErrCodeStop an error indicating the job is to be stopped
ErrCodeStop = "stop"
//ErrCodeConcurrency an error indicating conflict modification
ErrCodeConcurrency = "concurrency"
//ErrCodeDbFail an error indicating database access failed
ErrCodeDbFail = "db_fail"
//ErrCodeGeneral general error
ErrCodeGeneral = "general"
)