issue # GlobalExceptionHandler 파일 수정

This commit is contained in:
민건희
2025-11-04 19:57:43 +09:00
parent 32ea24ead5
commit ed9e17bb0f
2 changed files with 52 additions and 48 deletions

View File

@@ -1,48 +1,52 @@
package com.baekyangdan.scheduler.config; package com.baekyangdan.scheduler.config;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice; import org.springframework.web.bind.annotation.RestControllerAdvice;
import java.time.LocalDateTime; import com.baekyangdan.scheduler.exception.AuthException;
import java.util.HashMap; import com.baekyangdan.scheduler.exception.BusinessException;
import java.util.Map; import com.baekyangdan.scheduler.exception.ValidationException;
public class ExceptionHandler { import java.util.HashMap;
// 에러 핸들러 목록 import java.util.Map;
// 1. 비즈니스 로직 관련 예외 처리
// ex) 중복 데이터 @RestControllerAdvice
@ExceptionHandler(BusinessException.class) public class GlobalExceptionHandler {
public ResponseEntity<Map<String, Object>> handleBusinessException(BusinessException e) { // 에러 핸들러 목록
Map<String, Object> body = new HashMap<>(); // 1. 비즈니스 로직 관련 예외 처리
body.put("message", e.getMessage()); // ex) 중복 데이터
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(body); @ExceptionHandler(BusinessException.class)
} public ResponseEntity<Map<String, Object>> handleBusinessException(BusinessException e) {
Map<String, Object> body = new HashMap<>();
// 2. 입력값 유효성 검사 관련 예외 처리 body.put("message", e.getMessage());
// ex) 데이터 타입 검사 return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(body);
@ExceptionHandler(ValidationException.class) }
public ResponseEntity<Map<String, Object>> handleValidationException(ValidationException e) {
Map<String, Object> body = new HashMap<>(); // 2. 입력값 유효성 검사 관련 예외 처리
body.put("message", e.getMessage()); // ex) 데이터 타입 검사
return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body(body); @ExceptionHandler(ValidationException.class)
} public ResponseEntity<Map<String, Object>> handleValidationException(ValidationException e) {
Map<String, Object> body = new HashMap<>();
// 3. 권한/인가 관련 예외 처리 body.put("message", e.getMessage());
// ex) 로그인이 필요한 서비스에 로그인 사람이 호출 return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body(body);
@ExceptionHandler(AuthException.class) }
public ResponseEntity<Map<String, Object>> handleValidationException(ValidationException e) {
Map<String, Object> body = new HashMap<>(); // 3. 권한/인가 관련 예외 처리
body.put("message", e.getMessage()); // ex) 로그인이 필요한 서비스에 로그인 사람이 호출
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(body); @ExceptionHandler(AuthException.class)
} public ResponseEntity<Map<String, Object>> handleAuthException(ValidationException e) {
Map<String, Object> body = new HashMap<>();
// 4. 기타 예외 처리 body.put("message", e.getMessage());
@ExceptionHandler(Exception.class) return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(body);
public ResponseEntity<Map<String, Object>> handleDefaultException(Exception e) { }
Map<String, Object> body = new HashMap<>();
body.put("message", e.getMessage()); // 4. 기타 예외 처리
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(body); @ExceptionHandler(Exception.class)
} public ResponseEntity<Map<String, Object>> handleDefaultException(Exception e) {
Map<String, Object> body = new HashMap<>();
body.put("message", e.getMessage());
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(body);
}
} }

View File

@@ -11,7 +11,7 @@ public class RandomCodeGenerator {
private static final SecureRandom random = new SecureRandom(); private static final SecureRandom random = new SecureRandom();
public static String generateRandomCode(int length) { public static String generateRandomCode(int length) {
List<Character> codeCharacters = ArrayList<>(length); List<Character> codeCharacters = new ArrayList<>(length);
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
codeCharacters.add(randomChar()); codeCharacters.add(randomChar());