-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.go
More file actions
90 lines (81 loc) · 2.29 KB
/
Copy patherror.go
File metadata and controls
90 lines (81 loc) · 2.29 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
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
86
87
88
89
90
package vibejson
import (
"fmt"
"strconv"
)
// SyntaxError describes a JSON syntax error with byte, line, and column
// positions. It does not retain the decoder input.
type SyntaxError struct {
// Offset is the zero-based byte offset of the syntax failure in the input.
Offset int
// Line is the one-based input line containing Offset.
Line int
// Column is the one-based byte column containing Offset.
Column int
// Message describes the violated JSON grammar rule.
Message string
}
// Error formats the byte, line, column, and grammar failure.
func (e *SyntaxError) Error() string {
return fmt.Sprintf("json syntax error at byte %d, line %d, column %d: %s", e.Offset, e.Line, e.Column, e.Message)
}
func syntaxError(src []byte, off int, msg string) *SyntaxError {
if off < 0 {
off = 0
}
if off > len(src) {
off = len(src)
}
line, col := 1, 1
for i := 0; i < off; i++ {
if src[i] == '\n' {
line++
col = 1
continue
}
col++
}
return &SyntaxError{Offset: off, Line: line, Column: col, Message: msg}
}
// EncodeError reports a Go value that cannot be represented in JSON. The
// encoder does not attach the source value to the error.
type EncodeError struct {
// Path locates the offending value using JSON member names and array
// indexes, for example "items[3].scores[1]". It is empty when the
// top-level value itself failed. Building the path costs nothing until
// an error actually unwinds.
Path string
// Reason describes why the value cannot be represented as JSON.
Reason string
}
// Error formats the encode failure and its optional value path.
func (e *EncodeError) Error() string {
if e.Path != "" {
return fmt.Sprintf("vibejson: cannot encode value at %s: %s", e.Path, e.Reason)
}
return "vibejson: cannot encode value: " + e.Reason
}
func prependEncodePathField(err error, name string) error {
if e, ok := err.(*EncodeError); ok {
switch {
case e.Path == "":
e.Path = name
case e.Path[0] == '[':
e.Path = name + e.Path
default:
e.Path = name + "." + e.Path
}
}
return err
}
func prependEncodePathIndex(err error, index int) error {
if e, ok := err.(*EncodeError); ok {
segment := "[" + strconv.Itoa(index) + "]"
if e.Path == "" || e.Path[0] == '[' {
e.Path = segment + e.Path
} else {
e.Path = segment + "." + e.Path
}
}
return err
}