-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.go
More file actions
58 lines (48 loc) · 1.06 KB
/
error.go
File metadata and controls
58 lines (48 loc) · 1.06 KB
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
package types
import (
"errors"
)
type Error[T any] struct {
Value error
}
//lint:ignore U1000 method is used to implement Result[T]
func (Error[T]) result(t T) {}
// Deconstruct implements Result[T].Deconstruct()
func (e Error[T]) Deconstruct() (T, error) {
var t T
return t, e.Value
}
// IsError implements Result[T].IsError()
func (e Error[T]) IsError(err ...error) bool {
// no filters so match any error
if len(err) == 0 {
return true
}
// must match one of the filters
for _, target := range err {
if errors.Is(e.Value, target) {
return true
}
}
return false
}
// IsOk implements Result[T].IsOk()
func (e Error[T]) IsOk() bool {
return false
}
// Unwrap() implements Result[T].Unwrap()
func (e Error[T]) Unwrap() T {
Throw(e.Value)
var zero T
return zero
}
// MapError implements Result[T].MapError
func (o Error[T]) MapError(errMap func(error) error) Result[T] {
return NewError[T](errMap(o.Value))
}
// NewError returns an error over the supplied error
func NewError[T any](value error) Result[T] {
return Error[T]{
Value: value,
}
}