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