Skip to content

Commit

Permalink
refactor: ResponseEntityExceptionHandler를 상속하지 않도록 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
nayonsoso committed Jul 23, 2024
1 parent cb12488 commit 4cc7680
Showing 1 changed file with 5 additions and 14 deletions.
19 changes: 5 additions & 14 deletions backend/src/main/java/reviewme/global/GlobalExceptionHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,16 @@
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ProblemDetail;
import org.springframework.http.ResponseEntity;
import org.springframework.lang.Nullable;
import org.springframework.web.bind.MethodArgumentNotValidException;
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.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
import reviewme.global.exception.BadRequestException;
import reviewme.global.exception.FieldErrorResponse;
import reviewme.global.exception.NotFoundException;

@RestControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
public class GlobalExceptionHandler {

@ExceptionHandler(NotFoundException.class)
public ProblemDetail handleNotFoundException(NotFoundException ex) {
Expand All @@ -30,12 +28,7 @@ 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, "서버 에러가 발생했습니다.");
}

@Override
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<Object> handleMethodArgumentNotValid(
MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
List<FieldErrorResponse> fieldErrors = ex.getBindingResult()
Expand All @@ -55,10 +48,8 @@ public ResponseEntity<Object> handleMethodArgumentNotValid(
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(problemDetail);
}

@Override
public ResponseEntity<Object> handleExceptionInternal(
Exception ex, @Nullable Object body, HttpHeaders headers, HttpStatusCode statusCode, WebRequest request) {
return ResponseEntity.status(statusCode)
.body(ProblemDetail.forStatusAndDetail(statusCode, ex.getMessage()));
@ExceptionHandler(Exception.class)
public ProblemDetail handleException(Exception ex) {
return ProblemDetail.forStatusAndDetail(HttpStatus.INTERNAL_SERVER_ERROR, "서버 에러가 발생했습니다.");
}
}

0 comments on commit 4cc7680

Please sign in to comment.