-
Notifications
You must be signed in to change notification settings - Fork 2
/
cuzerr.go
119 lines (94 loc) · 2.1 KB
/
cuzerr.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
113
114
115
116
117
118
119
package cuzerr
import (
"fmt"
"net/http"
"time"
"github.com/bluekaki/pkg/errors"
"github.com/bluekaki/pkg/vv/proposal"
)
var _ proposal.Code = (*code)(nil)
type code struct {
bzCode int
httpCode int
desc string
}
func (c *code) BzCode() int {
return c.bzCode
}
func (c *code) HTTPCode() int {
return c.httpCode
}
func (c *code) Desc() string {
return c.desc
}
func (c *code) WithDesc(desc string) proposal.Code {
clone := *c
clone.desc = desc
return &clone
}
var bzCodes = make(map[int]bool)
// NewCode create a proposal code
func NewCode(bzCode, httpCode int, desc string) proposal.Code {
if bzCode <= 0 || bzCode > 99999999 {
panic(fmt.Sprintf("bzCode %d illegal", bzCode))
}
if http.StatusText(httpCode) == "" {
panic(fmt.Sprintf("httpCode %d not defined in http", httpCode))
}
if bzCodes[bzCode] {
panic(fmt.Sprintf("bzCode %d duplicated", bzCode))
}
bzCodes[bzCode] = true
return &code{
bzCode: bzCode,
httpCode: httpCode,
desc: desc,
}
}
type BzError interface {
proposal.BzError
AlertError(*proposal.AlertMessageMeta) proposal.AlertError
}
type bzError struct {
proposal.Code
err errors.Error
}
func (b *bzError) Error() string {
return b.err.Error()
}
func (b *bzError) StackErr() errors.Error {
return b.err
}
func (b *bzError) AlertError(meta *proposal.AlertMessageMeta) proposal.AlertError {
return &alertError{
bzError: b,
alertMessage: &proposal.AlertMessage{
ErrorVerbose: fmt.Sprintf("%+v", b.err),
Meta: meta,
Timestamp: time.Now(),
},
}
}
// NewBzError create a proposal bzerror
func NewBzError(code proposal.Code, err errors.Error) BzError {
if code == nil {
panic("proposal.Code required")
}
bzError := &bzError{err: err}
bzError.Code = code
return bzError
}
var _ proposal.AlertError = (*alertError)(nil)
type alertError struct {
bzError proposal.BzError
alertMessage *proposal.AlertMessage
}
func (a *alertError) Error() string {
return a.bzError.Error()
}
func (a *alertError) BzError() proposal.BzError {
return a.bzError
}
func (a *alertError) AlertMessage() *proposal.AlertMessage {
return a.alertMessage
}