diff options
Diffstat (limited to 'vendor/github.com/zclconf/go-cty-yaml/error.go')
-rw-r--r-- | vendor/github.com/zclconf/go-cty-yaml/error.go | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/vendor/github.com/zclconf/go-cty-yaml/error.go b/vendor/github.com/zclconf/go-cty-yaml/error.go new file mode 100644 index 0000000..ae41c48 --- /dev/null +++ b/vendor/github.com/zclconf/go-cty-yaml/error.go | |||
@@ -0,0 +1,97 @@ | |||
1 | package yaml | ||
2 | |||
3 | import ( | ||
4 | "errors" | ||
5 | "fmt" | ||
6 | ) | ||
7 | |||
8 | // Error is an error implementation used to report errors that correspond to | ||
9 | // a particular position in an input buffer. | ||
10 | type Error struct { | ||
11 | cause error | ||
12 | Line, Column int | ||
13 | } | ||
14 | |||
15 | func (e Error) Error() string { | ||
16 | return fmt.Sprintf("on line %d, column %d: %s", e.Line, e.Column, e.cause.Error()) | ||
17 | } | ||
18 | |||
19 | // Cause is an implementation of the interface used by | ||
20 | // github.com/pkg/errors.Cause, returning the underlying error without the | ||
21 | // position information. | ||
22 | func (e Error) Cause() error { | ||
23 | return e.cause | ||
24 | } | ||
25 | |||
26 | // WrappedErrors is an implementation of github.com/hashicorp/errwrap.Wrapper | ||
27 | // returning the underlying error without the position information. | ||
28 | func (e Error) WrappedErrors() []error { | ||
29 | return []error{e.cause} | ||
30 | } | ||
31 | |||
32 | func parserError(p *yaml_parser_t) error { | ||
33 | var cause error | ||
34 | if len(p.problem) > 0 { | ||
35 | cause = errors.New(p.problem) | ||
36 | } else { | ||
37 | cause = errors.New("invalid YAML syntax") // useless generic error, then | ||
38 | } | ||
39 | |||
40 | return parserErrorWrap(p, cause) | ||
41 | } | ||
42 | |||
43 | func parserErrorWrap(p *yaml_parser_t, cause error) error { | ||
44 | switch { | ||
45 | case p.problem_mark.line != 0: | ||
46 | line := p.problem_mark.line | ||
47 | column := p.problem_mark.column | ||
48 | // Scanner errors don't iterate line before returning error | ||
49 | if p.error == yaml_SCANNER_ERROR { | ||
50 | line++ | ||
51 | column = 0 | ||
52 | } | ||
53 | return Error{ | ||
54 | cause: cause, | ||
55 | Line: line, | ||
56 | Column: column + 1, | ||
57 | } | ||
58 | case p.context_mark.line != 0: | ||
59 | return Error{ | ||
60 | cause: cause, | ||
61 | Line: p.context_mark.line, | ||
62 | Column: p.context_mark.column + 1, | ||
63 | } | ||
64 | default: | ||
65 | return cause | ||
66 | } | ||
67 | } | ||
68 | |||
69 | func parserErrorf(p *yaml_parser_t, f string, vals ...interface{}) error { | ||
70 | return parserErrorWrap(p, fmt.Errorf(f, vals...)) | ||
71 | } | ||
72 | |||
73 | func parseEventErrorWrap(evt *yaml_event_t, cause error) error { | ||
74 | if evt.start_mark.line == 0 { | ||
75 | // Event does not have a start mark, so we won't wrap the error at all | ||
76 | return cause | ||
77 | } | ||
78 | return Error{ | ||
79 | cause: cause, | ||
80 | Line: evt.start_mark.line, | ||
81 | Column: evt.start_mark.column + 1, | ||
82 | } | ||
83 | } | ||
84 | |||
85 | func parseEventErrorf(evt *yaml_event_t, f string, vals ...interface{}) error { | ||
86 | return parseEventErrorWrap(evt, fmt.Errorf(f, vals...)) | ||
87 | } | ||
88 | |||
89 | func emitterError(e *yaml_emitter_t) error { | ||
90 | var cause error | ||
91 | if len(e.problem) > 0 { | ||
92 | cause = errors.New(e.problem) | ||
93 | } else { | ||
94 | cause = errors.New("failed to write YAML token") // useless generic error, then | ||
95 | } | ||
96 | return cause | ||
97 | } | ||