Skip to content

fix(license): improve exception handling in uploadLicense stream cleanup#3896

Open
melbiialy wants to merge 1 commit intoeclipse-sw360:mainfrom
melbiialy:fix/license-upload-stream-cleanup-and-validation
Open

fix(license): improve exception handling in uploadLicense stream cleanup#3896
melbiialy wants to merge 1 commit intoeclipse-sw360:mainfrom
melbiialy:fix/license-upload-stream-cleanup-and-validation

Conversation

@melbiialy
Copy link
Contributor

Summary

This PR fixes a critical exception handling flaw in the uploadLicense() method where the original exception could be lost during stream cleanup failures.

Problem:

The finally block would throw a new IOException during cleanup, replacing and hiding the actual import failure exception, making debugging extremely difficult.

Solution:

  • Added null/empty file validation
  • Refactored exception handling to preserve primary exceptions
  • Extracted cleanup logic to separate method
  • Close failures are now added as suppressed exceptions

Changed from:

finally { if (closeFailure != null) throw closeFailure; }

To:

catch (Exception e) { primaryException = e; throw e; }
finally { closeInputMapStreams(inputMap, primaryException); }

Dependencies
No new dependencies added.

Issue: #3895

Suggest Reviewer

@GMishx

The uploadLicense() method in Sw360LicenseService had a critical flaw
where the original exception could be lost when stream cleanup failed.
The finally block would throw a new IOException during cleanup, replacing
and hiding the actual import failure exception, making debugging extremely
difficult.

Added null/empty file validation at the start of the method. Refactored
exception handling to use a primaryException variable that preserves the
original error. Extracted stream cleanup logic to a separate method
closeInputMapStreams() that adds close failures as suppressed exceptions
instead of replacing the primary exception.

Changed from:
  finally { if (closeFailure != null) throw closeFailure; }
To:
  catch (Exception e) { primaryException = e; throw e; }
  finally { closeInputMapStreams(inputMap, primaryException); }

This ensures that if license import fails with "Invalid license format"
and stream close fails with "Stream already closed", both errors are
preserved with the import error as primary and close error as suppressed.

This fix improves debuggability of license upload failures and follows
Java exception handling best practices for resource cleanup.

Signed-off-by: melbiialy <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant