-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
errors.go
56 lines (43 loc) · 814 Bytes
/
errors.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
package hype
import (
"encoding/json"
"fmt"
)
const (
ErrNilFigure = ErrIsNil("figure")
)
type ErrIsNil string
func (e ErrIsNil) Error() string {
return fmt.Sprintf("%s is nil", string(e))
}
func WrapNodeErr(n Node, err error) error {
if err == nil {
return nil
}
if h, ok := n.(Tag); ok {
return fmt.Errorf("%T: %v: %w", h, h, err)
}
return fmt.Errorf("%T: %w", n, err)
}
func errForJSON(err error) any {
if err == nil {
return nil
}
if _, ok := err.(json.Marshaler); ok {
return err
}
return err.Error()
}
func toError(err error) string {
if err == nil {
return ""
}
if _, ok := err.(json.Marshaler); ok {
b, err := json.MarshalIndent(err, "", " ")
if err != nil {
return "error marshalling to json: " + err.Error()
}
return string(b)
}
return err.Error()
}