Skip to content

Commit

Permalink
[processor] improved error message for non-array EOF
Browse files Browse the repository at this point in the history
  • Loading branch information
draxil committed Aug 2, 2022
1 parent cb8b5e7 commit 0644f46
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
10 changes: 10 additions & 0 deletions processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ func (p processor) handleNonArray(j *json.JSON, clue byte) error {

n, err := j.WriteCurrentTo(out, true)
if err != nil {
if err == io.EOF {
return errNonArrayEOF(guessJSONType(clue))
}
return err
}
if n > 0 {
Expand Down Expand Up @@ -209,6 +212,13 @@ func errNotArrayWas(t string) error {
return fmt.Errorf("expected structure to be an array but found: %s", t)
}

func errNonArrayEOF(t string) error {
if t == "" {
return fmt.Errorf("JSON data ended prematurely")
}
return fmt.Errorf("file ended before the end of value (%s)", t)
}

func errBadPath(chunk string) error {
return fmt.Errorf("path node did not exist: %s", chunk)
}
Expand Down
9 changes: 8 additions & 1 deletion processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,13 @@ func TestProcessor(t *testing.T) {
expErr: errArrayEOF(3),
},

{
name: "array that doesn't end (trailing ws)",
in: sreader("[1, 2, 3, "),
exp: "1\n2\n3\n",
expErr: errArrayEOF(3),
},

{
name: "array that doesn't end - stutter comma",
in: sreader("[1, 2, 3,,"),
Expand Down Expand Up @@ -295,7 +302,7 @@ func TestProcessor(t *testing.T) {
name: "object that doesn't close",
in: sreader(`{"x":`),
exp: `{"x":`,
expErr: io.EOF,
expErr: errNonArrayEOF("object"),
},
// FUTURE: perhaps error
{
Expand Down

0 comments on commit 0644f46

Please sign in to comment.