All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.feingto.cloud.config.web.DefaultHandlerExceptionResolver Maven / Gradle / Ivy

The newest version!
package com.feingto.cloud.config.web;

import com.feingto.cloud.kit.CastUtils;
import com.feingto.cloud.kit.ClassKit;
import org.springframework.beans.ConversionNotSupportedException;
import org.springframework.beans.TypeMismatchException;
import org.springframework.core.Ordered;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import org.springframework.transaction.TransactionException;
import org.springframework.validation.BindException;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingPathVariableException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.context.request.async.AsyncRequestTimeoutException;
import org.springframework.web.multipart.MultipartException;
import org.springframework.web.multipart.support.MissingServletRequestPartException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.NoHandlerFoundException;
import org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolationException;
import java.nio.file.AccessDeniedException;

import static com.feingto.cloud.config.web.ExceptionResolverUtil.*;

/**
 * The default implementation of the {@link org.springframework.web.servlet.HandlerExceptionResolver}
 *
 * @author longfei
 */
public class DefaultHandlerExceptionResolver extends AbstractHandlerExceptionResolver {
    public DefaultHandlerExceptionResolver() {
        setOrder(Ordered.LOWEST_PRECEDENCE);
        setWarnLogCategory(getClass().getName());
    }

    @Nullable
    @Override
    public ModelAndView doResolveException(@NonNull HttpServletRequest request,
                                           @NonNull HttpServletResponse response,
                                           @Nullable Object handler,
                                           @NonNull Exception ex) {
        Integer status = null;
        String error = null;
        boolean hasSecurityClass = ClassKit.isExist(
                "org.springframework.security.access.AccessDeniedException");

        if (ex instanceof HttpRequestMethodNotSupportedException) {
            status = HttpServletResponse.SC_METHOD_NOT_ALLOWED;
            error = resolverException(ex, "不支持'" + request.getMethod() + "'请求方法");
        } else if (ex instanceof HttpMediaTypeNotSupportedException) {
            status = HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE;
            error = resolverException(ex, "不支持的MIME类型");
        } else if (ex instanceof HttpMediaTypeNotAcceptableException) {
            status = HttpServletResponse.SC_NOT_ACCEPTABLE;
            error = resolverException(ex, "找不到可以匹配(接受)的MIME类型");
        } else if (ex instanceof MissingPathVariableException) {
            status = HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
            error = resolverException(ex, "URI缺少PATH类型方法参数");
        } else if (ex instanceof MissingServletRequestParameterException) {
            status = HttpServletResponse.SC_BAD_REQUEST;
            error = resolverException(ex, "缺少请求参数");
        } else if (ex instanceof ServletRequestBindingException) {
            status = HttpServletResponse.SC_BAD_REQUEST;
            error = resolverException(ex, "绑定异常");
        } else if (ex instanceof ConversionNotSupportedException) {
            status = HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
            error = resolverException(ex, "Bean属性未匹配合适的转换器");
        } else if (ex instanceof TypeMismatchException) {
            status = HttpServletResponse.SC_BAD_REQUEST;
            error = resolverException(ex, "类型不匹配异常");
        } else if (ex instanceof HttpMessageNotReadableException) {
            status = HttpServletResponse.SC_BAD_REQUEST;
            error = resolverException(ex, "消息不可读异常");
        } else if (ex instanceof HttpMessageNotWritableException) {
            status = HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
            error = resolverException(ex, "消息不可写异常");
        } else if (ex instanceof MethodArgumentNotValidException) {
            status = HttpServletResponse.SC_BAD_REQUEST;
            error = resolverBindException(((MethodArgumentNotValidException) ex).getBindingResult());
        } else if (ex instanceof MissingServletRequestPartException) {
            status = HttpServletResponse.SC_BAD_REQUEST;
            error = resolverException(ex, "缺少多部分请求头标识");
        } else if (ex instanceof MultipartException) {
            status = HttpServletResponse.SC_BAD_REQUEST;
            error = resolverException(ex, "多部分请求异常");
        } else if (ex instanceof BindException) {
            status = HttpServletResponse.SC_BAD_REQUEST;
            error = resolverBindException(((BindException) ex).getBindingResult());
        } else if (ex instanceof NoHandlerFoundException) {
            status = HttpServletResponse.SC_NOT_FOUND;
            error = resolverException(ex, "找不到请求的资源");
        } else if (ex instanceof AsyncRequestTimeoutException) {
            status = HttpServletResponse.SC_SERVICE_UNAVAILABLE;
            error = resolverException(ex, "异步请求超时");
        } else if (hasSecurityClass && ex instanceof AccessDeniedException) {
            status = HttpServletResponse.SC_UNAUTHORIZED;
            error = resolverException(ex, "未授权");
        } else if (ex instanceof ConstraintViolationException) {
            status = HttpServletResponse.SC_BAD_REQUEST;
            error = String.join(", ", BeanValidators.extractPropertyAndMessageAsList(
                    CastUtils.cast(ex).getConstraintViolations()));
        } else if (ex instanceof DataAccessException) {
            status = HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
            error = resolverException(ex, "数据访问异常");
            if (ex instanceof DataIntegrityViolationException) {
                error = resolverException(ex, "违反完整性约束");
                if (ex instanceof DuplicateKeyException) {
                    error = resolverException(ex, "违反主键唯一约束");
                }
            }
        } else if (ex instanceof HttpClientErrorException) {
            status = CastUtils.cast(ex).getRawStatusCode();
            error = status == HttpServletResponse.SC_UNAUTHORIZED ? "未授权" : trace(ex);
        } else if (ex instanceof IllegalStateException) {
            status = HttpServletResponse.SC_BAD_REQUEST;
            error = resolverException(ex, "非法状态");
        } else if (ex instanceof IllegalArgumentException) {
            status = HttpServletResponse.SC_BAD_REQUEST;
            error = resolverException(ex, "非法参数");
        } else if (ex instanceof TransactionException) {
            status = HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
            error = resolverException(ex, "提交事务异常");
        }

        return status != null && error != null ? handleException(request, status, error) : null;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy