Fixed handling of json decoding error #1423

This commit is contained in:
Mike Farah 2022-11-10 22:33:38 +11:00
parent ececd00fbd
commit 3435fee1f9
2 changed files with 18 additions and 1 deletions

View File

@ -46,6 +46,10 @@ func (o *orderedMap) UnmarshalJSON(data []byte) error {
// cycle through k/v // cycle through k/v
var tok json.Token var tok json.Token
for tok, err = dec.Token(); !errors.Is(err, io.EOF); tok, err = dec.Token() { for tok, err = dec.Token(); !errors.Is(err, io.EOF); tok, err = dec.Token() {
if err != nil && !errors.Is(err, io.EOF) {
return err
}
// we can expect two types: string or Delim. Delim automatically means // we can expect two types: string or Delim. Delim automatically means
// that it is the closing bracket of the object, whereas string means // that it is the closing bracket of the object, whereas string means
// that there is another key. // that there is another key.

View File

@ -84,6 +84,13 @@ var jsonScenarios = []formatScenario{
input: `{"cat": "meow"}`, input: `{"cat": "meow"}`,
expected: "D0, P[], (!!map)::cat: meow\n", expected: "D0, P[], (!!map)::cat: meow\n",
}, },
{
description: "bad json",
skipDoc: true,
input: `{"a": 1 "b": 2}`,
expectedError: `bad file 'sample.yml': invalid character '"' after object key:value pair`,
scenarioType: "decode-error",
},
{ {
description: "Parse json: complex", description: "Parse json: complex",
subdescription: "JSON is a subset of yaml, so all you need to do is prettify the output", subdescription: "JSON is a subset of yaml, so all you need to do is prettify the output",
@ -303,7 +310,13 @@ func testJSONScenario(t *testing.T, s formatScenario) {
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewJSONDecoder(), NewJSONEncoder(0, false, false)), s.description) test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewJSONDecoder(), NewJSONEncoder(0, false, false)), s.description)
case "roundtrip-multi": case "roundtrip-multi":
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewJSONDecoder(), NewJSONEncoder(2, false, false)), s.description) test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewJSONDecoder(), NewJSONEncoder(2, false, false)), s.description)
case "decode-error":
result, err := processFormatScenario(s, NewJSONDecoder(), NewJSONEncoder(2, false, false))
if err == nil {
t.Errorf("Expected error '%v' but it worked: %v", s.expectedError, result)
} else {
test.AssertResultComplexWithContext(t, s.expectedError, err.Error(), s.description)
}
default: default:
panic(fmt.Sprintf("unhandled scenario type %q", s.scenarioType)) panic(fmt.Sprintf("unhandled scenario type %q", s.scenarioType))
} }