win.doyto.query.web.component.CommonExceptionHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of doyto-query-web Show documentation
Show all versions of doyto-query-web Show documentation
The web layer integrate with Spring Web
// Generated by delombok at Sat Apr 04 14:54:36 CST 2020
package win.doyto.query.web.component;
import com.fasterxml.jackson.databind.exc.InvalidFormatException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import win.doyto.query.web.response.ErrorCode;
import win.doyto.query.web.response.ErrorCodeException;
import win.doyto.query.web.response.PresetErrorCode;
import java.util.List;
/**
* CommonExceptionHandler
*
* @author f0rb on 2017-03-21.
*/
@ControllerAdvice
@ResponseBody
class CommonExceptionHandler {
@java.lang.SuppressWarnings("all")
@lombok.Generated
private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(CommonExceptionHandler.class);
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
public ErrorCode httpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
log.error("HttpRequestMethodNotSupportedException: " + e.getMessage(), e.getCause());
return ErrorCode.build(PresetErrorCode.HTTP_METHOD_NOT_SUPPORTED.args(e.getMethod()));
}
/**
* 参数类型转换错误
*
* @param e 异常
* @return result
*/
@ExceptionHandler(MethodArgumentTypeMismatchException.class)
public ErrorCode methodArgumentTypeMismatchException(MethodArgumentTypeMismatchException e) {
log.error("MethodArgumentTypeMismatchException: " + e.getMessage(), e);
return ErrorCode.build(PresetErrorCode.ARGUMENT_TYPE_MISMATCH);
}
@ExceptionHandler(HttpMessageNotReadableException.class)
public ErrorCode httpMessageNotReadableException(HttpMessageNotReadableException e) {
log.error("HttpMessageNotReadableException: " + e.getMessage(), e);
if (e.getCause() instanceof InvalidFormatException) {
return ErrorCode.build(PresetErrorCode.ARGUMENT_FORMAT_ERROR.args(((InvalidFormatException) e.getCause()).getValue()));
}
return ErrorCode.build(PresetErrorCode.REQUEST_BODY_ERROR);
}
@ExceptionHandler(MaxUploadSizeExceededException.class)
public ErrorCode uploadFileOverMaxSizeException(MaxUploadSizeExceededException e) {
log.error("MaxUploadSizeExceededException: {}", e.getMessage());
return ErrorCode.build(PresetErrorCode.FILE_UPLOAD_OVER_MAX_SIZE);
}
@ExceptionHandler(MethodArgumentNotValidException.class)
public ErrorCode httpMethodArgumentNotValidException(MethodArgumentNotValidException e) {
log.error("MethodArgumentNotValidException: " + e.getMessage(), e);
return buildByBindingResult(e.getBindingResult().getFieldErrors());
}
@ExceptionHandler(BindException.class)
public ErrorCode httpBindException(BindException e) {
return buildByBindingResult(e.getBindingResult().getFieldErrors());
}
private ErrorCode buildByBindingResult(List fieldErrors) {
ErrorCode errorCode = ErrorCode.build(PresetErrorCode.ARGUMENT_VALIDATION_FAILED);
fieldErrors.forEach(fieldError -> errorCode.addError(fieldError.getField(), fieldError.getDefaultMessage()));
return errorCode;
}
@ExceptionHandler(DuplicateKeyException.class)
public ErrorCode duplicateKeyException(DuplicateKeyException e) {
log.error("DuplicateKeyException: " + e.getMessage(), e);
return ErrorCode.build(PresetErrorCode.DUPLICATE_KEY_EXCEPTION);
}
@ExceptionHandler(Exception.class)
public ErrorCode exception(Exception e) {
log.error("Unknown Exception", e);
return ErrorCode.build(PresetErrorCode.ERROR);
}
@ExceptionHandler(ErrorCodeException.class)
public ErrorCode handleErrorCodeException(ErrorCodeException e) {
log.warn("ErrorCodeException: {}", e.getMessage());
return ErrorCode.build(e.getErrorCode());
}
}