Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to get source position of unmarshal errors (enhanced version) #901

Open
wants to merge 6 commits into
base: v3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 22 additions & 11 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"io"
"math"
"reflect"
"strconv"
"time"
)

Expand Down Expand Up @@ -110,7 +109,6 @@ func (p *parser) peek() yaml_event_type_t {
}

func (p *parser) fail() {
var where string
var line int
if p.parser.context_mark.line != 0 {
line = p.parser.context_mark.line
Expand All @@ -125,16 +123,13 @@ func (p *parser) fail() {
line++
}
}
if line != 0 {
where = "line " + strconv.Itoa(line) + ": "
}
var msg string
if len(p.parser.problem) > 0 {
msg = p.parser.problem
} else {
msg = "unknown problem parsing YAML content"
}
failf("%s%s", where, msg)
fail(&ParserError{msg, line})
}

func (p *parser) anchor(n *Node, anchor []byte) {
Expand Down Expand Up @@ -313,7 +308,7 @@ func (p *parser) mapping() *Node {
type decoder struct {
doc *Node
aliases map[*Node]bool
terrors []string
terrors []UnmarshalError

stringMapType reflect.Type
generalMapType reflect.Type
Expand Down Expand Up @@ -359,7 +354,11 @@ func (d *decoder) terror(n *Node, tag string, out reflect.Value) {
value = " `" + value + "`"
}
}
d.terrors = append(d.terrors, fmt.Sprintf("line %d: cannot unmarshal %s%s into %s", n.Line, shortTag(tag), value, out.Type()))
d.terrors = append(d.terrors, UnmarshalError{
Message: fmt.Sprintf("cannot unmarshal %s%s into %s", shortTag(tag), value, out.Type()),
Line: n.Line,
Column: n.Column,
})
}

func (d *decoder) callUnmarshaler(n *Node, u Unmarshaler) (good bool) {
Expand Down Expand Up @@ -773,7 +772,11 @@ func (d *decoder) mapping(n *Node, out reflect.Value) (good bool) {
for j := i + 2; j < l; j += 2 {
nj := n.Content[j]
if ni.Kind == nj.Kind && ni.Value == nj.Value {
d.terrors = append(d.terrors, fmt.Sprintf("line %d: mapping key %#v already defined at line %d", nj.Line, nj.Value, ni.Line))
d.terrors = append(d.terrors, UnmarshalError{
Message: fmt.Sprintf("mapping key %#v already defined at line %d", nj.Value, ni.Line),
Line: nj.Line,
Column: nj.Column,
})
}
}
}
Expand Down Expand Up @@ -921,7 +924,11 @@ func (d *decoder) mappingStruct(n *Node, out reflect.Value) (good bool) {
if info, ok := sinfo.FieldsMap[sname]; ok {
if d.uniqueKeys {
if doneFields[info.Id] {
d.terrors = append(d.terrors, fmt.Sprintf("line %d: field %s already set in type %s", ni.Line, name.String(), out.Type()))
d.terrors = append(d.terrors, UnmarshalError{
Message: fmt.Sprintf("field %s already set in type %s", name.String(), out.Type()),
Line: ni.Line,
Column: ni.Column,
})
continue
}
doneFields[info.Id] = true
Expand All @@ -941,7 +948,11 @@ func (d *decoder) mappingStruct(n *Node, out reflect.Value) (good bool) {
d.unmarshal(n.Content[i+1], value)
inlineMap.SetMapIndex(name, value)
} else if d.knownFields {
d.terrors = append(d.terrors, fmt.Sprintf("line %d: field %s not found in type %s", ni.Line, name.String(), out.Type()))
d.terrors = append(d.terrors, UnmarshalError{
Message: fmt.Sprintf("field %s not found in type %s", name.String(), out.Type()),
Line: ni.Line,
Column: ni.Column,
})
}
}

Expand Down
28 changes: 20 additions & 8 deletions decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -980,6 +980,18 @@ func (s *S) TestDecoderErrors(c *C) {
}
}

func (s *S) TestParserError(c *C) {
var v struct {
A, B int
}
data := "a: 1\n=\nb: 2"
err := yaml.Unmarshal([]byte(data), &v)
c.Assert(err, DeepEquals, &yaml.ParserError{
Message: "could not find expected ':'",
Line: 2,
})
}

var unmarshalerTests = []struct {
data, tag string
value interface{}
Expand Down Expand Up @@ -1112,8 +1124,8 @@ func (s *S) TestUnmarshalerWholeDocument(c *C) {
}

func (s *S) TestUnmarshalerTypeError(c *C) {
unmarshalerResult[2] = &yaml.TypeError{[]string{"foo"}}
unmarshalerResult[4] = &yaml.TypeError{[]string{"bar"}}
unmarshalerResult[2] = &yaml.TypeError{[]yaml.UnmarshalError{{"foo", 1, 1}}}
unmarshalerResult[4] = &yaml.TypeError{[]yaml.UnmarshalError{{"bar", 1, 1}}}
defer func() {
delete(unmarshalerResult, 2)
delete(unmarshalerResult, 4)
Expand All @@ -1130,8 +1142,8 @@ func (s *S) TestUnmarshalerTypeError(c *C) {
c.Assert(err, ErrorMatches, ""+
"yaml: unmarshal errors:\n"+
" line 1: cannot unmarshal !!str `A` into int\n"+
" foo\n"+
" bar\n"+
" line 1: foo\n"+
" line 1: bar\n"+
" line 1: cannot unmarshal !!str `B` into int")
c.Assert(v.M["abc"], NotNil)
c.Assert(v.M["def"], IsNil)
Expand All @@ -1143,8 +1155,8 @@ func (s *S) TestUnmarshalerTypeError(c *C) {
}

func (s *S) TestObsoleteUnmarshalerTypeError(c *C) {
unmarshalerResult[2] = &yaml.TypeError{[]string{"foo"}}
unmarshalerResult[4] = &yaml.TypeError{[]string{"bar"}}
unmarshalerResult[2] = &yaml.TypeError{[]yaml.UnmarshalError{{"foo", 1, 1}}}
unmarshalerResult[4] = &yaml.TypeError{[]yaml.UnmarshalError{{"bar", 1, 1}}}
defer func() {
delete(unmarshalerResult, 2)
delete(unmarshalerResult, 4)
Expand All @@ -1161,8 +1173,8 @@ func (s *S) TestObsoleteUnmarshalerTypeError(c *C) {
c.Assert(err, ErrorMatches, ""+
"yaml: unmarshal errors:\n"+
" line 1: cannot unmarshal !!str `A` into int\n"+
" foo\n"+
" bar\n"+
" line 1: foo\n"+
" line 1: bar\n"+
" line 1: cannot unmarshal !!str `B` into int")
c.Assert(v.M["abc"], NotNil)
c.Assert(v.M["def"], IsNil)
Expand Down
56 changes: 51 additions & 5 deletions yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"fmt"
"io"
"reflect"
"strconv"
"strings"
"sync"
"unicode/utf8"
Expand Down Expand Up @@ -288,7 +289,7 @@ func (e *Encoder) Close() (err error) {

func handleErr(err *error) {
if v := recover(); v != nil {
if e, ok := v.(yamlError); ok {
if e, ok := v.(*yamlError); ok {
*err = e.err
} else {
panic(v)
Expand All @@ -300,24 +301,69 @@ type yamlError struct {
err error
}

func (e *yamlError) Unwrap() error {
return e.err
}

func fail(err error) {
panic(yamlError{err})
panic(&yamlError{err})
}

func failf(format string, args ...interface{}) {
panic(yamlError{fmt.Errorf("yaml: "+format, args...)})
panic(&yamlError{fmt.Errorf("yaml: "+format, args...)})
}

// ParserError is each error with a source line position found by the parser.
// Unlike UnmarshalErrors, it is only used for terminal failures.
type ParserError struct {
Message string
Line int
}

func (e *ParserError) Error() string {
var b strings.Builder
b.WriteString("yaml: ")
if e.Line != 0 {
b.WriteString("line " + strconv.Itoa(e.Line) + ": ")
}
b.WriteString(e.Message)
return b.String()
}

// UnmarshalError is each error with a source position found by Unmarshal.
type UnmarshalError struct {
Message string
Line int
Column int
}

func (e UnmarshalError) Error() string {
return fmt.Sprintf("line %d: %s", e.Line, e.Message)
}

// A TypeError is returned by Unmarshal when one or more fields in
// the YAML document cannot be properly decoded into the requested
// types. When this error is returned, the value is still
// unmarshaled partially.
type TypeError struct {
Errors []string
Errors []UnmarshalError
}

func (e *TypeError) Error() string {
return fmt.Sprintf("yaml: unmarshal errors:\n %s", strings.Join(e.Errors, "\n "))
var b strings.Builder
b.WriteString("yaml: unmarshal errors:")
for _, err := range e.Errors {
b.WriteString("\n " + err.Error())
}
return b.String()
}

func (e *TypeError) Unwrap() []error {
errs := make([]error, 0, len(e.Errors))
for _, err := range e.Errors {
errs = append(errs, err)
}
return errs
}

type Kind uint32
Expand Down