Skip to content

Commit

Permalink
feat: ResponseEntityExceptionHandler에서 처리하는 예외를 직접 핸들링
Browse files Browse the repository at this point in the history
  • Loading branch information
nayonsoso committed Jul 23, 2024
1 parent 4cc7680 commit aafa347
Showing 1 changed file with 51 additions and 12 deletions.
63 changes: 51 additions & 12 deletions backend/src/main/java/reviewme/global/GlobalExceptionHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,23 @@

import java.util.List;
import java.util.Map;
import org.springframework.http.HttpHeaders;
import org.springframework.beans.TypeMismatchException;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ProblemDetail;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
import org.springframework.validation.method.MethodValidationException;
import org.springframework.web.HttpMediaTypeException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.method.annotation.HandlerMethodValidationException;
import org.springframework.web.multipart.support.MissingServletRequestPartException;
import org.springframework.web.server.MissingRequestValueException;
import org.springframework.web.servlet.NoHandlerFoundException;
import org.springframework.web.servlet.resource.NoResourceFoundException;
import reviewme.global.exception.BadRequestException;
import reviewme.global.exception.FieldErrorResponse;
import reviewme.global.exception.NotFoundException;
Expand All @@ -28,9 +36,45 @@ public ProblemDetail handleBadRequestException(BadRequestException ex) {
return ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST, ex.getErrorMessage());
}

@ExceptionHandler(Exception.class)
public ProblemDetail handleException(Exception ex) {
return ProblemDetail.forStatusAndDetail(HttpStatus.INTERNAL_SERVER_ERROR, "서버 에러가 발생했습니다.");
}

// default error
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
public ProblemDetail handleHttpRequestMethodNotSupportedException(RuntimeException ex) {
return ProblemDetail.forStatusAndDetail(HttpStatus.METHOD_NOT_ALLOWED, "지원하지 않는 HTTP 메서드입니다.");
}

@ExceptionHandler(HttpMediaTypeException.class)
public ProblemDetail handleHttpMediaTypeException(RuntimeException ex) {
return ProblemDetail.forStatusAndDetail(HttpStatus.UNSUPPORTED_MEDIA_TYPE, "잘못된 media type 입니다.");
}

@ExceptionHandler({MissingRequestValueException.class, MissingServletRequestPartException.class})
public ProblemDetail handleMissingRequestException(RuntimeException ex) {
return ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST, "필수 요청 데이터가 누락되었습니다.");
}

@ExceptionHandler({ServletRequestBindingException.class, HttpMessageNotReadableException.class})
public ProblemDetail handleServletRequestBindingException(RuntimeException ex) {
return ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST, "요청을 읽을 수 없습니다.");
}

@ExceptionHandler({MethodValidationException.class, BindException.class,
TypeMismatchException.class, HandlerMethodValidationException.class})
public ProblemDetail handleRequestFormatException(RuntimeException ex) {
return ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST, "요청의 형식이 잘못되었습니다.");
}

@ExceptionHandler({NoHandlerFoundException.class, NoResourceFoundException.class})
public ProblemDetail handleNoHandlerFoundException(RuntimeException ex) {
return ProblemDetail.forStatusAndDetail(HttpStatus.NOT_FOUND, "잘못된 경로의 요청입니다.");
}

@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<Object> handleMethodArgumentNotValid(
MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
public ProblemDetail handleMethodArgumentNotValid(MethodArgumentNotValidException ex) {
List<FieldErrorResponse> fieldErrors = ex.getBindingResult()
.getFieldErrors()
.stream()
Expand All @@ -45,11 +89,6 @@ public ResponseEntity<Object> handleMethodArgumentNotValid(
);
Map<String, Object> properties = Map.of("fieldErrors", fieldErrors);
problemDetail.setProperties(properties);
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(problemDetail);
}

@ExceptionHandler(Exception.class)
public ProblemDetail handleException(Exception ex) {
return ProblemDetail.forStatusAndDetail(HttpStatus.INTERNAL_SERVER_ERROR, "서버 에러가 발생했습니다.");
return problemDetail;
}
}

0 comments on commit aafa347

Please sign in to comment.