From 507d21772d212299514f254471008433a64ed3ff Mon Sep 17 00:00:00 2001 From: Abdur Rahman <90972063+Abdurrahman-shaikh@users.noreply.github.com> Date: Wed, 5 Feb 2025 02:31:41 +0530 Subject: [PATCH] Fix issue #2842: Handle qpdf exit code 3 as success with warnings (#2883) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # 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 <77850077+Frooodle@users.noreply.github.com> --- .../software/SPDF/utils/ProcessExecutor.java | 31 +++++++++++++------ 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/src/main/java/stirling/software/SPDF/utils/ProcessExecutor.java b/src/main/java/stirling/software/SPDF/utils/ProcessExecutor.java index d6d8afd6c02..dd1e7e894e8 100644 --- a/src/main/java/stirling/software/SPDF/utils/ProcessExecutor.java +++ b/src/main/java/stirling/software/SPDF/utils/ProcessExecutor.java @@ -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; @@ -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();