-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy patherrable.go
217 lines (197 loc) · 4.11 KB
/
errable.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package gust
import (
"reflect"
)
// Errable is the type that indicates whether there is an error.
type Errable[E any] struct {
errVal *E
}
// NonErrable returns no error object.
func NonErrable[E any]() Errable[E] {
return Errable[E]{}
}
// ToErrable converts an error value (E) to `Errable[T]`.
func ToErrable[E any](errVal E) Errable[E] {
switch t := any(errVal).(type) {
case nil:
return Errable[E]{}
case error:
if t == nil {
return Errable[E]{}
}
case int, int64, int32, int16, int8, uint, uint64, uint32, uint16, uint8, float32, float64, complex64, complex128, string, bool:
case *int:
if t == (*int)(nil) {
return Errable[E]{}
}
case *int64:
if t == (*int64)(nil) {
return Errable[E]{}
}
case *int32:
if t == (*int32)(nil) {
return Errable[E]{}
}
case *int16:
if t == (*int16)(nil) {
return Errable[E]{}
}
case *int8:
if t == (*int8)(nil) {
return Errable[E]{}
}
case *uint:
if t == (*uint)(nil) {
return Errable[E]{}
}
case *uint64:
if t == (*uint64)(nil) {
return Errable[E]{}
}
case *uint32:
if t == (*uint32)(nil) {
return Errable[E]{}
}
case *uint16:
if t == (*uint16)(nil) {
return Errable[E]{}
}
case *uint8:
if t == (*uint8)(nil) {
return Errable[E]{}
}
case *float32:
if t == (*float32)(nil) {
return Errable[E]{}
}
case *float64:
if t == (*float64)(nil) {
return Errable[E]{}
}
case *complex64:
if t == (*complex64)(nil) {
return Errable[E]{}
}
case *complex128:
if t == (*complex128)(nil) {
return Errable[E]{}
}
case *string:
if t == (*string)(nil) {
return Errable[E]{}
}
case *bool:
if t == (*bool)(nil) {
return Errable[E]{}
}
default:
v := reflect.ValueOf(errVal)
if v.Kind() == reflect.Ptr && v.IsNil() {
return Errable[E]{}
}
}
return Errable[E]{errVal: &errVal}
}
// TryPanic panics if the errVal is not nil.
func TryPanic[E any](errVal E) {
ToErrable(errVal).TryPanic()
}
// TryThrow panic returns E (panicValue[*E]) if the errVal is not nil.
// NOTE:
//
// If there is an E, that panic should be caught with CatchErrable[E] or CatchEnumResult[U, E].
func TryThrow[E any](errVal E) {
ToErrable(errVal).TryThrow()
}
func (e Errable[E]) IsErr() bool {
return e.errVal != nil
}
func (e Errable[E]) IsOk() bool {
return e.errVal == nil
}
func (e Errable[E]) ToError() error {
if e.IsOk() {
return nil
}
return toError(e.UnwrapErr())
}
func (e Errable[E]) UnwrapErr() E {
return *e.errVal
}
func (e Errable[E]) UnwrapErrOr(def E) E {
if e.IsErr() {
return e.UnwrapErr()
}
return def
}
func (e Errable[E]) EnumResult() EnumResult[Void, E] {
if e.IsErr() {
return EnumErr[Void, E](e.UnwrapErr())
}
return EnumOk[Void, E](nil)
}
func (e Errable[E]) Result() Result[Void] {
if e.IsErr() {
return Err[Void](e.UnwrapErr())
}
return Ok[Void](nil)
}
func (e Errable[E]) Option() Option[E] {
if e.IsErr() {
return Some[E](e.UnwrapErr())
}
return None[E]()
}
// CtrlFlow returns the `CtrlFlow[E, Void]`.
func (e Errable[E]) CtrlFlow() CtrlFlow[E, Void] {
if e.IsErr() {
return Break[E, Void](e.UnwrapErr())
}
return Continue[E, Void](nil)
}
func (e Errable[E]) InspectErr(f func(err E)) Errable[E] {
if e.IsErr() {
f(e.UnwrapErr())
}
return e
}
func (e Errable[E]) Inspect(f func()) Errable[E] {
if e.IsOk() {
f()
}
return e
}
// TryPanic panics if the errVal is not nil.
func (e Errable[E]) TryPanic() {
if e.IsErr() {
panic(e.UnwrapErr())
}
}
// TryThrow panic returns E (panicValue[*E]) if the errVal is not nil.
// NOTE:
//
// If there is an E, that panic should be caught with CatchErrable[E] or CatchEnumResult[U, E].
func (e Errable[E]) TryThrow() {
if e.errVal != nil {
panic(panicValue[*E]{value: e.errVal})
}
}
// CatchErrable catches panic caused by Errable[E].TryThrow() and sets E to *Errable[E]
// Example:
//
// ```go
// func example() (errable Errable[string]) {
// defer CatchErrable[E](&errable)
// ToErrable("panic error").TryThrow()
// return ToErrable("return error")
// }
// ```
func CatchErrable[E any](result *Errable[E]) {
switch p := recover().(type) {
case nil:
case panicValue[*E]:
result.errVal = p.value
default:
panic(p)
}
}