-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.go
153 lines (132 loc) · 2.94 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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package winter
import (
"errors"
"net/http"
)
const (
HaltExtraKeyMessage = "message"
)
// HaltOption configuration function for [HaltError]
type HaltOption func(h *haltError)
// HaltWithStatusCode a [HaltOption] setting status code
func HaltWithStatusCode(code int) HaltOption {
return func(h *haltError) {
h.statusCode = code
}
}
// HaltWithBadRequest alias to [HaltWithStatusCode] with [http.StatusBadRequest]
func HaltWithBadRequest() HaltOption {
return HaltWithStatusCode(http.StatusBadRequest)
}
// HaltWithMessage a [HaltOption] overriding message key
func HaltWithMessage(m string) HaltOption {
return HaltWithExtra(HaltExtraKeyMessage, m)
}
// HaltWithExtra a [HaltOption] setting extras with a key-value
func HaltWithExtra(k string, v any) HaltOption {
return func(h *haltError) {
if h.extras == nil {
h.extras = map[string]any{}
}
h.extras[k] = v
}
}
// HaltWithExtras a [HaltOption] setting extras with key-values
func HaltWithExtras(m map[string]any) HaltOption {
return func(h *haltError) {
if h.extras == nil {
h.extras = map[string]any{}
}
for k, v := range m {
h.extras[k] = v
}
}
}
type withStatusCode interface {
StatusCode() int
}
type withExtract interface {
ExtractExtras(m map[string]any)
}
type withUnwrap interface {
Unwrap() error
}
var (
_ withStatusCode = &haltError{}
_ withExtract = &haltError{}
_ withUnwrap = &haltError{}
)
type haltError struct {
error
statusCode int
extras map[string]any
}
func (h *haltError) Unwrap() error {
return h.error
}
func (h *haltError) StatusCode() int {
return h.statusCode
}
func (h *haltError) ExtractExtras(m map[string]any) {
for k, v := range h.extras {
m[k] = v
}
}
// NewHaltError create a new [HaltError]
func NewHaltError(err error, opts ...HaltOption) error {
he := &haltError{
error: err,
statusCode: http.StatusInternalServerError,
}
for _, opt := range opts {
opt(he)
}
return he
}
// Halt panic with [NewHaltError]
func Halt(err error, opts ...HaltOption) {
panic(NewHaltError(err, opts...))
}
// HaltString panic with [NewHaltError] and [errors.New]
func HaltString(s string, opts ...HaltOption) {
Halt(errors.New(s), opts...)
}
// StatusCodeFromError get status code from previous created [HaltError]
func StatusCodeFromError(err error) int {
for {
if err == nil {
return http.StatusInternalServerError
}
if eh, ok := err.(withStatusCode); ok {
return eh.StatusCode()
}
if eu, ok := err.(withUnwrap); ok {
err = eu.Unwrap()
} else {
break
}
}
return http.StatusInternalServerError
}
// JSONBodyFromError extract extras from previous created [HaltError]
func JSONBodyFromError(err error) (m map[string]any) {
for {
if err == nil {
return
}
if m == nil {
m = map[string]any{
HaltExtraKeyMessage: err.Error(),
}
}
if eh, ok := err.(withExtract); ok {
eh.ExtractExtras(m)
}
if eu, ok := err.(withUnwrap); ok {
err = eu.Unwrap()
} else {
break
}
}
return
}