devGuide/EXCEPTION_HANDLING_GUIDE.md
This guide outlines the common error handling patterns used within Stirling-PDF and provides tips for internationalising error messages. The examples cover the main languages found in the project: Java, JavaScript, HTML/CSS, and a small amount of Python.
Java forms the core of Stirling-PDF. When adding new features or handling errors:
try-with-resources when working with streams or other closable resources to ensure clean-up even on failure.ResponseStatusException or using @ExceptionHandler methods.messages_en_GB.properties and referencing them with message keys.On the client side, JavaScript handles form validation and user interactions.
try/catch around asynchronous operations (e.g., fetch) and display a translated error notice if the call fails.HTML templates should reserve a space for displaying error messages. Example pattern:
<div class="error" role="alert" th:text="${errorMessage}"></div>
Use CSS classes (e.g., .error) to style the message so it is clearly visible and accessible. Keep the markup simple to ensure screen readers can announce the error correctly.
Python scripts in this project are mainly for utility tasks. Follow these guidelines:
try/except blocks.Example:
try:
perform_task()
except Exception as err:
logger.error("Task failed: %s", err)
print(gettext("task.error"))
All user-visible error strings should be defined in the main translation file (messages_en_GB.properties). Other language files will use the same keys. Refer to messages in code rather than hard-coding text.
When creating new messages:
messages_en_GB.properties.Following these patterns helps keep Stirling-PDF stable, easier to debug, and friendly to users in all supported languages.