From 293cd7b0182d07bc600ad569efb5b87816f69d65 Mon Sep 17 00:00:00 2001 From: forge-bot Date: Tue, 14 Jul 2026 08:00:08 +0000 Subject: [PATCH] =?UTF-8?q?TA=20=EC=97=AD=ED=95=A0=20Spring=20=EA=B2=BD?= =?UTF-8?q?=EA=B3=84=20smoke=20(role-ta-live-v2-001)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../exception/GlobalExceptionHandler.java | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 src/main/java/com/runtimematrix/exception/GlobalExceptionHandler.java diff --git a/src/main/java/com/runtimematrix/exception/GlobalExceptionHandler.java b/src/main/java/com/runtimematrix/exception/GlobalExceptionHandler.java new file mode 100644 index 0000000..3dc965b --- /dev/null +++ b/src/main/java/com/runtimematrix/exception/GlobalExceptionHandler.java @@ -0,0 +1,75 @@ +package com.runtimematrix.exception; + +import com.runtimematrix.dto.ErrorResponse; +import jakarta.servlet.http.HttpServletRequest; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.MethodArgumentNotValidException; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +@RestControllerAdvice +public class GlobalExceptionHandler { + + private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class); + + @ExceptionHandler(BusinessException.class) + public ResponseEntity handleBusinessException( + BusinessException ex, HttpServletRequest request) { + log.error("Business exception: code={}, message={}", ex.getCode(), ex.getMessage()); + + ErrorResponse response = new ErrorResponse( + ex.getCode(), + ex.getMessage(), + ex.getTimestamp().toString(), + request.getRequestURI() + ); + + HttpStatus status = determineHttpStatus(ex); + return ResponseEntity.status(status).body(response); + } + + @ExceptionHandler(MethodArgumentNotValidException.class) + public ResponseEntity handleValidationException( + MethodArgumentNotValidException ex, HttpServletRequest request) { + String message = ex.getBindingResult().getFieldErrors().stream() + .map(error -> error.getField() + ": " + error.getDefaultMessage()) + .reduce((a, b) -> a + "; " + b) + .orElse("Validation failed"); + + ErrorResponse response = new ErrorResponse( + "VALIDATION_ERROR", + message, + Instant.now().toString(), + request.getRequestURI() + ); + + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(response); + } + + @ExceptionHandler(Exception.class) + public ResponseEntity handleGenericException( + Exception ex, HttpServletRequest request) { + log.error("Unexpected exception", ex); + + ErrorResponse response = new ErrorResponse( + "ERR_INTERNAL", + "내부 서버 오류가 발생했습니다", + Instant.now().toString(), + request.getRequestURI() + ); + + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response); + } + + private HttpStatus determineHttpStatus(BusinessException ex) { + String code = ex.getCode(); + if (code.startsWith("VALIDATION_")) return HttpStatus.BAD_REQUEST; + if (code.startsWith("AUTH_")) return HttpStatus.UNAUTHORIZED; + if (code.startsWith("CONFLICT_")) return HttpStatus.CONFLICT; + if (code.startsWith("ERR_NOT_FOUND")) return HttpStatus.NOT_FOUND; + return HttpStatus.INTERNAL_SERVER_ERROR; + } +}