-
Notifications
You must be signed in to change notification settings - Fork 80
Fix Sonar violations for InterruptedException handling in waitForRapidResults #1492
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
Fix Sonar violations for InterruptedException handling in waitForRapidResults #1492
Conversation
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.
Pull Request Overview
This PR fixes a Sonar violation related to improper InterruptedException handling in the waitForRapidResults
method. The change ensures that InterruptedException is properly re-thrown rather than being caught and ignored, which addresses the static analysis tool's compliance requirements.
Key changes:
- Added explicit InterruptedException handling that re-throws the exception
- Removed unused import statement
@@ -721,10 +719,11 @@ public List<DeveloperScansScanView> waitForRapidResults(BlackDuckRunData blackDu | |||
mode, | |||
calculateMaxWaitInSeconds(fibonacciSequenceIndex) | |||
); | |||
} catch (InterruptedException e) { | |||
throw new InterruptedException(e.getMessage()); |
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.
Creating a new InterruptedException and throwing it loses the original stack trace and interrupt status. Instead, you should re-interrupt the current thread using Thread.currentThread().interrupt()
and then rethrow the original exception or wrap it in an unchecked exception.
throw new InterruptedException(e.getMessage()); | |
Thread.currentThread().interrupt(); | |
throw new OperationException("Thread was interrupted during rapid scan wait.", e); |
Copilot uses AI. Check for mistakes.
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.
This might be a good suggestion. You could simply throw e;
.
If you follow what auditLog.namedInternal()
does, you'll see that eventually OperationWrapper.wrapped()
is called which handles InterruptedException
as described here.
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.
Thanks. I've updated it as per the suggestion.
@@ -721,10 +719,11 @@ public List<DeveloperScansScanView> waitForRapidResults(BlackDuckRunData blackDu | |||
mode, | |||
calculateMaxWaitInSeconds(fibonacciSequenceIndex) | |||
); | |||
} catch (InterruptedException e) { | |||
throw new InterruptedException(e.getMessage()); | |||
} catch (IntegrationRestException e) { | |||
throw handleRapidScanException(e); | |||
} catch (Exception e) { |
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.
The removal of the error logging statement means that exceptions other than InterruptedException and IntegrationRestException will no longer be logged before being wrapped in OperationException. This could make debugging more difficult as the original exception details may be lost.
} catch (Exception e) { | |
} catch (Exception e) { | |
logger.error("An unexpected error occurred during the rapid wait operation.", e); |
Copilot uses AI. Check for mistakes.
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 had a pending comment on the original MR that I ultimately decided to hold back but now that we are revisiting ... Is it possible to extract the error handling logic out of OperationRunner and move it to a (possibly pre-existing) utility class? Not necessary if it's a big effort, but wondering if you see that potential also (or maybe you already tried and this was a cleaner solution) -- thanks either way!
That's a great suggestion. I originally thought of this while implementing this. Are you talking about the moving the |
You should be able to simply let exceptions bubble up from here and they will be handled by |
The original intent of the ticket was to provide users with a clearer, actionable message when Rapid Scans fail with a
If we allow the exception to bubble up directly to In the current implementation, the exception is intercepted in So while bubbling up is generally preferred for consistency, in this case, intercepting allows us to modify the exception message without disrupting the overall exception handling mechanism. If you can think of any cleaner way to achieve this preserving the enhanced message logic, you can suggest that. |
Sonar build was failing and complaining with the following message after the pull request Enhance error message for BOM_COMPARE mode failures in rapid scan was merged,
This fix ensures sonar build not failing.