jslt
jslt copied to clipboard
Exceptions messages are a bit unclear
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!
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.
The error message is confusing.
Check this https://jsoneditoronline.org/#left=cloud.d7e0a32efe73462eb4c877e69eb98d42
It complains that the ',' is unexpected.
These are also correct JSON objects:
{
"id": []
}
and
{
"id": {}
}
or
{
"id": null
}
or
{
"id": false
}
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?
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.
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
Which version are you using ? I cannot reproduce your error in the playground. I get different error messages, see above.
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.
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!
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
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.
Reopening, because those error messages really could be better than they are.