Skip to content

Narrow catch clause in createArchive() - #561

Open
elharo wants to merge 1 commit into
apache:masterfrom
elharo:fix-catch-exception
Open

Narrow catch clause in createArchive()#561
elharo wants to merge 1 commit into
apache:masterfrom
elharo:fix-catch-exception

Conversation

@elharo

@elharo elharo commented Jul 19, 2026

Copy link
Copy Markdown
Contributor

Fixes #559

Replaces the broad catch (Exception e) in createArchive() with catch (MavenArchiverException | ArchiverException e), which are the specific checked exceptions declared by MavenArchiver.createArchive(). The // TODO: improve error handling comment is removed since the exception type is now correctly scoped.

return jarFile;
} catch (Exception e) {
// TODO: improve error handling
} catch (MavenArchiverException e) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ArchiverException needs to be caught too. Just because it can propagate does not mean it should propagate

@elharo
elharo marked this pull request as draft July 19, 2026 16:32
…ArchiverException

Instead of catching the broad Exception type, catch only the specific
exceptions that can actually be thrown: MavenArchiverException (checked
exception from createArchive()) and ArchiverException (RuntimeException
from addDirectory()). The TODO comment is removed since the exception
types are now correctly scoped.
@elharo
elharo force-pushed the fix-catch-exception branch from 897796f to 963d50f Compare July 19, 2026 16:39
@elharo
elharo marked this pull request as ready for review July 19, 2026 16:45
@elharo
elharo requested review from Copilot and cstamas July 19, 2026 16:46

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 addresses issue #559 by narrowing exception handling in AbstractJarMojo#createArchive() so that it no longer catches a generic Exception while assembling the JAR, improving error specificity and avoiding unintended swallowing of unrelated runtime failures.

Changes:

  • Replace catch (Exception e) with a narrower multi-catch for MavenArchiverException and ArchiverException.
  • Remove the // TODO: improve error handling comment now that the catch block is scoped to archiver-related exceptions.
  • Add the required ArchiverException import.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +292 to 293
} catch (MavenArchiverException | ArchiverException e) {
throw new MojoException("Error assembling JAR", e);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be addressed

@elharo elharo changed the title Narrow catch clause in createArchive() to MavenArchiverException Narrow catch clause in createArchive() Jul 19, 2026
@elharo
elharo requested a review from ascheman August 26, 2026 10:47
@elharo
elharo requested a review from slachiewicz September 3, 2026 12:10
@slachiewicz
slachiewicz removed their request for review September 6, 2026 19:55
@ascheman

ascheman commented Sep 7, 2026

Copy link
Copy Markdown
Contributor

@elharo — reviewing as requested.

The narrowing itself looks correct: catch (MavenArchiverException | ArchiverException e) still compiles against throws MojoException, so no checked exception leaks, and I agree archiver failures should be wrapped rather than propagated raw.

My main point is sequencing rather than the diff: I'd suggest we merge #508 first and then re-evaluate #561. #508 rewrites createArchive() to write archive content via the JDK jar ToolProvider (keeping MavenArchiver only for the manifest), which removes both the catch block this PR narrows and the addDirectory(...) call on the following line. So once #508 lands, #561 is likely superseded entirely — re-evaluating afterwards avoids a guaranteed conflict and some throwaway work.

If #561 does proceed independently of #508, two small follow-ups:

  • The PR description no longer matches the code (also the open "To be addressed" thread): it says the catch is narrowed to only MavenArchiverException and that ArchiverException is a RuntimeException that propagates — but the code multi-catches both, and plexus ArchiverException is a checked exception (extends Exception). Worth aligning the description with the code.
  • Scout rule: the adjacent addDirectory(File, String[], String[]) is @Deprecated in plexus-archiver 4.12.0 (all four File-based overloads — "Will go away in next major version"). The non-deprecated form is:
    archiver.getArchiver().addFileSet(
        DefaultFileSet.fileSet(contentDirectory.toFile())
            .includeExclude(getIncludes(), getExcludes()));

Both are moot if we take the #508-first route.

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.

Catch Exception is too broad in createArchive()

4 participants