Skip to content

Commit

Permalink
Fix issue #2842: Handle qpdf exit code 3 as success with warnings (#2883
Browse files Browse the repository at this point in the history
)

# Description of Changes

Please provide a summary of the changes, including:

- **What was changed**:
- Modified the `ProcessExecutor` class to accept exit code `3` from
**qpdf** as a success with warnings.
- Added a check to ensure that only **qpdf**’s exit code `3` is treated
as a warning.
- Added a warning log for **qpdf** exit code `3` to provide better
visibility into the repair process.

- **Why the change was made**:
- The repair process was failing when **qpdf** returned exit code `3`,
even though the operation succeeded with warnings. This caused
unnecessary errors for users.
- The changes ensure that PDFs with minor structural issues (e.g.,
mismatched object counts) are still repaired successfully, while logging
warnings for transparency.
- Added a check to ensure that only **qpdf**’s exit code `3` is treated
as a warning, preventing potential issues with other tools that might
use exit code `3` for actual errors.

Closes #2842

---

## Checklist

### General

- [x] I have read the [Contribution
Guidelines](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/CONTRIBUTING.md)
- [x] I have read the [Stirling-PDF Developer
Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/DeveloperGuide.md)
(if applicable)
- [x] I have performed a self-review of my own code
- [x] My changes generate no new warnings

### Testing (if applicable)

- [x] I have tested my changes locally.
- Verified that exit code `3` is only treated as a warning for **qpdf**
and not for other tools.

---

### Additional Notes
- The changes align with **qpdf**'s behavior, where exit code `3`
indicates a successful operation with warnings.
- Added a check to ensure that only **qpdf**’s exit code `3` is treated
as a warning, preventing potential issues with other tools.

Co-authored-by: Anthony Stirling <[email protected]>
  • Loading branch information
Abdurrahman-shaikh and Frooodle authored Feb 4, 2025
1 parent 5bf050d commit 507d217
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions src/main/java/stirling/software/SPDF/utils/ProcessExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,9 @@ public ProcessExecutorResult runCommandWithOutputHandling(
errorReaderThread.join();
outputReaderThread.join();

boolean isQpdf =
command != null && !command.isEmpty() && command.get(0).contains("qpdf");

if (outputLines.size() > 0) {
String outputMessage = String.join("\n", outputLines);
messages += outputMessage;
Expand All @@ -233,20 +236,28 @@ public ProcessExecutorResult runCommandWithOutputHandling(
log.warn("Command error output:\n" + errorMessage);
}
if (exitCode != 0) {
throw new IOException(
"Command process failed with exit code "
+ exitCode
+ ". Error message: "
+ errorMessage);
if (isQpdf && exitCode == 3) {
log.warn("qpdf succeeded with warnings: {}", messages);
} else {
throw new IOException(
"Command process failed with exit code "
+ exitCode
+ ". Error message: "
+ errorMessage);
}
}
}

if (exitCode != 0) {
throw new IOException(
"Command process failed with exit code "
+ exitCode
+ "\nLogs: "
+ messages);
if (isQpdf && exitCode == 3) {
log.warn("qpdf succeeded with warnings: {}", messages);
} else {
throw new IOException(
"Command process failed with exit code "
+ exitCode
+ "\nLogs: "
+ messages);
}
}
} finally {
semaphore.release();
Expand Down

0 comments on commit 507d217

Please sign in to comment.