From 25a1e6192e7c375a172379494072f3075c1f0d57 Mon Sep 17 00:00:00 2001 From: sohyundoh Date: Thu, 12 Oct 2023 11:32:48 +0900 Subject: [PATCH] =?UTF-8?q?#150=20[feat]=20=EC=98=88=EC=99=B8=20=EB=A6=AC?= =?UTF-8?q?=ED=84=B4=20=EA=B0=92=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../advice/ControllerExceptionAdvice.java | 97 ++++++++++--------- 1 file changed, 53 insertions(+), 44 deletions(-) diff --git a/src/main/java/com/asap/server/common/advice/ControllerExceptionAdvice.java b/src/main/java/com/asap/server/common/advice/ControllerExceptionAdvice.java index 48e38a86..21e4bbec 100644 --- a/src/main/java/com/asap/server/common/advice/ControllerExceptionAdvice.java +++ b/src/main/java/com/asap/server/common/advice/ControllerExceptionAdvice.java @@ -6,7 +6,12 @@ import com.asap.server.controller.dto.response.HostLoginResponseDto; import com.asap.server.exception.Error; import com.asap.server.exception.model.AsapException; +import com.asap.server.exception.model.BadRequestException; +import com.asap.server.exception.model.ConflictException; +import com.asap.server.exception.model.ForbiddenException; import com.asap.server.exception.model.HostTimeForbiddenException; +import com.asap.server.exception.model.NotFoundException; +import com.asap.server.exception.model.UnauthorizedException; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.http.HttpStatus; @@ -50,12 +55,6 @@ protected ErrorResponse handleMethodArgumentNotValidException(final MethodArgume else return ErrorResponse.error(Error.VALIDATION_REQUEST_MISSING_EXCEPTION, fieldError.getDefaultMessage()); } - @ResponseStatus(HttpStatus.NOT_FOUND) - @ExceptionHandler(NoHandlerFoundException.class) - public ErrorResponse handleNotFoundException(NoHandlerFoundException exception) { - return ErrorResponse.error(Error.URI_NOT_FOUND_EXCEPTION); - } - @ResponseStatus(HttpStatus.BAD_REQUEST) @ExceptionHandler(HttpMessageNotReadableException.class) protected ErrorResponse handleJsonParseException(final HttpMessageNotReadableException e) { @@ -68,61 +67,71 @@ protected ErrorResponse handleValidationException(final ConstraintViolationExcep return ErrorResponse.error(Error.VALIDATION_REQUEST_MISSING_EXCEPTION); } + + @ResponseStatus(HttpStatus.BAD_REQUEST) + @ExceptionHandler(BadRequestException.class) + protected ErrorResponse handleBadRequestException(final BadRequestException e) { + return ErrorResponse.error(e.getError()); + } + /** - * 405 Method Not Allowed - * 지원하지 않은 HTTP method 호출 할 경우 발생하는 Exception + * 401 UnAuthorization */ - @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED) - @ExceptionHandler(HttpRequestMethodNotSupportedException.class) - protected ErrorResponse handleHttpRequestMethodNotSupportedException( - HttpRequestMethodNotSupportedException exception) { - return ErrorResponse.error(METHOD_NOT_ALLOWED_EXCEPTION); + @ResponseStatus(HttpStatus.UNAUTHORIZED) + @ExceptionHandler(UnauthorizedException.class) + protected ErrorResponse handleUnAuthorizedException(final UnauthorizedException e) { + return ErrorResponse.error(e.getError()); } -// /** -// * 401 UnAuthorization -// */ -// @ResponseStatus(HttpStatus.UNAUTHORIZED) -// @ExceptionHandler(UnauthorizedException.class) -// protected ErrorResponse handleUnAuthorizedException(final UnauthorizedException e) { -// return ErrorResponse.error(e.getError()); -// } -// -// /** -// * 403 Forbidden -// */ -// @ResponseStatus(HttpStatus.FORBIDDEN) -// @ExceptionHandler(ForbiddenException.class) -// protected ErrorResponse handleForbiddenException(final ForbiddenException e) { -// return ErrorResponse.error(e.getError()); -// } - /** * 403 Forbidden */ + @ResponseStatus(HttpStatus.FORBIDDEN) + @ExceptionHandler(ForbiddenException.class) + protected ErrorResponse handleForbiddenException(final ForbiddenException e) { + return ErrorResponse.error(e.getError()); + } + @ResponseStatus(HttpStatus.FORBIDDEN) @ExceptionHandler(HostTimeForbiddenException.class) protected ErrorDataResponse handleForbiddenException(final HostTimeForbiddenException e) { return ErrorDataResponse.error(e.getError(), e.getMessage(), e.getData()); } -// /** -// * 404 Not Found -// */ -// @ResponseStatus(HttpStatus.NOT_FOUND) -// @ExceptionHandler(NotFoundException.class) -// protected ErrorResponse handleNotFoundException(final NotFoundException e) { -// return ErrorResponse.error(e.getError()); -// } + /** + * 404 Not Found + */ + @ResponseStatus(HttpStatus.NOT_FOUND) + @ExceptionHandler(NotFoundException.class) + protected ErrorResponse handleNotFoundException(final NotFoundException e) { + return ErrorResponse.error(e.getError()); + } + + @ResponseStatus(HttpStatus.NOT_FOUND) + @ExceptionHandler(NoHandlerFoundException.class) + public ErrorResponse handleNotFoundException(NoHandlerFoundException exception) { + return ErrorResponse.error(Error.URI_NOT_FOUND_EXCEPTION); + } + + /** + * 405 Method Not Allowed + * 지원하지 않은 HTTP method 호출 할 경우 발생하는 Exception + */ + @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED) + @ExceptionHandler(HttpRequestMethodNotSupportedException.class) + protected ErrorResponse handleHttpRequestMethodNotSupportedException( + HttpRequestMethodNotSupportedException exception) { + return ErrorResponse.error(METHOD_NOT_ALLOWED_EXCEPTION); + } /** * 409 Conflict */ -// @ResponseStatus(HttpStatus.CONFLICT) -// @ExceptionHandler(ConflictException.class) -// protected ErrorResponse handleConflictException(final ConflictException e) { -// return ErrorResponse.error(e.getError()); -// } + @ResponseStatus(HttpStatus.CONFLICT) + @ExceptionHandler(ConflictException.class) + protected ErrorResponse handleConflictException(final ConflictException e) { + return ErrorResponse.error(e.getError()); + } /** * 500 Internal Server