From c9c44723c2f91dc4c03f0b1a9f55bfc1d49f647b Mon Sep 17 00:00:00 2001 From: Anthony Stirling <77850077+Frooodle@users.noreply.github.com.> Date: Wed, 2 Jul 2025 11:58:29 +0100 Subject: [PATCH] errors --- .../software/common/util/ExceptionUtils.java | 2 +- .../software/common/util/PdfUtils.java | 62 +++++++------------ .../main/resources/messages_en_GB.properties | 6 ++ 3 files changed, 31 insertions(+), 39 deletions(-) diff --git a/common/src/main/java/stirling/software/common/util/ExceptionUtils.java b/common/src/main/java/stirling/software/common/util/ExceptionUtils.java index baea7dd40..400376f7a 100644 --- a/common/src/main/java/stirling/software/common/util/ExceptionUtils.java +++ b/common/src/main/java/stirling/software/common/util/ExceptionUtils.java @@ -311,7 +311,7 @@ public class ExceptionUtils { public static void logException(String operation, Exception e) { if (e instanceof IOException && PdfErrorUtils.isCorruptedPdfError((IOException) e)) { log.warn("PDF corruption detected during {}: {}", operation, e.getMessage()); - } else if (isEncryptionError((IOException) e) || isPasswordError((IOException) e)) { + } else if (e instanceof IOException io && (isEncryptionError(io) || isPasswordError(io))) { log.info("PDF security issue during {}: {}", operation, e.getMessage()); } else { log.error("Unexpected error during {}", operation, e); diff --git a/common/src/main/java/stirling/software/common/util/PdfUtils.java b/common/src/main/java/stirling/software/common/util/PdfUtils.java index 7bf934bb3..9e4f64229 100644 --- a/common/src/main/java/stirling/software/common/util/PdfUtils.java +++ b/common/src/main/java/stirling/software/common/util/PdfUtils.java @@ -139,12 +139,10 @@ public class PdfUtils { // Validate and limit DPI to prevent excessive memory usage final int MAX_SAFE_DPI = 300; // Maximum safe DPI to prevent memory issues if (DPI > MAX_SAFE_DPI) { - throw new IllegalArgumentException( - String.format( - "DPI value %d exceeds maximum safe limit of %d. " - + "High DPI values can cause memory issues and crashes. " - + "Please use a lower DPI value.", - DPI, MAX_SAFE_DPI)); + throw ExceptionUtils.createIllegalArgumentException( + "error.dpiExceedsLimit", + "DPI value {0} exceeds maximum safe limit of {1}. High DPI values can cause memory issues and crashes. Please use a lower DPI value.", + DPI, MAX_SAFE_DPI); } try (PDDocument document = pdfDocumentFactory.load(inputStream)) { @@ -177,12 +175,10 @@ public class PdfUtils { if (e.getMessage() != null && e.getMessage() .contains("Maximum size of image exceeded")) { - throw new IllegalArgumentException( - String.format( - "PDF page %d is too large to render at %d DPI. " - + "Please try a lower DPI value (recommended: 150 or less).", - i + 1, DPI), - e); + throw ExceptionUtils.createIllegalArgumentException( + "error.pageTooBigForDpi", + "PDF page {0} is too large to render at {1} DPI. Please try a lower DPI value (recommended: 150 or less).", + i + 1, DPI); } throw e; } @@ -223,13 +219,10 @@ public class PdfUtils { if (e.getMessage() != null && e.getMessage() .contains("Maximum size of image exceeded")) { - throw new IllegalArgumentException( - String.format( - "PDF page %d is too large to render at %d DPI. " - + "The resulting image would exceed Java's maximum array size. " - + "Please try a lower DPI value (recommended: 150 or less).", - i + 1, DPI), - e); + throw ExceptionUtils.createIllegalArgumentException( + "error.pageTooBigExceedsArray", + "PDF page {0} is too large to render at {1} DPI. The resulting image would exceed Java's maximum array size. Please try a lower DPI value (recommended: 150 or less).", + i + 1, DPI); } throw e; } @@ -266,12 +259,10 @@ public class PdfUtils { if (e.getMessage() != null && e.getMessage() .contains("Maximum size of image exceeded")) { - throw new IllegalArgumentException( - String.format( - "PDF page %d is too large to render at %d DPI. " - + "Please try a lower DPI value (recommended: 150 or less).", - i + 1, DPI), - e); + throw ExceptionUtils.createIllegalArgumentException( + "error.pageTooBigForDpi", + "PDF page {0} is too large to render at {1} DPI. Please try a lower DPI value (recommended: 150 or less).", + i + 1, DPI); } throw e; } @@ -300,12 +291,10 @@ public class PdfUtils { } catch (IllegalArgumentException e) { if (e.getMessage() != null && e.getMessage().contains("Maximum size of image exceeded")) { - throw new IllegalArgumentException( - String.format( - "PDF page %d is too large to render at %d DPI. " - + "Please try a lower DPI value (recommended: 150 or less).", - i + 1, DPI), - e); + throw ExceptionUtils.createIllegalArgumentException( + "error.pageTooBigForDpi", + "PDF page {0} is too large to render at {1} DPI. Please try a lower DPI value (recommended: 150 or less).", + i + 1, DPI); } throw e; } @@ -352,13 +341,10 @@ public class PdfUtils { } catch (IllegalArgumentException e) { if (e.getMessage() != null && e.getMessage().contains("Maximum size of image exceeded")) { - throw new IllegalArgumentException( - String.format( - "PDF page %d is too large to render at 300 DPI. " - + "The resulting image would exceed Java's maximum array size. " - + "Please use a lower DPI value for PDF-to-image conversion.", - page + 1), - e); + throw ExceptionUtils.createIllegalArgumentException( + "error.pageTooBigFor300Dpi", + "PDF page {0} is too large to render at 300 DPI. The resulting image would exceed Java's maximum array size. Please use a lower DPI value for PDF-to-image conversion.", + page + 1); } throw e; } diff --git a/stirling-pdf/src/main/resources/messages_en_GB.properties b/stirling-pdf/src/main/resources/messages_en_GB.properties index 029645cc2..2584b228a 100644 --- a/stirling-pdf/src/main/resources/messages_en_GB.properties +++ b/stirling-pdf/src/main/resources/messages_en_GB.properties @@ -194,6 +194,12 @@ error.invalidFormat=Invalid {0} format: {1} error.endpointDisabled=This endpoint has been disabled by the admin error.urlNotReachable=URL is not reachable, please provide a valid URL +# DPI and image rendering messages +error.dpiExceedsLimit=DPI value {0} exceeds maximum safe limit of {1}. High DPI values can cause memory issues and crashes. Please use a lower DPI value. +error.pageTooBigForDpi=PDF page {0} is too large to render at {1} DPI. Please try a lower DPI value (recommended: 150 or less). +error.pageTooBigExceedsArray=PDF page {0} is too large to render at {1} DPI. The resulting image would exceed Java's maximum array size. Please try a lower DPI value (recommended: 150 or less). +error.pageTooBigFor300Dpi=PDF page {0} is too large to render at 300 DPI. The resulting image would exceed Java's maximum array size. Please use a lower DPI value for PDF-to-image conversion. + # URL and website conversion messages # System requirements messages