Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/main/java/com/Rootin/global/exception/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ public enum ErrorCode {

// Common
INVALID_INPUT(HttpStatus.BAD_REQUEST, "잘못된 입력입니다."),
MISSING_REQUEST_HEADER(HttpStatus.BAD_REQUEST, "필수 헤더가 누락되었습니다."),
UNAUTHORIZED(HttpStatus.UNAUTHORIZED, "인증이 필요합니다."),
FORBIDDEN(HttpStatus.FORBIDDEN, "접근 권한이 없습니다."),
NOT_FOUND(HttpStatus.NOT_FOUND, "리소스를 찾을 수 없습니다."),
METHOD_NOT_ALLOWED(HttpStatus.METHOD_NOT_ALLOWED, "지원하지 않는 HTTP 메서드입니다."),
CONFLICT(HttpStatus.CONFLICT, "이미 처리된 요청이거나 데이터 제약 조건에 위배됩니다."),
INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "서버 오류가 발생했습니다."),

// User
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@

import com.Rootin.global.common.ApiResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingRequestHeaderException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.servlet.resource.NoResourceFoundException;

@Slf4j
@RestControllerAdvice(basePackages = "com.Rootin")
Expand Down Expand Up @@ -40,6 +46,54 @@ public ResponseEntity<ApiResponse<Void>> handleHttpMessageNotReadableException(H
.body(ApiResponse.error(ErrorCode.INVALID_INPUT.getMessage()));
}

@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
public ResponseEntity<ApiResponse<Void>> handleMethodNotAllowed(HttpRequestMethodNotSupportedException e) {
log.warn("HttpRequestMethodNotSupportedException: {}", e.getMessage());
return ResponseEntity
.status(HttpStatus.METHOD_NOT_ALLOWED)
.body(ApiResponse.error(ErrorCode.METHOD_NOT_ALLOWED.getMessage()));
}

@ExceptionHandler(MissingRequestHeaderException.class)
public ResponseEntity<ApiResponse<Void>> handleMissingRequestHeader(MissingRequestHeaderException e) {
log.warn("MissingRequestHeaderException: {}", e.getMessage());
return ResponseEntity
.status(HttpStatus.BAD_REQUEST)
.body(ApiResponse.error(ErrorCode.MISSING_REQUEST_HEADER.getMessage() + ": " + e.getHeaderName()));
}

@ExceptionHandler(MethodArgumentTypeMismatchException.class)
public ResponseEntity<ApiResponse<Void>> handleMethodArgumentTypeMismatch(MethodArgumentTypeMismatchException e) {
log.warn("MethodArgumentTypeMismatchException: {}", e.getMessage());
return ResponseEntity
.status(HttpStatus.BAD_REQUEST)
.body(ApiResponse.error(ErrorCode.INVALID_INPUT.getMessage()));
}

@ExceptionHandler(DataIntegrityViolationException.class)
public ResponseEntity<ApiResponse<Void>> handleDataIntegrityViolation(DataIntegrityViolationException e) {
log.warn("DataIntegrityViolationException: {}", e.getMessage());
return ResponseEntity
.status(HttpStatus.CONFLICT)
.body(ApiResponse.error(ErrorCode.CONFLICT.getMessage()));
}

@ExceptionHandler(AccessDeniedException.class)
public ResponseEntity<ApiResponse<Void>> handleAccessDenied(AccessDeniedException e) {
log.warn("AccessDeniedException: {}", e.getMessage());
return ResponseEntity
.status(HttpStatus.FORBIDDEN)
.body(ApiResponse.error(ErrorCode.FORBIDDEN.getMessage()));
}

@ExceptionHandler(NoResourceFoundException.class)
public ResponseEntity<ApiResponse<Void>> handleNoResourceFound(NoResourceFoundException e) {
log.warn("NoResourceFoundException: {}", e.getMessage());
return ResponseEntity
.status(HttpStatus.NOT_FOUND)
.body(ApiResponse.error(ErrorCode.NOT_FOUND.getMessage()));
}

@ExceptionHandler(Exception.class)
public ResponseEntity<ApiResponse<Void>> handleException(Exception e) {
log.error("Unexpected error", e);
Expand Down
Loading