-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
[smhi] Fix exception in aggregation function when daily forecast is empty #10851
Conversation
Catching a division by zero exception, raised in your own code isn't a pretty good solution. Better check if the divisor is zero beforehand. |
That's what I did, the method returns Lines 41 to 43 in b4a61f5
The catch-all is just so the binding won't crash if there are other exceptions thrown that I might have missed. |
Ok, I missed that. Catching unchecked exceptions in binding's scheduler threads is not necessary as the framework will catch and log them. |
Yes, but if the exception is caught by the scheduler it stops scheduling new tasks, so the binding stops updating. |
I know that it's probably bad practice to catch Exception, but is there a better way to prevent overlooked bugs from causing the binding to stop working? |
Are you sure the periodic task is cancelled completely? Actually, we came to the conclusion in openhab/openhab-core#2392 that this is covered for bindings. |
The user who reported the issue had the binding stop updating, and I had a different issue earlier (which is already patched) that also caused the same symptom. The discussion you linked only seems to suggest that the exceptions are logged, not that the execution of the scheduled tasks continue? Should this be filed as an issue in oh core? I can see cases where you don't want the task to continue be scheduled, e.g. to avoid tasks that consistently throws exception to continue to run, possibly hogging resources (which I admit would also be the case if everyone catches Exception in all tasks) |
Also catch any Exception to prevent thread from crashing Signed-off-by: Anders Alfredsson <[email protected]>
You're right. The exception is logged and the periodic task is terminated. I think is is okay behavior. It's ok to let the binding crash (loudly) if it encounters a bug that needs to be fixed. I'd remove the generic try-catch if it was my code as I think bugs should be fixed and not handled. I leave it up to you if you keep it or not. But if you keep it, you should make the log level error. |
I totally agree, but I also want to spare users from problems caused by my mistakes. I narrowed down the try-catch to |
Improve tests to cover different forecast scenarios Signed-off-by: Anders Alfredsson <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
…mpty (openhab#10851) * Fix exception in aggregation function when daily forecast is empty Also catch any Exception to prevent thread from crashing Signed-off-by: Anders Alfredsson <[email protected]> * Refactor to improve robustness of calculations Improve tests to cover different forecast scenarios Signed-off-by: Anders Alfredsson <[email protected]>
…mpty (openhab#10851) * Fix exception in aggregation function when daily forecast is empty Also catch any Exception to prevent thread from crashing Signed-off-by: Anders Alfredsson <[email protected]> * Refactor to improve robustness of calculations Improve tests to cover different forecast scenarios Signed-off-by: Anders Alfredsson <[email protected]> Signed-off-by: Luca Calcaterra <[email protected]>
…mpty (openhab#10851) * Fix exception in aggregation function when daily forecast is empty Also catch any Exception to prevent thread from crashing Signed-off-by: Anders Alfredsson <[email protected]> * Refactor to improve robustness of calculations Improve tests to cover different forecast scenarios Signed-off-by: Anders Alfredsson <[email protected]> Signed-off-by: Luca Calcaterra <[email protected]>
…mpty (openhab#10851) * Fix exception in aggregation function when daily forecast is empty Also catch any Exception to prevent thread from crashing Signed-off-by: Anders Alfredsson <[email protected]> * Refactor to improve robustness of calculations Improve tests to cover different forecast scenarios Signed-off-by: Anders Alfredsson <[email protected]> Signed-off-by: Luca Calcaterra <[email protected]>
…mpty (openhab#10851) * Fix exception in aggregation function when daily forecast is empty Also catch any Exception to prevent thread from crashing Signed-off-by: Anders Alfredsson <[email protected]> * Refactor to improve robustness of calculations Improve tests to cover different forecast scenarios Signed-off-by: Anders Alfredsson <[email protected]>
…mpty (openhab#10851) * Fix exception in aggregation function when daily forecast is empty Also catch any Exception to prevent thread from crashing Signed-off-by: Anders Alfredsson <[email protected]> * Refactor to improve robustness of calculations Improve tests to cover different forecast scenarios Signed-off-by: Anders Alfredsson <[email protected]>
…mpty (openhab#10851) * Fix exception in aggregation function when daily forecast is empty Also catch any Exception to prevent thread from crashing Signed-off-by: Anders Alfredsson <[email protected]> * Refactor to improve robustness of calculations Improve tests to cover different forecast scenarios Signed-off-by: Anders Alfredsson <[email protected]>
Also catch any Exception to prevent thread from crashing
Fixes #10850
Signed-off-by: Anders Alfredsson [email protected]