Skip to content

Commit

Permalink
[feat] exception handler에 예외 추가 (#187)
Browse files Browse the repository at this point in the history
* [feat] add errorcode

* [feat] add exceptions in exception handler
  • Loading branch information
Parkjyun authored Aug 26, 2024
1 parent 51876c0 commit cbe0d3c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@
import org.hankki.hankkiserver.common.code.StoreErrorCode;
import org.hankki.hankkiserver.common.code.StoreImageErrorCode;
import org.hankki.hankkiserver.common.exception.*;
import org.springframework.http.converter.HttpMessageNotReadableException;
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 org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.servlet.resource.NoResourceFoundException;
import software.amazon.awssdk.core.exception.SdkClientException;

@RestControllerAdvice
Expand All @@ -19,62 +23,79 @@ public class GlobalExceptionHandler {

@ExceptionHandler(BadRequestException.class)
public HankkiResponse<Void> handleBadRequestException(BadRequestException e) {
log.error("handleBadRequestException() in GlobalExceptionHandler throw BadRequestException : {}", e.getMessage());
log.warn("handleBadRequestException() in GlobalExceptionHandler throw BadRequestException : {}", e.getMessage());
return HankkiResponse.fail(e.getErrorCode());
}

@ExceptionHandler(UnauthorizedException.class)
public HankkiResponse<Void> handleUnauthorizedException(UnauthorizedException e) {
log.error("handleUnauthorizedException() in GlobalExceptionHandler throw UnauthorizedException : {}", e.getMessage());
log.warn("handleUnauthorizedException() in GlobalExceptionHandler throw UnauthorizedException : {}", e.getMessage());
return HankkiResponse.fail(e.getErrorCode());
}

@ExceptionHandler(NotFoundException.class)
public HankkiResponse<Void> handleEntityNotFoundException(NotFoundException e) {
log.error("handleEntityNotFoundException() in GlobalExceptionHandler throw EntityNotFoundException : {}", e.getMessage());
log.warn("handleEntityNotFoundException() in GlobalExceptionHandler throw EntityNotFoundException : {}", e.getMessage());
return HankkiResponse.fail(e.getErrorCode());
}

@ExceptionHandler(ConflictException.class)
public HankkiResponse<Void> handleConflictException(ConflictException e) {
log.error("handleConflictException() in GlobalExceptionHandler throw ConflictException : {}", e.getMessage());
log.warn("handleConflictException() in GlobalExceptionHandler throw ConflictException : {}", e.getMessage());
return HankkiResponse.fail(e.getErrorCode());
}

@ExceptionHandler(MissingServletRequestParameterException.class)
public HankkiResponse<Void> handleMissingServletRequestParameterException(MissingServletRequestParameterException e) {
log.error("handleMissingServletRequestParameterException() in GlobalExceptionHandler throw MissingServletRequestParameterException : {}", e.getMessage());
log.warn("handleMissingServletRequestParameterException() in GlobalExceptionHandler throw MissingServletRequestParameterException : {}", e.getMessage());
return HankkiResponse.fail(BusinessErrorCode.BAD_REQUEST);
}

@ExceptionHandler(MaxUploadSizeExceededException.class)
public HankkiResponse<Void> handleMaxUploadSizeExceededException(MaxUploadSizeExceededException e) {
log.error("handleMaxUploadSizeExceededException() in GlobalExceptionHandler throw MaxUploadSizeExceededException : {}", e.getMessage());
log.warn("handleMaxUploadSizeExceededException() in GlobalExceptionHandler throw MaxUploadSizeExceededException : {}", e.getMessage());
return HankkiResponse.fail(StoreErrorCode.STORE_FILE_SIZE_EXCEEDED);
}

@ExceptionHandler(MethodArgumentNotValidException.class)
public HankkiResponse<Void> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
log.error("handleMethodArgumentNotValidException() in GlobalExceptionHandler throw MethodArgumentNotValidException : {}", e.getMessage());
log.warn("handleMethodArgumentNotValidException() in GlobalExceptionHandler throw MethodArgumentNotValidException : {}", e.getMessage());
return HankkiResponse.fail(BusinessErrorCode.BAD_REQUEST);
}

@ExceptionHandler(MethodArgumentTypeMismatchException.class)
public HankkiResponse<Void> handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException e) {
log.warn("handleMethodArgumentTypeMismatchException() in GlobalExceptionHandler throw MethodArgumentTypeMismatchException : {}", e.getMessage());
return HankkiResponse.fail(BusinessErrorCode.BAD_REQUEST);
}

@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
public HankkiResponse<Void> handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
log.warn("handleHttpRequestMethodNotSupportedException() in GlobalExceptionHandler throw HttpRequestMethodNotSupportedException : {}", e.getMessage());
return HankkiResponse.fail(BusinessErrorCode.BAD_REQUEST);
}

@ExceptionHandler(NoResourceFoundException.class)
public HankkiResponse<Void> handleNoResourceFoundException(NoResourceFoundException e) {
log.warn("handleNoResourceFoundException() in GlobalExceptionHandler throw NoResourceFoundException : {}", e.getMessage());
return HankkiResponse.fail(BusinessErrorCode.NOT_FOUND);
}

@ExceptionHandler(HttpMessageNotReadableException.class)
public HankkiResponse<Void> handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
log.warn("handleHttpMessageNotReadableException() in GlobalExceptionHandler throw HttpMessageNotReadableException : {}", e.getMessage());
return HankkiResponse.fail(BusinessErrorCode.BAD_REQUEST);
}

@ExceptionHandler(SdkClientException.class)
public HankkiResponse<Void> handleSdkClientException(SdkClientException e) {
log.error("handleSdkClientException() in GlobalExceptionHandler throw SdkClientException : {}", e.getMessage());
log.warn("handleSdkClientException() in GlobalExceptionHandler throw SdkClientException : {}", e.getMessage());
return HankkiResponse.fail(StoreImageErrorCode.STORE_IMAGE_UPLOAD_FAILED);
}

@ExceptionHandler(BadGatewayException.class)
public HankkiResponse<Void> handleBadGatewayException(BadGatewayException e) {
log.error("handleBadGatewayException() in GlobalExceptionHandler throw BadGatewayException : {}", e.getMessage());
return HankkiResponse.fail(e.getErrorCode());
}

@ExceptionHandler(Exception.class)
public HankkiResponse<Void> handleException(Exception e) {
log.error("handleException() in GlobalExceptionHandler throw Exception [{}] : {}", e.getClass() , e.getMessage());
log.error("[500] INTERNAL SERVER ERROR({}) : {}",e.getClass() , e.getMessage());
return HankkiResponse.fail(BusinessErrorCode.INTERNAL_SERVER_ERROR);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
public enum BusinessErrorCode implements ErrorCode {

BAD_REQUEST(HttpStatus.BAD_REQUEST, "잘못된 요청입니다."),
NOT_FOUND(HttpStatus.NOT_FOUND, "요청한 자원을 찾을 수 없습니다."),
INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "서버 내부 오류가 발생했습니다.");

private final HttpStatus httpStatus;
Expand Down

0 comments on commit cbe0d3c

Please sign in to comment.