-
Notifications
You must be signed in to change notification settings - Fork 0
/
detailed.go
67 lines (56 loc) · 1.04 KB
/
detailed.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
package ge
import (
"fmt"
"iter"
"maps"
"strings"
)
type D map[string]any
type Detailed interface {
Details() iter.Seq2[string, any]
}
type detailed struct {
err error
details D
}
func Detail(err error, details D) error {
if err == nil {
return nil
}
if len(details) == 0 {
return err
}
return detailed{err, details}
}
func Details(err error) iter.Seq2[string, any] {
if e, ok := err.(Detailed); ok {
return e.Details()
}
return nil
}
func (d detailed) Error() string {
return d.err.Error()
}
func (d detailed) String() string {
return "error: " + d.err.Error() + " " + d.details.String()
}
func (d detailed) Err() error {
return d.err
}
func (d detailed) Details() iter.Seq2[string, any] {
return maps.All(d.details)
}
func (d D) String() string {
if len(d) == 0 {
return "[]"
}
builder := strings.Builder{}
for k, v := range d {
builder.WriteString(k)
builder.WriteString(":")
builder.WriteString(fmt.Sprint(v))
builder.WriteByte(' ')
}
str := builder.String()
return "[" + str[:len(str)-1] + "]"
}