-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
execute_error.go
85 lines (66 loc) · 1.49 KB
/
execute_error.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
package hype
import (
"encoding/json"
"errors"
"fmt"
"path/filepath"
"strings"
)
type ExecuteError struct {
Err error
Contents []byte
Document *Document
Filename string
Root string
}
func (pe ExecuteError) MarshalJSON() ([]byte, error) {
mm := map[string]any{
"contents": string(pe.Contents),
"document": pe.Document,
"error": errForJSON(pe.Err),
"filename": pe.Filename,
"root": pe.Root,
"type": toType(pe),
}
return json.MarshalIndent(mm, "", " ")
}
func (pe ExecuteError) Error() string {
sb := &strings.Builder{}
var lines []string
fp := filepath.Join(pe.Root, pe.Filename)
if len(fp) > 0 {
lines = append(lines, fmt.Sprintf("filepath: %s", fp))
}
if pe.Document != nil && len(pe.Document.Title) > 0 {
lines = append(lines, fmt.Sprintf("document: %s", pe.Document.Title))
}
if pe.Err != nil {
lines = append(lines, fmt.Sprintf("execute error: %s", pe.Err))
}
sb.WriteString(strings.Join(lines, "\n"))
s := sb.String()
return strings.TrimSpace(s)
}
func (pe ExecuteError) String() string {
return pe.Error()
}
func (pe ExecuteError) Unwrap() error {
if _, ok := pe.Err.(unwrapper); ok {
return errors.Unwrap(pe.Err)
}
return pe.Err
}
func (pe ExecuteError) As(target any) bool {
ex, ok := target.(*ExecuteError)
if !ok {
return errors.As(pe.Err, target)
}
(*ex) = pe
return true
}
func (pe ExecuteError) Is(target error) bool {
if _, ok := target.(ExecuteError); ok {
return true
}
return errors.Is(pe.Err, target)
}