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;
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<Map<String, Object>> handleBusinessException(BusinessException e) {
Map<String, Object> body = new HashMap<>();
body.put("message", e.getMessage());
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(body);
}
// 2. 입력값 유효성 검사 관련 예외 처리
// ex) 데이터 타입 검사
@ExceptionHandler(ValidationException.class)
public ResponseEntity<Map<String, Object>> handleValidationException(ValidationException e) {
Map<String, Object> body = new HashMap<>();
body.put("message", e.getMessage());
return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body(body);
}
// 3. 권한/인가 관련 예외 처리
// ex) 로그인이 필요한 서비스에 로그인 사람이 호출
@ExceptionHandler(AuthException.class)
public ResponseEntity<Map<String, Object>> handleValidationException(ValidationException e) {
Map<String, Object> body = new HashMap<>();
body.put("message", e.getMessage());
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(body);
}
// 4. 기타 예외 처리
@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);
}
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<Map<String, Object>> handleBusinessException(BusinessException e) {
Map<String, Object> body = new HashMap<>();
body.put("message", e.getMessage());
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(body);
}
// 2. 입력값 유효성 검사 관련 예외 처리
// ex) 데이터 타입 검사
@ExceptionHandler(ValidationException.class)
public ResponseEntity<Map<String, Object>> handleValidationException(ValidationException e) {
Map<String, Object> body = new HashMap<>();
body.put("message", e.getMessage());
return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body(body);
}
// 3. 권한/인가 관련 예외 처리
// ex) 로그인이 필요한 서비스에 로그인 사람이 호출
@ExceptionHandler(AuthException.class)
public ResponseEntity<Map<String, Object>> handleAuthException(ValidationException e) {
Map<String, Object> body = new HashMap<>();
body.put("message", e.getMessage());
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(body);
}
// 4. 기타 예외 처리
@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();
public static String generateRandomCode(int length) {
List<Character> codeCharacters = ArrayList<>(length);
List<Character> codeCharacters = new ArrayList<>(length);
for (int i = 0; i < length; i++) {
codeCharacters.add(randomChar());