-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfault.go
More file actions
48 lines (38 loc) · 863 Bytes
/
Copy pathfault.go
File metadata and controls
48 lines (38 loc) · 863 Bytes
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
package rowmap
import (
"errors"
"fmt"
)
type withRowMap struct {
wrapped error
entityType string
}
func (e *withRowMap) Error() string { return fmt.Sprintf("<rowmap> : %v", e.wrapped) }
func (e *withRowMap) Cause() error { return e.wrapped }
func (e *withRowMap) Unwrap() error { return e.wrapped }
func (e *withRowMap) String() string { return e.Error() }
func Wrap[E any](err error, mapper MapperFunc[E]) error {
if err == nil {
return nil
}
var d E
return &withRowMap{
wrapped: err,
entityType: fmt.Sprintf("%T", d),
}
}
func With[E any](mapper MapperFunc[E]) func(error) error {
return func(err error) error {
return Wrap(err, mapper)
}
}
func Get(err error) (string, bool) {
if err == nil {
return "", false
}
var with *withRowMap
if errors.As(err, &with) {
return with.entityType, true
}
return "", false
}