--- trigger: always_on --- # ADR-007 Error Handling Strategy ## CRITICAL RULES - **ALWAYS** use layered error classification (Validation, Business, System) - **NEVER** expose technical details to end users - **ALWAYS** provide user-friendly error messages with recovery guidance - **ALWAYS** log technical details for debugging - **NEVER** use generic error messages without context ## Error Classification | Error Type | Description | User Message | Technical Log | |------------|-------------|--------------|---------------| | **Validation** | Input validation failures | Clear field-level errors | Full validation details | | **Business** | Business rule violations | Actionable guidance | Business context + user ID | | **System** | Infrastructure failures | Generic "try again" | Full stack trace + metrics | ## Backend Pattern (NestJS) ```typescript // Custom Exception Hierarchy export class BusinessException extends HttpException { constructor( message: string, userMessage: string, recoveryAction?: string, errorCode?: string ) { super({ message, userMessage, recoveryAction, errorCode }, 400); } } // Global Exception Filter @Catch() export class GlobalExceptionFilter implements ExceptionFilter { catch(exception: unknown, host: ArgumentsHost) { // Classify error and provide appropriate response // Log technical details // Return user-friendly message } } ``` ## Frontend Pattern (Next.js) ```typescript // Error Display Component const ErrorDisplay = ({ error, onRetry }) => { const userMessage = error.userMessage || 'เกิดข้อผิดพลาด'; const recoveryAction = error.recoveryAction; return (
{userMessage}
{recoveryAction &&{recoveryAction}
} {onRetry && }