jslt icon indicating copy to clipboard operation
jslt copied to clipboard

Exceptions messages are a bit unclear

Open Coder-DG opened this issue 4 years ago • 12 comments

Hi,

Take for example this invalid JSLT:

{
  "id" : ,
}

This will produce this exception (using the demo playground):

com.schibsted.spt.data.jslt.JsltException: Parse error: Encountered " "," ", "" at line 2, column 10.

How do you read whatever is between Encountered and at line 2,...? It's kind of confusing IMO.

Let me know what do you think, thank you for this great library!

Coder-DG avatar Dec 10 '20 15:12 Coder-DG

It means that you have to provide a value after the ':', and no extra comma at the end, if there are no more attributes aside from "id".

Either

{
   "id": 11335452577
}

or

{
      "id": "gwsetuu6w4egb2"
}

are correct JSON objects.

catull avatar Dec 10 '20 16:12 catull

The error message is confusing.

Check this https://jsoneditoronline.org/#left=cloud.d7e0a32efe73462eb4c877e69eb98d42

It complains that the ',' is unexpected.

catull avatar Dec 10 '20 16:12 catull

These are also correct JSON objects:

{
  "id": []
}

and

{
  "id": {}
}

or

{
  "id": null
}

or

{
  "id": false
}

catull avatar Dec 10 '20 16:12 catull

It means that you have to provide a value after the ':', and no extra comma at the end, if there are no more attributes aside from "id".

How do you understand that from just looking at the " "," ", "" part of the exception?

Coder-DG avatar Dec 10 '20 16:12 Coder-DG

There is a grammar that the parser tells you about, in short-hand notation.

It must be

double-quote + string + double-quote + colon + value

"value" itself can be a primitive (7, 3.4, false, "A", "Hello you!" etc)

You only provided

double-quote + id + double-quote + colon + comma (leaving out the extra white-spaces)

The parser reads .....

value interpretation status
" first double-quote OKAY
id attribute name OKAY
" second double-quote OKAY
space reported as " ", it is allowed OKAY
: the colon is not reported silently skipped
space it is reported as " ", it is allowed OKAY
no value this is where it reports "" - the empty string ERROR

it expected a value, either another object, array, or primitive value - number, letter, string or false

You have to squint.

Seriously, I only know this because I ran into it before.

catull avatar Dec 10 '20 16:12 catull

Using this in the playgound [1]

{
  "id":,
}

results in this error message:

com.fasterxml.jackson.core.JsonParseException: Unexpected character (',' (code 44)): expected a valid value (JSON String, Number, Array, Object or token 'null', 'true' or 'false')
 at [Source: (String)"{
  "id":,
}
"; line: 2, column: 9]

This simply tells you the extra comma is not appreciated. So you remove it.

leaving

{
  "id":
}

which produces this error in [1]

com.fasterxml.jackson.core.JsonParseException: Unexpected character ('}' (code 125)): expected a valid value (JSON String, Number, Array, Object or token 'null', 'true' or 'false')
 at [Source: (String)"{
  "id":
}"; line: 3, column: 2]

this tells you the closing brace ("}") appears too early, a value is expected before that brace.

[1] https://www.garshol.priv.no/jslt-demo

catull avatar Dec 10 '20 17:12 catull

Which version are you using ? I cannot reproduce your error in the playground. I get different error messages, see above.

catull avatar Dec 10 '20 17:12 catull

If you put it in the JSLT source field in the playground you get:

com.schibsted.spt.data.jslt.JsltException: Parse error: Encountered " "," ", "" at line 2, column 8.
Was expecting one of: ...

I totally agree this is difficult to read. Unfortunately, the tricky part:

Encountered " "," ", ""

comes from javacc, so it's a bit of work to change. The code that generates the message is:

    String retval = "Encountered \"";
    Token tok = currentToken.next;
    for (int i = 0; i < maxSize; i++) {
      if (i != 0) retval += " ";
      if (tok.kind == 0) {
        retval += tokenImage[0];
        break;
      }
      retval += " " + tokenImage[tok.kind];
      retval += " \"";
      retval += add_escapes(tok.image);
      retval += " \"";
      tok = tok.next; 
    }

It would be possible to catch it and use our own code to create our own exception message, since all the attributes of ParseException are available. I'm not sure it's worth it, but it could be done.

larsga avatar Dec 10 '20 17:12 larsga

It would be possible to catch it and use our own code to create our own exception message, since all the attributes of ParseException are available. I'm not sure it's worth it, but it could be done.

I thought it'd be easier, I was afraid it came from a 3rd party lib and not from your code. It isn't really important to me, just thought I'd bring it up.

Thanks!

Coder-DG avatar Dec 10 '20 18:12 Coder-DG

Which version are you using ? I cannot reproduce your error in the playground. I get different error messages, see above.

That's weird. I've experienced a similar error with JSLT 0.1.11, and on the demo playground site: http://www.garshol.priv.no/jslt-demo

Coder-DG avatar Dec 10 '20 18:12 Coder-DG

You entered the JSON object in the JSTL text area in the playground.

I, OTOH, entered the same JSON object in the Input text area. That explains the different errors experienced.

catull avatar Dec 12 '20 16:12 catull

Reopening, because those error messages really could be better than they are.

larsga avatar Dec 12 '20 21:12 larsga