Skip to content

Commit

Permalink
refactor: 예외 응답 개선 (#335)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikekks authored Aug 25, 2024
1 parent e75f7c1 commit 708c3f5
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,35 +1,119 @@
package org.sopt.makers.crew.main.common.advice;

import org.sopt.makers.crew.main.common.exception.BaseException;
import org.sopt.makers.crew.main.common.exception.CommonResponseDto;
import org.sopt.makers.crew.main.common.exception.ExceptionResponse;
import org.sopt.makers.crew.main.common.exception.ErrorStatus;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingPathVariableException;
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 jakarta.validation.ConstraintViolationException;
import lombok.extern.slf4j.Slf4j;

@RestControllerAdvice
@Slf4j
public class ControllerExceptionAdvice {

@ExceptionHandler(BaseException.class)
public ResponseEntity<CommonResponseDto> handleGlobalException(BaseException ex) {
return ResponseEntity.status(ex.getStatusCode())
.body(CommonResponseDto.fail(ex.getErrorCode()));
}

@ExceptionHandler(MissingServletRequestParameterException.class)
public ResponseEntity<CommonResponseDto> handleMissingParameter(
MissingServletRequestParameterException ex) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(CommonResponseDto.fail(
ErrorStatus.VALIDATION_REQUEST_MISSING_EXCEPTION.getErrorCode()));
}

@ExceptionHandler(IllegalArgumentException.class)
public ResponseEntity<CommonResponseDto> handleIllegalArgument(IllegalArgumentException ex) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(CommonResponseDto.fail(ex.getMessage()));
}
@ExceptionHandler(BaseException.class)
public ResponseEntity<ExceptionResponse> handleGlobalException(BaseException e) {
log.warn("{}", e.getMessage());
return ResponseEntity.status(e.getStatusCode())
.body(ExceptionResponse.fail(e.getErrorCode()));
}

@ExceptionHandler(MissingServletRequestParameterException.class)
public ResponseEntity<ExceptionResponse> handleMissingParameter(
MissingServletRequestParameterException e) {
log.warn("{}", e.getMessage());
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(ExceptionResponse.fail(
ErrorStatus.VALIDATION_REQUEST_MISSING_EXCEPTION.getErrorCode()));
}

@ExceptionHandler(IllegalArgumentException.class)
public ResponseEntity<ExceptionResponse> handleIllegalArgument(IllegalArgumentException e) {
log.warn("{}", e.getMessage());
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(ExceptionResponse.fail(
ErrorStatus.INVALID_INPUT_VALUE.getErrorCode()));
}

@ExceptionHandler(ConstraintViolationException.class) // @Notnull 오류
public ResponseEntity<ExceptionResponse> handleConstraintViolationException(ConstraintViolationException e) {
log.warn("{}", e.getMessage());
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(ExceptionResponse.fail(
ErrorStatus.INVALID_INPUT_VALUE.getErrorCode()));
}

@ExceptionHandler(DataIntegrityViolationException.class) // null value 오류
public ResponseEntity<ExceptionResponse> handleDataIntegrityViolationException(DataIntegrityViolationException e) {
log.warn("{}", e.getMessage());
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(ExceptionResponse.fail(
ErrorStatus.INVALID_INPUT_VALUE.getErrorCode()));
}

@ExceptionHandler(HttpMessageNotReadableException.class) // null value 오류
public ResponseEntity<ExceptionResponse> handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
log.warn("{}", e.getMessage());
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(ExceptionResponse.fail(
ErrorStatus.INVALID_INPUT_VALUE.getErrorCode()));
}

@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<ExceptionResponse> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
log.warn("{}", e.getMessage());
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(ExceptionResponse.fail(
ErrorStatus.INVALID_INPUT_VALUE.getErrorCode()));
}

/**
* path variable errors
*/
@ExceptionHandler(MethodArgumentTypeMismatchException.class)
public ResponseEntity<ExceptionResponse> handleMethodArgumentTypeMismatchException(
MethodArgumentTypeMismatchException e) {
log.warn("{}", e.getMessage());
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(ExceptionResponse.fail(
ErrorStatus.INVALID_INPUT_VALUE.getErrorCode()));
}

@ExceptionHandler(MissingPathVariableException.class)
public ResponseEntity<ExceptionResponse> handleMissingPathVariableException(
MissingPathVariableException e) {
log.warn("{}", e.getMessage());
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(ExceptionResponse.fail(
ErrorStatus.INVALID_INPUT_VALUE.getErrorCode()));
}

@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
public ResponseEntity<ExceptionResponse> handleHttpRequestMethodNotSupportedException(
HttpRequestMethodNotSupportedException e) {
log.warn("{}", e.getMessage());
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(ExceptionResponse.fail(
ErrorStatus.INVALID_INPUT_VALUE.getErrorCode()));
}

@ExceptionHandler(Exception.class)
public ResponseEntity<ExceptionResponse> handleException(Exception e) {
log.error("{}", e.getMessage());
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(ExceptionResponse.fail(
ErrorStatus.INTERNAL_SERVER_ERROR.getErrorCode()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public enum ErrorStatus {
*/
VALIDATION_EXCEPTION("CF-001"),
VALIDATION_REQUEST_MISSING_EXCEPTION("요청값이 입력되지 않았습니다."),
INVALID_INPUT_VALUE("요청값이 올바르지 않습니다."),
INVALID_INPUT_VALUE_FILTER("요청값 또는 토큰이 올바르지 않습니다."),
NOT_FOUND_MEETING("모임이 없습니다."),
NOT_FOUND_POST("존재하지 않는 게시글입니다."),
NOT_FOUND_COMMENT("존재하지 않는 댓글입니다."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
@Builder
@Getter
@JsonInclude(JsonInclude.Include.NON_NULL)
public class CommonResponseDto<T> {
public class ExceptionResponse {

private final String errorCode;

public static CommonResponseDto fail(String errorCode) {
return CommonResponseDto.builder()
public static ExceptionResponse fail(String errorCode) {
return ExceptionResponse.builder()
.errorCode(errorCode)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package org.sopt.makers.crew.main.common.jwt;

import static org.sopt.makers.crew.main.common.exception.ErrorStatus.*;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

import org.sopt.makers.crew.main.common.exception.ExceptionResponse;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.databind.ObjectMapper;

@Component
public class JwtAuthenticationEntryPoint implements AuthenticationEntryPoint {

Expand All @@ -21,6 +26,11 @@ public void commence(HttpServletRequest request, HttpServletResponse response,
public void setResponse(HttpServletResponse response) throws IOException {
response.setContentType("application/json;charset=UTF-8");
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);

ObjectMapper mapper = new ObjectMapper();
String jsonResponse = mapper.writeValueAsString(ExceptionResponse.fail(INVALID_INPUT_VALUE_FILTER.getErrorCode()));

response.getWriter().write(jsonResponse);
}

}

0 comments on commit 708c3f5

Please sign in to comment.