Enhancement request for more informative error messages - as simple as "Syntax error in JSON on line foo." would be better than the current message, which is just "Syntax error in JSON."
Description
Details
- Reference
- bz50511
Related Objects
- Mentioned In
- T305105: TemplateData save hook causes a duplicate parse, and is questionable UX
T301854: geoshapes API errors should have diagnostic value
T258790: Old wikitext editor no longer displays error message when :Status::newFatal() is called (in TemplateData) - Mentioned Here
- T256574: TemplateData editor: Bad JSON error message is confusing
T258790: Old wikitext editor no longer displays error message when :Status::newFatal() is called (in TemplateData)
Event Timeline
It is in my opinion not feasible to get line numbers because we use a native PHP extension to parse the JSON.
If the JSON syntax itself is invalid, there is nothing we can do. Best case scenario the parser gives the type of error ("Unexpected comma", "Expected closing curly brace" etc.) but a line number is unlikely.
Once the JSON is parsed, the TemplateData validation itself (e.g. sets referring to existing parameters) we have full control over and gives pretty informative errors with the full path to the property.
Unexpected property "params.user.blabla".
Property "params.timestamp.description" is expected to be of type "string|object".
etc.
weskaggs wrote:
Displaying the contents of the line at which an error is detected, with a few lines around it for context, would be almost as useful as a line number. Of course the actual error can be a long way off from the point at which a syntax error is detected, but the information would still be helpful. Adding information about the type of error would make it even better. Really any information at all about the nature of the error would be an improvement.
For the moment, you can use TemplateDataEditor to be able to create the <templatedata/> without editing any real JSON.
(In reply to comment #2)
Presumably this could be done if bug 50169 was implemented, though. :-)
Well, in case of bug 50169 (VisualEditor plugin for TemplateData) there would never be a syntax error in the JSON because the user wouldn't be editing it directly. They'd be using a form-like interface to interact with it (VE would generate the JSON which is always valid).
However if the VE plugin would have a raw mode to edit the JSON (e.g. using an Ace surface like Extension:CodeEditor does), we could use its JSHint integration to get inline errors by line/char highlighting (e.g. like in Sublime).
This is not the same issue as T256574.
T256574 is about the error message when clicking the "Manage TemplateData" button to open the "Template documentation editor" dialog:
This is about the error message when clicking "Publish changes": (currently this error doesn't appear at all, that's yet another separate issue filed as T258790)
While I agree this is a valid feature request, I'm afraid there isn't much we can do at this point. I tried to annotate the generic error message with what json_last_error_msg() reports. All it says is "Syntax error". 😞 We would need to use another JSON parser to fulfill this feature request. That's unlikely to happen.