issue # 에러 핸들링
This commit is contained in:
@@ -0,0 +1,48 @@
|
|||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package com.baekyangdan.scheduler.exception;
|
||||||
|
|
||||||
|
public class AuthException extends RuntimeException {
|
||||||
|
public AuthException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package com.baekyangdan.scheduler.exception;
|
||||||
|
|
||||||
|
public class BusinessException extends RuntimeException {
|
||||||
|
public BusinessException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package com.baekyangdan.scheduler.exception;
|
||||||
|
|
||||||
|
public class ValidationException extends RuntimeException {
|
||||||
|
public ValidationException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user