exceptions.md
docs/exceptions.md
1# Exception Handling23## Domain Exception Model45Avoid creating a new exception class for every individual failure mode. Instead, maintain a single domain exception class carrying an error-code enum:67```java8throw new AppException(AppErrorMessage.COMMUNITY_NOT_FOUND);9```1011New failure modes simply require adding a new enum value to `AppErrorMessage`, rather than introducing a new exception class to wire up and handle.1213## Global Exception Handling1415Centralize all error handling through a single `GlobalExceptionHandler` annotated with `@ControllerAdvice` and `@ExceptionHandler`:1617- Single point of truth for mapping domain exceptions and validation failures to HTTP status codes.18- Produces a consistent error response structure across the entire API.19- **No local `@ExceptionHandler` methods in controllers:** Local handlers fragment status mapping and create multiple places to check when responses look unexpected.2021## Logging at the Throw Site2223**Do not log an exception when throwing it.**2425```java26// ✗ Redundant — logs the exception twice27log.error("Task not found with id: {}", id);28throw new AppException(AppErrorMessage.TASK_NOT_FOUND);2930// ✓ Let the handler log it31throw new AppException(AppErrorMessage.TASK_NOT_FOUND);32```3334The `GlobalExceptionHandler` logs the exception in one place. Logging at the throw site writes the exact same failure to the logs twice.35