-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
59 lines (50 loc) · 1.36 KB
/
Copy patherrors.go
File metadata and controls
59 lines (50 loc) · 1.36 KB
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
package riskybiscuits
import (
"fmt"
"cuelang.org/go/cue/errors"
"cuelang.org/go/cue/token"
)
type PortablePosition struct {
Filepath string `json:"filepath"`
Offset int `json:"offset"`
}
func ErrorToPortable(err errors.Error) *PortableError {
poss := err.InputPositions()
possJson := make([]PortablePosition, len(poss))
for i, pos := range poss {
possJson[i] = PosToPortable(pos)
}
f, args := err.Msg()
return &PortableError{
PositionJSON: PosToPortable(err.Position()),
InputPositionsJSON: possJson,
ErrorJSON: err.Error(),
PathJSON: err.Path(),
MsgJSON: fmt.Sprintf(f, args...),
}
}
func PosToPortable(p token.Pos) PortablePosition {
if p == token.NoPos {
return PortablePosition{}
}
return PortablePosition{
Filepath: p.Filename(),
Offset: p.Offset(),
}
}
type PortableError struct {
PositionJSON PortablePosition `json:"position"`
InputPositionsJSON []PortablePosition `json:"input_positions"`
ErrorJSON string `json:"error"`
PathJSON []string `json:"paths"`
MsgJSON string `json:"msg"`
}
func (p *PortableError) Error() string {
return p.ErrorJSON
}
func (p *PortableError) Path() []string {
return p.PathJSON
}
func (p *PortableError) Msg() (format string, args []interface{}) {
return "%s", []interface{}{p.MsgJSON}
}