Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include additional structured information in error objects #26

Open
g-a-v-i-n opened this issue Jan 19, 2023 · 7 comments
Open

Include additional structured information in error objects #26

g-a-v-i-n opened this issue Jan 19, 2023 · 7 comments

Comments

@g-a-v-i-n
Copy link

g-a-v-i-n commented Jan 19, 2023

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.

@rgrove
Copy link
Owner

rgrove commented Jan 19, 2023

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.

@rgrove rgrove changed the title Option to return errors instead of throw Include additional structured information in error objects Jan 19, 2023
@g-a-v-i-n
Copy link
Author

Thanks for the reply!

Here's the UI I'd ideally like to make using the error you gave as an example.
Frame 23

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.

@rgrove
Copy link
Owner

rgrove commented Jan 19, 2023

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.

@g-a-v-i-n
Copy link
Author

That works for me, thanks!

@rgrove
Copy link
Owner

rgrove commented Jan 19, 2023

Awesome! I'll go ahead and close this since it sounds like there's nothing more to do here. 😄

@rgrove rgrove closed this as not planned Won't fix, can't repro, duplicate, stale Jan 19, 2023
@rgrove
Copy link
Owner

rgrove commented Jan 23, 2023

Reopening since there's been a second request for error codes, so it sounds like that may be worth looking into.

@wooorm
Copy link

wooorm commented Jan 23, 2023

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants