parse-xml
parse-xml copied to clipboard
Include additional structured information in error objects
Thanks for this library, its the only one I've found with useful, descriptive error handling besides the DOMParser.
Would it be possible to extend the error object to break down the message a bit more? What I have in mind is to go from this:
Unclosed start tag for element `text` (line 1, column 243) iddle text-anchor="middle" font-size="200px" fill= ^
To this:
{
errorCode: 0,
message: Unclosed start tag for element,
element: 'text',
line: 1,
col, 243,
snippet: `middle text-anchor="middle" font-size="200px" fill=`
}
I know some of those entries are already in the object, which is great. Having the extra ones, like errorCode
, message
and element
or similar would help with UI work.
Thanks for the suggestion!
For the sake of discussion, here's an accurate example of the current structure of a parse-xml error object:
{
message: 'Unclosed start tag for element `unclosed` (line 1, column 15)\n' +
' <xml><unclosed</xml>\n' +
' ^\n',
column: 15,
excerpt: '<xml><unclosed</xml>',
line: 1,
pos: 14
}
parse-xml errors don't have numeric codes associated with them, so there wouldn't be anything to include in an errorCode
property. What you've labeled "snippet" is available via the excerpt
property, and "col" is available as column
.
It sounds like the main things you're looking for are a short, generic version of the message without formatting (like "Unclosed start tag for element") and an element
property containing the name of the unclosed element. Is that correct?
One difficulty is that not all error messages are associated with elements. Some are associated with attributes, entities, characters, comments, etc. Others aren't associated with any specific part of the document because they occur when the input is invalid. It may be difficult to represent all possible cases in a useful way.
It might help to understand how you're hoping to represent these errors in a UI.
Thanks for the reply!
Here's the UI I'd ideally like to make using the error you gave as an example.
A short, generic message + element property would be great. That way I can present the error to users and forward them to the line/col in my editor interface when they click the [...] button in the error.
If there were unique data-structures associated with error objects, having error codes would offer an easy way to case/switch between error message components on a frontend, as opposed to checking if properties exist.
It looks like you should be able to achieve that UI using the existing error objects by stripping off the end of the message
string, starting with the line and column numbers. Here's a regex that would do it:
let strippedMessage = error.message.replace(/\s+\(line [\s\S]+/, '');
// => 'Unclosed start tag for element `unclosed`'
This should work with all parse-xml errors, since they all follow the same format.
While I agree that adding error codes could make it easier for consumers to identify error types, one hesitation I have about this is that there are actually quite a few possible error types (more than 30). Exporting constants for 30+ error codes would increase the size of the library, and I suspect most people wouldn't use them. I'll give it some thought though.
That works for me, thanks!
Awesome! I'll go ahead and close this since it sounds like there's nothing more to do here. 😄
Reopening since there's been a second request for error codes, so it sounds like that may be worth looking into.
I am particularly interested in a code
for errors, so their kind can be categorised some way even though their text is a bit different.
HTML has slowly started defining their “parse errors” (even though HTML does not have actual errors). Here’s a short list of examples: https://github.com/syntax-tree/hast-util-from-html#optionskey-in-errorcode. Might be useful as inspiration?