From ed9e17bb0f6bf16330041f7e420b30a5df18f57e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EB=AF=BC=EA=B1=B4=ED=9D=AC?= Date: Tue, 4 Nov 2025 19:57:43 +0900 Subject: [PATCH] =?UTF-8?q?issue=20#=20GlobalExceptionHandler=20=ED=8C=8C?= =?UTF-8?q?=EC=9D=BC=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...ndler.java => GlobalExceptionHandler.java} | 98 ++++++++++--------- .../utils/generator/RandomCodeGenerator.java | 2 +- 2 files changed, 52 insertions(+), 48 deletions(-) rename src/main/java/com/baekyangdan/scheduler/config/{ExceptionHandler.java => GlobalExceptionHandler.java} (84%) diff --git a/src/main/java/com/baekyangdan/scheduler/config/ExceptionHandler.java b/src/main/java/com/baekyangdan/scheduler/config/GlobalExceptionHandler.java similarity index 84% rename from src/main/java/com/baekyangdan/scheduler/config/ExceptionHandler.java rename to src/main/java/com/baekyangdan/scheduler/config/GlobalExceptionHandler.java index bc22001..59464fa 100644 --- a/src/main/java/com/baekyangdan/scheduler/config/ExceptionHandler.java +++ b/src/main/java/com/baekyangdan/scheduler/config/GlobalExceptionHandler.java @@ -1,48 +1,52 @@ -package com.baekyangdan.scheduler.config; - -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.ExceptionHandler; -import org.springframework.web.bind.annotation.RestControllerAdvice; - -import java.time.LocalDateTime; -import java.util.HashMap; -import java.util.Map; - -public class ExceptionHandler { - // 에러 핸들러 목록 - // 1. 비즈니스 로직 관련 예외 처리 - // ex) 중복 데이터 - @ExceptionHandler(BusinessException.class) - public ResponseEntity> handleBusinessException(BusinessException e) { - Map body = new HashMap<>(); - body.put("message", e.getMessage()); - return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(body); - } - - // 2. 입력값 유효성 검사 관련 예외 처리 - // ex) 데이터 타입 검사 - @ExceptionHandler(ValidationException.class) - public ResponseEntity> handleValidationException(ValidationException e) { - Map body = new HashMap<>(); - body.put("message", e.getMessage()); - return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body(body); - } - - // 3. 권한/인가 관련 예외 처리 - // ex) 로그인이 필요한 서비스에 로그인 안 한 사람이 호출 - @ExceptionHandler(AuthException.class) - public ResponseEntity> handleValidationException(ValidationException e) { - Map body = new HashMap<>(); - body.put("message", e.getMessage()); - return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(body); - } - - // 4. 기타 예외 처리 - @ExceptionHandler(Exception.class) - public ResponseEntity> handleDefaultException(Exception e) { - Map body = new HashMap<>(); - body.put("message", e.getMessage()); - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(body); - } +package com.baekyangdan.scheduler.config; + +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +import com.baekyangdan.scheduler.exception.AuthException; +import com.baekyangdan.scheduler.exception.BusinessException; +import com.baekyangdan.scheduler.exception.ValidationException; + +import java.util.HashMap; +import java.util.Map; + +@RestControllerAdvice +public class GlobalExceptionHandler { + // 에러 핸들러 목록 + // 1. 비즈니스 로직 관련 예외 처리 + // ex) 중복 데이터 + @ExceptionHandler(BusinessException.class) + public ResponseEntity> handleBusinessException(BusinessException e) { + Map body = new HashMap<>(); + body.put("message", e.getMessage()); + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(body); + } + + // 2. 입력값 유효성 검사 관련 예외 처리 + // ex) 데이터 타입 검사 + @ExceptionHandler(ValidationException.class) + public ResponseEntity> handleValidationException(ValidationException e) { + Map body = new HashMap<>(); + body.put("message", e.getMessage()); + return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body(body); + } + + // 3. 권한/인가 관련 예외 처리 + // ex) 로그인이 필요한 서비스에 로그인 안 한 사람이 호출 + @ExceptionHandler(AuthException.class) + public ResponseEntity> handleAuthException(ValidationException e) { + Map body = new HashMap<>(); + body.put("message", e.getMessage()); + return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(body); + } + + // 4. 기타 예외 처리 + @ExceptionHandler(Exception.class) + public ResponseEntity> handleDefaultException(Exception e) { + Map body = new HashMap<>(); + body.put("message", e.getMessage()); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(body); + } } \ No newline at end of file diff --git a/src/main/java/com/baekyangdan/scheduler/utils/generator/RandomCodeGenerator.java b/src/main/java/com/baekyangdan/scheduler/utils/generator/RandomCodeGenerator.java index 54541d6..72e42f7 100644 --- a/src/main/java/com/baekyangdan/scheduler/utils/generator/RandomCodeGenerator.java +++ b/src/main/java/com/baekyangdan/scheduler/utils/generator/RandomCodeGenerator.java @@ -11,7 +11,7 @@ public class RandomCodeGenerator { private static final SecureRandom random = new SecureRandom(); public static String generateRandomCode(int length) { - List codeCharacters = ArrayList<>(length); + List codeCharacters = new ArrayList<>(length); for (int i = 0; i < length; i++) { codeCharacters.add(randomChar());