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.

Discussion: should all callbacks be wrapped so that exceptions are caught

See original GitHub issue

I personally strongly believe that all callbacks should be wrapped in try-catch blocks so that even if these 3rd party code sections throw exceptions, the rest of the code continues as normal. This way we don’t have to worry about IF code being interrupted, left in an intermediate, broken state. The plugin’s name and if possible, some sort of gui identifier (eg. the associated class’s simple name) should be included the error messages that the catch blocks produce.

Sometimes the best way to achieve this wrapping/boxing is to not expose a getter to the callback, but a method that calls the callback instead. This breaks all logic that relies on the getters, the real question is whether anything at all relies on it. Eg. GuiItem#getAction() returns a non-null value (it internally creates an empty lambda), in that case relying on the return value is very hard and probably no one does it. It might be possible in connection with nullable getters though.

List of what would be affected: (hopefully this is a complete list)

  • GuiItem#getAction()
  • Pane#onClick (protected variable)
  • Gui#getOnClose()
  • Gui#getOnOutsideClick()
  • Gui#getOnGlobalClick()
  • Gui#getOnBottomClick()
  • Gui#getOnTopClick()

I would be more than happy to implement and PR this, the question is whether I should. This is a place for arguments and counter-arguments.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
stefvanschiecommented, Mar 13, 2020

Let’s keep this open for a little longer - if no one uses the current methods, they can go; if people are using them (and want to continue using those instead of the new callX versions) we can just keep them.

0reactions
stefvanschiecommented, May 30, 2020

I’ve gone through the dependents of this framework and from what I can find, none of them seem to be making use of the getX methods, so replacing them with their equivalent callX method should be fine.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Significant performance hit caused by throwing Errors: should ...
handlers (and callbacks as necessary) should be wrapped in a try/catch to avoid undefined state in core. No. Qv the discussion in #5114....
Read more >
A way of explicitly reporting exceptions - ESDiscuss.org
The first strategy has the problem that a listener throwing an exception prevents later listeners from being notified, which is suboptimal. The second...
Read more >
Why should I not wrap every block in "try"-"catch"?
I consider code that is littered with try / catch blocks to be a good target for code review and refactoring. It indicates...
Read more >
Error handling in Node.js - LogRocket Blog
Deliver reliable software by learning how to handle errors in Node.js using callbacks, promises, and event emitters.
Read more >
Execution not stopping after python exception raised from ...
I'm wrapping a C++ class from cython. The class constructor takes a callback as an argument, and the callback can be later called...
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