Skip to content

#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere

Answer an unrepairable document with 422, not 500 (#8083)

EthanHealy011805284687ca
diff --git a/app/core/src/main/java/stirling/software/SPDF/exception/GlobalExceptionHandler.java b/app/core/src/main/java/stirling/software/SPDF/exception/GlobalExceptionHandler.java
index ce06769cdf..fb913d663c 100644
--- a/app/core/src/main/java/stirling/software/SPDF/exception/GlobalExceptionHandler.java
+++ b/app/core/src/main/java/stirling/software/SPDF/exception/GlobalExceptionHandler.java
@@ -375,6 +375,28 @@ public class GlobalExceptionHandler {
ex, HttpStatus.UNPROCESSABLE_ENTITY, ErrorTypes.COMPLIANCE_NOT_MET, title, request);
}
+ /**
+ * Handle a damaged document that every available repair tool has declined.
+ *
+ * @param ex the PdfUnrepairableException
+ * @param request the HTTP servlet request
+ * @return ProblemDetail with HTTP 422 UNPROCESSABLE_ENTITY: the request was understood and the
+ * tools ran, the document is simply beyond repair
+ */
+ @ExceptionHandler(PdfUnrepairableException.class)
+ public ResponseEntity<ProblemDetail> handlePdfUnrepairable(
+ PdfUnrepairableException ex, HttpServletRequest request) {
+ // A verdict on the document, not a fault on this server: without its own handler it fell to
+ // the catch-all, which answers 500 and logs at ERROR, so every refusal read as an outage.
+ logException("warn", "PDF Unrepairable", request, ex, ex.getErrorCode());
+
+ String title =
+ getLocalizedMessage(
+ "error.pdfUnrepairable.title", ErrorTitles.PDF_UNREPAIRABLE_DEFAULT);
+ return createProblemDetailResponse(
+ ex, HttpStatus.UNPROCESSABLE_ENTITY, ErrorTypes.PDF_UNREPAIRABLE, title, request);
+ }
+
/**
* Handle FFmpeg dependency missing errors when media conversion endpoints are invoked.
*
@@ -1161,7 +1183,12 @@ public class GlobalExceptionHandler {
public ResponseEntity<ProblemDetail> handleRuntimeException(
RuntimeException ex, HttpServletRequest request) {
- // Check if this RuntimeException wraps a typed exception from job execution
+ // Check if this RuntimeException wraps a typed exception from job execution.
+ //
+ // Must stay in step with the @ExceptionHandler methods above: Spring resolves on the type
+ // thrown, so a wrapped exception reaches this handler and never theirs. A subtype missing
+ // from this ladder falls to handleBaseApp, and the same failure answers 500 when it came
+ // through a job and its own status when it did not.
Throwable cause = ex.getCause();
if (cause instanceof BaseAppException appEx) {
// Delegate to specific BaseAppException handlers
@@ -1169,6 +1196,8 @@ public class GlobalExceptionHandler {
return handlePdfPassword((PdfPasswordException) appEx, request);
} else if (appEx instanceof ComplianceNotMetException complianceEx) {
return handleComplianceNotMet(complianceEx, request);
+ } else if (appEx instanceof PdfUnrepairableException unrepairableEx) {
+ return handlePdfUnrepairable(unrepairableEx, request);
} else if (appEx instanceof PdfCorruptedException
|| appEx instanceof PdfEncryptionException
|| appEx instanceof OutOfMemoryDpiException) {
@@ -1500,6 +1529,7 @@ public class GlobalExceptionHandler {
static final String INVALID_ARGUMENT = "/errors/invalid-argument";
static final String IO_ERROR = "/errors/io-error";
static final String COMPLIANCE_NOT_MET = "/errors/compliance-not-met";
+ static final String PDF_UNREPAIRABLE = "/errors/pdf-unrepairable";
static final String UNEXPECTED = "/errors/unexpected";
}
@@ -1514,6 +1544,7 @@ public class GlobalExceptionHandler {
static final String PDF_ENCRYPTION_DEFAULT = "PDF Encryption Error";
static final String APPLICATION_DEFAULT = "Application Error";
static final String COMPLIANCE_NOT_MET_DEFAULT = "Compliance Standard Not Met";
+ static final String PDF_UNREPAIRABLE_DEFAULT = "PDF Could Not Be Repaired";
static final String CBR_FORMAT_DEFAULT = "Invalid CBR File Format";
static final String CBZ_FORMAT_DEFAULT = "Invalid CBZ File Format";
static final String EML_FORMAT_DEFAULT = "Invalid EML File Format";
diff --git a/app/core/src/test/java/stirling/software/SPDF/controller/api/misc/RepairControllerMoreTest.java b/app/core/src/test/java/stirling/software/SPDF/controller/api/misc/RepairControllerMoreTest.java
index bd7e29415b..1df0747f5e 100644
--- a/app/core/src/test/java/stirling/software/SPDF/controller/api/misc/RepairControllerMoreTest.java
+++ b/app/core/src/test/java/stirling/software/SPDF/controller/api/misc/RepairControllerMoreTest.java
@@ -302,6 +302,30 @@ class RepairControllerMoreTest {
assertFalse(thrown.getMessage().contains("/tmp/repair123.pdf"));
}
}
+
+ @Test
+ @DisplayName("a cancelled job stays cancelled rather than reading as unrepairable")
+ void qpdfInterruptPropagates() throws Exception {
+ when(endpointConfiguration.isGroupEnabled("Ghostscript")).thenReturn(false);
+ when(endpointConfiguration.isGroupEnabled("qpdf")).thenReturn(true);
+
+ try (MockedStatic<ProcessExecutor> mockedFactory = mockStatic(ProcessExecutor.class)) {
+ ProcessExecutor qpdfExecutor = mock(ProcessExecutor.class);
+ when(qpdfExecutor.runCommandWithOutputHandling(any()))
+ .thenThrow(new InterruptedException("cancelled"));
+
+ mockedFactory
+ .when(() -> ProcessExecutor.getInstance(ProcessExecutor.Processes.QPDF))
+ .thenReturn(qpdfExecutor);
+
+ // The catch names IOException and RuntimeException for this reason: widening it to
+ // Exception would swallow the interrupt and file a verdict on a document nothing
+ // finished reading.
+ assertThrows(
+ InterruptedException.class,
+ () -> repairController.repairPdf(pdfFileFrom(inputPdf(1))));
+ }
+ }
}
@Nested
diff --git a/app/core/src/test/java/stirling/software/SPDF/exception/GlobalExceptionHandlerTest.java b/app/core/src/test/java/stirling/software/SPDF/exception/GlobalExceptionHandlerTest.java
index 1e63034665..0cfebcfd8d 100644
--- a/app/core/src/test/java/stirling/software/SPDF/exception/GlobalExceptionHandlerTest.java
+++ b/app/core/src/test/java/stirling/software/SPDF/exception/GlobalExceptionHandlerTest.java
@@ -294,6 +294,24 @@ class GlobalExceptionHandlerTest {
assertEquals(HttpStatus.BAD_REQUEST, resp.getStatusCode());
}
+ @Test
+ void handleRuntimeException_wrapping_PdfUnrepairableException_returns_422() {
+ // Repair runs as a job, so this is the path a real refusal takes. Answering 500 here and
+ // 422 when it is thrown directly would make one outcome two, by route alone.
+ PdfUnrepairableException cause = new PdfUnrepairableException("beyond repair", "E076");
+ RuntimeException ex = new RuntimeException("wrapped", cause);
+ ResponseEntity<ProblemDetail> resp = handler.handleRuntimeException(ex, request);
+ assertEquals(HttpStatus.UNPROCESSABLE_ENTITY, resp.getStatusCode());
+ }
+
+ @Test
+ void handlePdfUnrepairable_returns_422() {
+ PdfUnrepairableException ex = new PdfUnrepairableException("beyond repair", "E076");
+ ResponseEntity<ProblemDetail> resp = handler.handlePdfUnrepairable(ex, request);
+ assertEquals(HttpStatus.UNPROCESSABLE_ENTITY, resp.getStatusCode());
+ assertNotNull(resp.getBody());
+ }
+
@Test
void handleRuntimeException_wrapping_BaseValidationException_returns_400() {
CbrFormatException cause = new CbrFormatException("bad format", "E030");