this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

internal/encoding/yaml: add underlying error to "cannot decode V as T"

This can be useful context that would otherwise be lost.
Some of these errors may be a bit repetitive, such as repeating
the input YAML string, but that's fine.

Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: Ia95c25566288a4f1b7403a7b1fec1fe1e13ae92e
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1195045
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
Reviewed-by: Roger Peppe <rogpeppe@gmail.com>

+8 -6
+5 -3
internal/encoding/yaml/decode.go
··· 542 542 // We make the assumption that any valid YAML integer literal will be a valid 543 543 // CUE integer literal as well, with the only exception of octal numbers above. 544 544 // Note that `!!int 123.456` is not allowed. 545 - if err := literal.ParseNum(value, &info); err != nil || !info.IsInt() { 546 - return nil, d.posErrorf(yn, "cannot decode %q as %s", value, yn.ShortTag()) 545 + if err := literal.ParseNum(value, &info); err != nil { 546 + return nil, d.posErrorf(yn, "cannot decode %q as %s: %v", value, tag, err) 547 + } else if !info.IsInt() { 548 + return nil, d.posErrorf(yn, "cannot decode %q as %s: not a literal number", value, tag) 547 549 } 548 550 return d.makeNum(yn, value, token.INT), nil 549 551 ··· 563 565 // CUE float literal as well, with the only exception of Inf/NaN above. 564 566 // Note that `!!float 123` is allowed. 565 567 if err := literal.ParseNum(value, &info); err != nil { 566 - return nil, d.posErrorf(yn, "cannot decode %q as %s", value, yn.ShortTag()) 568 + return nil, d.posErrorf(yn, "cannot decode %q as %s: %v", value, tag, err) 567 569 } 568 570 // If the decoded YAML scalar was explicitly or implicitly a float, 569 571 // and the scalar literal looks like an integer,
+3 -3
internal/encoding/yaml/decode_test.go
··· 902 902 var unmarshalErrorTests = []struct { 903 903 data, error string 904 904 }{ 905 - {"\nv: !!float 'error'", `test.yaml:2: cannot decode "error" as !!float`}, 906 - {"\nv: !!int 'error'", `test.yaml:2: cannot decode "error" as !!int`}, 907 - {"\nv: !!int 123.456", `test.yaml:2: cannot decode "123.456" as !!int`}, 905 + {"\nv: !!float 'error'", `test.yaml:2: cannot decode "error" as !!float: illegal number start "error"`}, 906 + {"\nv: !!int 'error'", `test.yaml:2: cannot decode "error" as !!int: illegal number start "error"`}, 907 + {"\nv: !!int 123.456", `test.yaml:2: cannot decode "123.456" as !!int: not a literal number`}, 908 908 {"v: [A,", "test.yaml:1: did not find expected node content"}, 909 909 {"v:\n- [A,", "test.yaml:2: did not find expected node content"}, 910 910 {"a:\n- b: *,", "test.yaml:2: did not find expected alphabetic or numeric character"},