Search in sources :

Example 1 with ValidationException

use of com.shaoqunliu.validation.exception.ValidationException in project CILManagement-Server by LiuinStein.

the class GlobalExceptionResolver method resolveException.

/**
 * Resolve the exception form controller
 *
 * @param request   http request
 * @param response  http response
 * @param o         the executed handler, or null if none chosen at the time of the exception (for example, if multipart resolution failed)
 * @param exception the exception that threw from controller
 * @return a new ModelAndView
 * @implNote https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/servlet/HandlerExceptionResolver.html
 */
@NotNull
@Override
public ModelAndView resolveException(@NotNull HttpServletRequest request, @NotNull HttpServletResponse response, @Nullable Object o, @NotNull Exception exception) {
    RestfulResult result = new RestfulResult(1, exception.getMessage(), new HashMap<>());
    response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
    if (exception instanceof SimpleException) {
        result.setCode(((SimpleException) exception).getCode());
    }
    if (exception instanceof SimpleHttpException) {
        response.setStatus(((SimpleHttpException) exception).getHttpStatusToReturn().value());
    }
    if (exception instanceof HttpRequestMethodNotSupportedException) {
        result.setCode(405);
        response.setStatus(HttpStatus.METHOD_NOT_ALLOWED.value());
    }
    if (exception instanceof ValidationException || exception instanceof ServletRequestBindingException || exception instanceof IllegalArgumentException) {
        response.setStatus(HttpStatus.BAD_REQUEST.value());
    }
    if (exception instanceof ValidationInternalException) {
        response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
    }
    if (exception instanceof DataAccessException) {
        result.setMessage("database access error");
    }
    try {
        if ("application/xml".equals(request.getHeader("Accept"))) {
            response.setHeader("Content-Type", "application/xml;charset=UTF-8");
            response.getWriter().print(result.toXmlString());
        } else {
            response.setHeader("Content-Type", "application/json;charset=UTF-8");
            response.getWriter().print(result.toJsonString());
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return new ModelAndView();
}
Also used : ServletRequestBindingException(org.springframework.web.bind.ServletRequestBindingException) ValidationException(com.shaoqunliu.validation.exception.ValidationException) RestfulResult(cn.opencil.vo.RestfulResult) ValidationInternalException(com.shaoqunliu.validation.exception.ValidationInternalException) ModelAndView(org.springframework.web.servlet.ModelAndView) IOException(java.io.IOException) HttpRequestMethodNotSupportedException(org.springframework.web.HttpRequestMethodNotSupportedException) DataAccessException(org.springframework.dao.DataAccessException) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with ValidationException

use of com.shaoqunliu.validation.exception.ValidationException in project CILManagement-Server by LiuinStein.

the class AbstractValidator method validate.

/**
 * @see com.shaoqunliu.validation.ValidationAdapter#validate(Object, Class[])
 */
@Override
public <T> T validate(T object, Class<?>... groups) throws ValidationException {
    sanityCheckGroups(groups);
    POJOReflection reflection = new POJOReflection(object);
    StringBuilder message = new StringBuilder(128);
    classFunctionMap.forEach((type, function) -> {
        if (isFailFast() && message.length() != 0) {
            return;
        }
        reflection.forEachAnnotationByFieldByType((field, annotation) -> {
            try {
                AnnotationReflection annotationReflection = new AnnotationReflection(annotation);
                Class<?>[] classes = (Class<?>[]) annotationReflection.getMember("groups");
                if ((groups.length == 0 && classes.length == 0) || Arrays.stream(classes).anyMatch(Arrays.asList(groups)::contains)) {
                    Object value = reflection.getValue(field.getName());
                    if (!function.apply(value, annotationReflection::getMember)) {
                        String annotationMessage = annotationReflection.getMember("message").toString();
                        message.append(annotationMessage.length() == 0 ? "invalid value " + value.toString() + " was given" : annotationMessage);
                    }
                }
            } catch (Exception ignored) {
            // ignored
            }
        }, type);
    });
    if (message.length() != 0) {
        throw new ValidationException(message.toString());
    }
    return object;
}
Also used : POJOReflection(com.shaoqunliu.reflection.POJOReflection) AnnotationReflection(com.shaoqunliu.reflection.AnnotationReflection) ValidationException(com.shaoqunliu.validation.exception.ValidationException) ValidationException(com.shaoqunliu.validation.exception.ValidationException)

Aggregations

ValidationException (com.shaoqunliu.validation.exception.ValidationException)2 RestfulResult (cn.opencil.vo.RestfulResult)1 AnnotationReflection (com.shaoqunliu.reflection.AnnotationReflection)1 POJOReflection (com.shaoqunliu.reflection.POJOReflection)1 ValidationInternalException (com.shaoqunliu.validation.exception.ValidationInternalException)1 IOException (java.io.IOException)1 NotNull (org.jetbrains.annotations.NotNull)1 DataAccessException (org.springframework.dao.DataAccessException)1 HttpRequestMethodNotSupportedException (org.springframework.web.HttpRequestMethodNotSupportedException)1 ServletRequestBindingException (org.springframework.web.bind.ServletRequestBindingException)1 ModelAndView (org.springframework.web.servlet.ModelAndView)1