updates on docs

This commit is contained in:
Anthony Stirling 2025-07-02 14:19:43 +01:00
parent 9e3a7c642d
commit f4e8bbf7a3
2 changed files with 44 additions and 16 deletions

View File

@ -104,7 +104,7 @@ public class ExceptionUtils {
*/ */
public static IOException createIOException( public static IOException createIOException(
String messageKey, String defaultMessage, Exception cause, Object... args) { String messageKey, String defaultMessage, Exception cause, Object... args) {
String message = String.format(defaultMessage, args); String message = messageKey != null ? defaultMessage : String.format(defaultMessage, args);
return new IOException(message, cause); return new IOException(message, cause);
} }
@ -119,7 +119,7 @@ public class ExceptionUtils {
*/ */
public static RuntimeException createRuntimeException( public static RuntimeException createRuntimeException(
String messageKey, String defaultMessage, Exception cause, Object... args) { String messageKey, String defaultMessage, Exception cause, Object... args) {
String message = String.format(defaultMessage, args); String message = messageKey != null ? defaultMessage : String.format(defaultMessage, args);
return new RuntimeException(message, cause); return new RuntimeException(message, cause);
} }
@ -133,7 +133,8 @@ public class ExceptionUtils {
*/ */
public static IllegalArgumentException createIllegalArgumentException( public static IllegalArgumentException createIllegalArgumentException(
String messageKey, String defaultMessage, Object... args) { String messageKey, String defaultMessage, Object... args) {
String message = String.format(defaultMessage, args); // Only format if no translation key provided (for backwards compatibility)
String message = messageKey != null ? defaultMessage : String.format(defaultMessage, args);
return new TranslatableException(message, messageKey, args); return new TranslatableException(message, messageKey, args);
} }

View File

@ -1,14 +1,23 @@
# Exception Handling Guide # Exception Handling Guide
This guide shows how to use the new centralized exception handling utilities for consistent, internationalized error messages. This guide shows how to use the centralized exception handling utilities for consistent error messages with frontend translation support.
## Architecture Overview
The system uses a **backend-frontend translation split**:
- **Backend**: Creates structured JSON error responses with translation keys and English fallbacks
- **Frontend**: Translates error messages to user's language using JavaScript
## New Utilities ## New Utilities
### 1. I18nUtils ### 1. ExceptionUtils
Provides centralized access to Spring MessageSource for internationalized messages. Creates `TranslatableException` instances with structured translation data for frontend.
### 2. ExceptionUtils ### 2. GlobalExceptionHandler
Handles exceptions with internationalized error messages and appropriate logging. Converts exceptions to structured JSON responses with translation information.
### 3. MessageFormatter.js
Frontend utility for translating error messages with placeholder replacement.
## Usage Examples ## Usage Examples
@ -58,17 +67,35 @@ throw ExceptionUtils.createFileProcessingException("merge", originalException);
throw ExceptionUtils.createIOException("error.customKey", "Default message", originalException, arg1, arg2); throw ExceptionUtils.createIOException("error.customKey", "Default message", originalException, arg1, arg2);
``` ```
### Using I18nUtils Directly ### JSON Error Response Format
```java The system returns structured JSON error responses with translation support:
// Get message with current locale
String message = I18nUtils.getMessage("error.pdfCorrupted");
// Get message with arguments ```json
String message = I18nUtils.getMessage("error.pdfCorruptedDuring", "during merge"); {
"error": "Bad Request",
"message": "DPI value 500 exceeds maximum safe limit of 300. High DPI values can cause memory issues and crashes. Please use a lower DPI value.",
"trace": "java.lang.IllegalArgumentException: ...",
"translationKey": "error.dpiExceedsLimit",
"translationArgs": ["500", "300"]
}
```
// Get message with fallback **Key Features:**
String message = I18nUtils.getMessage("error.customKey", "Default message", arg1, arg2); - `message`: English fallback for API consumers that ignore translation
- `translationKey`: Frontend translation key
- `translationArgs`: Arguments for placeholder replacement
- API consumers can rely on `message` for backwards compatibility
### Frontend Translation with MessageFormatter
```javascript
// Translate error messages with placeholder replacement
const displayMessage = window.MessageFormatter.translate(
json.translationKey,
json.translationArgs,
json.message // fallback to original message
);
``` ```
## Controller Pattern ## Controller Pattern