Skip to content

Commit

Permalink
feat: 전역 예외 처리 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
sa46lll committed Dec 23, 2023
1 parent 9241b8d commit 205352a
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.flow.sa46lll.fileshield.adapter.in.web;

import com.flow.sa46lll.fileshield.dto.ApiResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class ApiControllerAdvice {

private static final Logger log = LoggerFactory.getLogger(ApiControllerAdvice.class);

@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
protected <T> ApiResponse<T> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
log.warn("handleMethodArgumentNotValidException: ", e);
return ApiResponse.failure(
e.getBindingResult().getAllErrors().get(0).getDefaultMessage()
);
}

@ExceptionHandler(HttpMessageNotReadableException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public <T> ApiResponse<T> handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
log.info("handleHttpMessageNotReadableException: ", e);
return ApiResponse.failure(e.getMessage());
}

@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
public <T> ApiResponse<T> handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
log.info("handleHttpRequestMethodNotSupportedException: ", e);
return ApiResponse.failure(e.getBody().getTitle());
}

@ExceptionHandler(Exception.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public <T> ApiResponse<T> handleException(Exception e) {
log.error("handleException: ", e);
return ApiResponse.failure();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.flow.sa46lll.fileshield.dto;

public record ApiResponse<T>(String code, String message, T data) {

private static final String SUCCESS = "요청에 성공했습니다.";
private static final String FAIL = "요청에 실패했습니다.";

public static <T> ApiResponse<T> success() {
return new ApiResponse<>(SUCCESS, null, null);
}

public static <T> ApiResponse<T> success(T data) {
return new ApiResponse<>(SUCCESS, null, data);
}

public static <T> ApiResponse<T> failure() {
return new ApiResponse<>(FAIL, null, null);
}

public static <T> ApiResponse<T> failure(String message) {
return new ApiResponse<>(FAIL, message, null);
}
}

0 comments on commit 205352a

Please sign in to comment.