generated from pricelees/issue-pr-template
[#5]: 공통 기능 코틀린 마이그레이션 및 패키지 분리 #6
@ -1,71 +1,75 @@
|
||||
package roomescape.common.exception;
|
||||
package roomescape.common.exception
|
||||
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.context.support.DefaultMessageSourceResolvable;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.converter.HttpMessageNotReadableException;
|
||||
import org.springframework.web.HttpRequestMethodNotSupportedException;
|
||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
import org.springframework.web.client.ResourceAccessException;
|
||||
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import roomescape.common.dto.response.ErrorResponse;
|
||||
import io.github.oshai.kotlinlogging.KLogger
|
||||
import io.github.oshai.kotlinlogging.KotlinLogging
|
||||
import jakarta.servlet.http.HttpServletResponse
|
||||
import org.springframework.http.HttpStatus
|
||||
import org.springframework.http.converter.HttpMessageNotReadableException
|
||||
import org.springframework.web.HttpRequestMethodNotSupportedException
|
||||
import org.springframework.web.bind.MethodArgumentNotValidException
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler
|
||||
import org.springframework.web.bind.annotation.ResponseStatus
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice
|
||||
import org.springframework.web.client.ResourceAccessException
|
||||
import roomescape.common.dto.response.ErrorResponse
|
||||
|
||||
@RestControllerAdvice
|
||||
public class ExceptionControllerAdvice {
|
||||
class ExceptionControllerAdvice(
|
||||
private val logger: KLogger = KotlinLogging.logger {}
|
||||
) {
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(getClass());
|
||||
@ExceptionHandler(value = [RoomescapeException::class])
|
||||
fun handleRoomEscapeException(
|
||||
e: RoomescapeException,
|
||||
response: HttpServletResponse
|
||||
): ErrorResponse {
|
||||
logger.error(e) { "message: ${e.message}, invalidValue: ${e.invalidValue}" }
|
||||
response.status = e.httpStatus.value()
|
||||
|
||||
@ExceptionHandler(value = {RoomescapeException.class})
|
||||
public ErrorResponse handleRoomEscapeException(RoomescapeException e, HttpServletResponse response) {
|
||||
logger.error("{}{}", e.getMessage(), e.getInvalidValue(), e);
|
||||
response.setStatus(e.getHttpStatus().value());
|
||||
return ErrorResponse.of(e.getErrorType(), e.getMessage());
|
||||
}
|
||||
return ErrorResponse.of(e.errorType, e.message!!)
|
||||
}
|
||||
|
||||
@ExceptionHandler(ResourceAccessException.class)
|
||||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
public ErrorResponse handleResourceAccessException(ResourceAccessException e) {
|
||||
logger.error(e.getMessage(), e);
|
||||
return ErrorResponse.of(ErrorType.PAYMENT_SERVER_ERROR, ErrorType.PAYMENT_SERVER_ERROR.description);
|
||||
}
|
||||
@ExceptionHandler(ResourceAccessException::class)
|
||||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
fun handleResourceAccessException(e: ResourceAccessException): ErrorResponse {
|
||||
logger.error(e) { "message: ${e.message}" }
|
||||
|
||||
@ExceptionHandler(value = HttpMessageNotReadableException.class)
|
||||
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||
public ErrorResponse handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
|
||||
logger.error(e.getMessage(), e);
|
||||
return ErrorResponse.of(ErrorType.INVALID_REQUEST_DATA_TYPE,
|
||||
ErrorType.INVALID_REQUEST_DATA_TYPE.description);
|
||||
}
|
||||
return ErrorResponse.of(ErrorType.PAYMENT_SERVER_ERROR, ErrorType.PAYMENT_SERVER_ERROR.description)
|
||||
}
|
||||
|
||||
@ExceptionHandler(value = MethodArgumentNotValidException.class)
|
||||
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||
public ErrorResponse handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
|
||||
String messages = e.getBindingResult().getAllErrors().stream()
|
||||
.map(DefaultMessageSourceResolvable::getDefaultMessage)
|
||||
.collect(Collectors.joining(", "));
|
||||
@ExceptionHandler(value = [HttpMessageNotReadableException::class])
|
||||
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||
fun handleHttpMessageNotReadableException(e: HttpMessageNotReadableException): ErrorResponse {
|
||||
logger.error(e) { "message: ${e.message}" }
|
||||
|
||||
logger.error(messages, e);
|
||||
return ErrorResponse.of(ErrorType.INVALID_REQUEST_DATA, messages);
|
||||
}
|
||||
return ErrorResponse.of(ErrorType.INVALID_REQUEST_DATA_TYPE,
|
||||
ErrorType.INVALID_REQUEST_DATA_TYPE.description)
|
||||
}
|
||||
|
||||
@ExceptionHandler(value = HttpRequestMethodNotSupportedException.class)
|
||||
@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
|
||||
public ErrorResponse handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
|
||||
logger.error(e.getMessage(), e);
|
||||
return ErrorResponse.of(ErrorType.METHOD_NOT_ALLOWED, ErrorType.METHOD_NOT_ALLOWED.description);
|
||||
}
|
||||
@ExceptionHandler(value = [MethodArgumentNotValidException::class])
|
||||
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||
fun handleMethodArgumentNotValidException(e: MethodArgumentNotValidException): ErrorResponse {
|
||||
val messages: String = e.bindingResult.allErrors
|
||||
.mapNotNull { it.defaultMessage }
|
||||
.joinToString(", ")
|
||||
logger.error(e) { "message: $messages" }
|
||||
|
||||
@ExceptionHandler(value = Exception.class)
|
||||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
public ErrorResponse handleException(Exception e) {
|
||||
logger.error(e.getMessage(), e);
|
||||
return ErrorResponse.of(ErrorType.INTERNAL_SERVER_ERROR, ErrorType.INTERNAL_SERVER_ERROR.description);
|
||||
}
|
||||
return ErrorResponse.of(ErrorType.INVALID_REQUEST_DATA, messages)
|
||||
}
|
||||
|
||||
@ExceptionHandler(value = [HttpRequestMethodNotSupportedException::class])
|
||||
@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
|
||||
fun handleHttpRequestMethodNotSupportedException(e: HttpRequestMethodNotSupportedException): ErrorResponse {
|
||||
logger.error(e) { "message: ${e.message}" }
|
||||
|
||||
return ErrorResponse.of(ErrorType.METHOD_NOT_ALLOWED, ErrorType.METHOD_NOT_ALLOWED.description)
|
||||
}
|
||||
|
||||
@ExceptionHandler(value = [Exception::class])
|
||||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
fun handleException(e: Exception): ErrorResponse {
|
||||
logger.error(e) { "message: ${e.message}" }
|
||||
|
||||
return ErrorResponse.of(ErrorType.UNEXPECTED_ERROR, ErrorType.UNEXPECTED_ERROR.description)
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user