-
Notifications
You must be signed in to change notification settings - Fork 328
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
Catch generic throwable and pass exception to returned future #1285
Conversation
Kudos, SonarCloud Quality Gate passed! |
@@ -285,6 +285,9 @@ public ListenableFuture<WriteApiResponse> writeJsonStream( | |||
writeApiFutureResponse.setException(e); | |||
// Restore interrupted state in case of an InterruptedException | |||
Thread.currentThread().interrupt(); | |||
} catch (Throwable t) { |
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.
@elefeint Catching a wider exception is a good idea here and thanks for making the changes. However shall we catch Exception
instead? Catching Throwable
results in this Sonar issue: https://sonarcloud.io/project/issues?resolved=false&types=CODE_SMELL&pullRequest=1285&id=GoogleCloudPlatform_spring-cloud-gcp&open=AYPCUFuggImngqXU1xdj
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.
Sonar is right, in general, however the user experience will be significantly worse if we follow this recommendation -- for example, if an unexpected Exception (or even Throwable
such as linkage errors) arises during the separate thread execution, the error will just disappear without leaving a trace.
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.
That causes 2 issues:
- The error disappears without any user-visible evidence.
CompletableFuture
will be left hanging forever.
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.
Makes sense @elefeint
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.
I'm not sure what would be a better way to handle exceptions from the asynchronous task. In this case, catching Throwable
or Exception
seems appropriate.
This PR adds a catch-all for any unforeseen errors and passes the error to the returned future.
In normal code, catching
Throwable
is a bad idea -- the error should be allowed to propagate and stop the app. However, when running in a separate thread, an uncaughtThrowable
will simply disappear without a trace, so we have to catch it and hand the error over to the returned future, so that the failure may be acted upon.Unrelated changes: