Skip to content

Commit

Permalink
Feature | #4 | @lcomment | 에러 핸들러 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
lcomment committed May 3, 2024
1 parent 9d4edc5 commit 4fad91b
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.cakk.api.controller.advice;

import static org.springframework.http.HttpStatus.*;
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;

import java.sql.SQLException;
import java.util.List;

import jakarta.servlet.http.HttpServletRequest;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;

import lombok.RequiredArgsConstructor;

import com.cakk.api.service.slack.SlackService;
import com.cakk.common.enums.ReturnCode;
import com.cakk.common.exception.CakkException;
import com.cakk.common.response.ApiResponse;

@RestControllerAdvice
@RequiredArgsConstructor
public class GlobalControllerAdvice {

private final SlackService slackService;

@ExceptionHandler(CakkException.class)
public ResponseEntity<ApiResponse<Void>> handleCakkException(CakkException exception, HttpServletRequest request) {
if (exception.getReturnCode().equals(ReturnCode.EXTERNAL_SERVER_ERROR)) {
slackService.sendSlackForError(exception, request);
}

return getResponseEntity(BAD_REQUEST, ApiResponse.fail(exception.getReturnCode()));
}

@ExceptionHandler(value = {
HttpMessageNotReadableException.class,
MissingServletRequestParameterException.class,
MethodArgumentTypeMismatchException.class
})
public ResponseEntity<ApiResponse<Void>> handleRequestException(Exception exception) {
return getResponseEntity(BAD_REQUEST, ApiResponse.fail(ReturnCode.WRONG_PARAMETER));
}

@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
public ResponseEntity<ApiResponse<Void>> handleMethodNotSupportedException(HttpRequestMethodNotSupportedException exception) {
return getResponseEntity(BAD_REQUEST, ApiResponse.fail(ReturnCode.METHOD_NOT_ALLOWED));
}

@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<ApiResponse<List<FieldError>>> badRequestExHandler(BindingResult bindingResult) {
return getResponseEntity(BAD_REQUEST, ApiResponse.fail(ReturnCode.WRONG_PARAMETER, bindingResult.getFieldErrors()));
}

@ExceptionHandler(value = {
SQLException.class,
RuntimeException.class
})
public ResponseEntity<ApiResponse<String>> handleServerException(SQLException exception, HttpServletRequest request) {
slackService.sendSlackForError(exception, request);
return getResponseEntity(INTERNAL_SERVER_ERROR, ApiResponse.error(ReturnCode.INTERNAL_SERVER_ERROR, exception.getMessage()));
}

private <T> ResponseEntity<ApiResponse<T>> getResponseEntity(HttpStatus status, ApiResponse<T> response) {
return ResponseEntity.status(status).body(response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ public enum ReturnCode {
EMPTY_AUTH_JWT("1103", "인증 정보가 비어있는 jwt 토큰입니다."),
EMPTY_USER("1104", "비어있는 유저 정보로 jwt 토큰을 생성할 수 없습니다."),

// 클라이언트 에러
WRONG_PARAMETER("9000", "잘못된 파라미터 입니다."),
METHOD_NOT_ALLOWED("9001", "허용되지 않은 메소드 입니다."),

// 서버 에러 (9998, 9999)
INTERNAL_SERVER_ERROR("9998", "내부 서버 에러 입니다."),
EXTERNAL_SERVER_ERROR("9999", "외부 서버 에러 입니다.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
@Getter
public class CakkException extends RuntimeException {

private final ReturnCode returnCode;
private final String code;
private final String message;

public CakkException(ReturnCode returnCode) {
this.returnCode = returnCode;
this.code = returnCode.getCode();
this.message = returnCode.getMessage();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ public static <T> ApiResponse<T> success(T data) {
return response;
}

public static <T> ApiResponse<T> fail(ReturnCode returnCode) {
ApiResponse<T> response = new ApiResponse<>();

response.returnCode = returnCode.getCode();
response.returnMessage = returnCode.getMessage();
response.data = null;

return response;
}

public static <T> ApiResponse<T> fail(ReturnCode returnCode, T data) {
ApiResponse<T> response = new ApiResponse<>();

Expand Down

0 comments on commit 4fad91b

Please sign in to comment.