question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot gracefully handle parser error with onError option or try-catch

See original GitHub issue

First off; thank you for the great library! It’s been a pleasure to work with so far.

This issue pertains to the React component using the React presets. I am experiencing a runtime error when specific (not all) improper BBCode is passed to the parser, and am unable to gracefully handle the error using onError or try-catch block. I’ve created a sandbox to illustrate the issue.

Sandbox: https://codesandbox.io/s/purple-cloud-k8fjl?file=/src/index.js In this sandbox, I’m using BBCode component in React to render the BBCode [b]Testing[/b][hr].

Expected behavior: It should process the [b]Testing[/b] and leave the problematic [hr] unprocessed. So I expect it to render the following: Testing[hr]

It should also either trigger an onError() call, or the code in the catch block of the try-catch. Ideally we want it handled in onError(), but I included the try-catch in this sandbox because it’s weird that the try-catch does not catch it. Maybe that can indicate something about the cause of the problem.

Actual behavior: Neither the onError() function nor the catch block are called. The application crashes with a runtime error. The specific error depends on what specifically broke the parser. In my example I use "[hr]" and it throws:

Invariant Violation
hr is a void element tag and must neither have `children` nor use `dangerouslySetInnerHTML`.
    in hr (created by Component)
    in span (created by Component)
    in Component (at src/index.js:10)
    in App (at src/index.js:25)

[hr] is not the only tag that causes this; it also breaks on [br] and [col], so it seems to be a special case and there may be other cases. It does not break on all improper BBCode but I am not sure what the conditions are that trigger this.

It is possible this is not an issue with bbob but an error in my own code; if so I would be grateful for any help with what I did wrong, and how to properly handle it to get the behavior I want 😃

Thank you again!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
JiLiZARTcommented, Nov 17, 2020
0reactions
JiLiZARTcommented, Dec 4, 2020

Yes, you right. Error occurs in React JSX. So it can be fixed in bbob-react package. (escape invalid chars, trim tag names, etc)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Proper way to catch exception from JSON.parse - Stack Overflow
i post something into an iframe then read back the contents of the iframe with json parse...so sometimes it's not a json string....
Read more >
Full Stack Error Handling with GraphQL and Apollo
If networkError is present in your response, it means your entire query was rejected, and therefore no data was returned. For example, the ......
Read more >
Using .then(), .catch(), .finally() to Handle Errors in Javascript ...
This is a “parsetime error” as the code cannot be parsed properly. try... catch can only catch errors which occur at runtime (after...
Read more >
Error handling, "try...catch" - The Modern JavaScript Tutorial
The JavaScript engine first reads the code, and then runs it. The errors that occur on the reading phase are called “parse-time” errors...
Read more >
Node.js Error Handling Best Practices - Sematext
If you use a try-catch block you can handle the error gracefully. Otherwise, the app usually crashes, unless you have a fallback for...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found