-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.go
80 lines (66 loc) · 1.07 KB
/
error.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
package app
import (
"bytes"
"fmt"
)
type Error interface {
error
StatusCode() int
Status() string
Message() string
}
type err struct {
code int
status string
Op string
msg string
Err error
}
var _ Error = (*err)(nil)
func (e *err) Error() string {
return fmt.Sprintf("%s: %d %s", e.Message(), e.code, e.status)
}
func (e *err) StatusCode() int {
if e.code == 0 {
return 500
}
return e.code
}
func (e *err) Status() string {
return e.status
}
func (e *err) Message() string {
var buf bytes.Buffer
if e.Op != "" {
fmt.Fprint(&buf, e.Op)
}
if e.msg != "" {
fmt.Fprintf(&buf, ": %s", e.msg)
}
if e.Err != nil {
fmt.Fprintf(&buf, ": %s", e.Err.Error())
}
return buf.String()
}
func NewErr(code int, status, msg string) *err {
return &err{
code: code,
status: status,
msg: msg,
}
}
func FromErr(e error, op string) *err {
if e == nil {
return nil
}
res := &err{Op: op}
err, _ := e.(Error)
if err != nil {
res.code = err.StatusCode()
res.status = err.Status()
res.msg = err.Message()
} else {
res.Err = e
}
return res
}