forked from binhonglee/kdlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
60 lines (52 loc) · 1.63 KB
/
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
57
58
59
60
package kdl
import (
"errors"
"io"
"strconv"
"strings"
)
var (
// ErrInvalidSyntax is a base error for when
// a parser comes across a document that is not spec-compliant.
ErrInvalidSyntax = errors.New("invalid syntax")
// ErrInvalidEncoding is a base error for when
// an invalid UTF8 byte sequence is encountered.
ErrInvalidEncoding = errors.New("document is not UTF-8 encoded")
// ErrUnexpectedEOF is a base error for when
// the data abruptly ends e.g. inside a string.
ErrUnexpectedEOF = io.ErrUnexpectedEOF
// ErrInvalidValueType happens when a raw value cannot be cast to a kdl.Value.
ErrInvalidValueType = errors.New("cannot transform to a valid kdl.Value type")
)
// ErrWithPosition wraps an error,
// adding information where in the document did it occur.
type ErrWithPosition struct {
Err error // The original error.
Line int // Line where the error occurred, 1-indexed.
Column int // Column where the error occurred, 0-indexed.
}
// Error formats an error message.
func (e *ErrWithPosition) Error() string {
innerMsg := "null"
err := e.Err
if err != nil {
innerMsg = err.Error()
}
var s strings.Builder
s.Grow(len(innerMsg) + 24)
s.WriteString(innerMsg)
s.WriteString(" [line ")
s.WriteString(strconv.Itoa(e.Line))
s.WriteString(", column ")
s.WriteString(strconv.Itoa(e.Column))
s.WriteString("]")
return s.String()
}
// Unwrap returns the original error.
func (e *ErrWithPosition) Unwrap() error {
return e.Err
}
// addErrPosInfo wraps an error, adding position information from context.
func addErrPosInfo(err error, r *reader) error {
return &ErrWithPosition{Err: err, Line: r.line, Column: r.pos}
}