Search in sources :

Example 26 with BindException

use of org.springframework.validation.BindException in project disconf by knightliao.

the class MyExceptionHandler method resolveException.

@Override
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object o, Exception e) {
    LOG.warn(request.getRequestURI() + " ExceptionHandler FOUND. " + e.toString() + "\t" + e.getCause());
    // PathVariable 出错
    if (e instanceof TypeMismatchException) {
        return getParamErrors((TypeMismatchException) e);
    // Bean 参数无法映射错误
    } else if (e instanceof InvalidPropertyException) {
        return getParamErrors((InvalidPropertyException) e);
    // @Valid 出错
    } else if (e instanceof BindException) {
        return ParamValidateUtils.getParamErrors((BindException) e);
    // 业务校验处理
    } else if (e instanceof FieldException) {
        return getParamErrors((FieldException) e);
    } else if (e instanceof DocumentNotFoundException) {
        response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        try {
            FileUtils.closeWriter(response.getWriter());
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        return null;
    // 用户没有请求方法的访问权限
    } else if (e instanceof AccessDeniedException) {
        LOG.warn("details: " + ((AccessDeniedException) e).getErrorMessage());
        return buildError("auth.access.denied", ErrorCode.ACCESS_NOAUTH_ERROR);
    } else if (e instanceof HttpRequestMethodNotSupportedException) {
        return buildError("syserror.httpmethod", ErrorCode.HttpRequestMethodNotSupportedException);
    } else if (e instanceof MissingServletRequestParameterException) {
        return buildError("syserror.param.miss", ErrorCode.MissingServletRequestParameterException);
    } else if (e instanceof GlobalExceptionAware) {
        LOG.error("details: ", e);
        GlobalExceptionAware g = (GlobalExceptionAware) e;
        return buildError(g.getErrorMessage(), g.getErrorCode());
    } else {
        LOG.warn("details: ", e);
        return buildError("syserror.inner", ErrorCode.GLOBAL_ERROR);
    }
}
Also used : AccessDeniedException(com.baidu.dsp.common.exception.AccessDeniedException) FieldException(com.baidu.dsp.common.exception.FieldException) DocumentNotFoundException(com.baidu.dsp.common.exception.DocumentNotFoundException) GlobalExceptionAware(com.baidu.dsp.common.exception.base.GlobalExceptionAware) TypeMismatchException(org.springframework.beans.TypeMismatchException) InvalidPropertyException(org.springframework.beans.InvalidPropertyException) BindException(org.springframework.validation.BindException) IOException(java.io.IOException) MissingServletRequestParameterException(org.springframework.web.bind.MissingServletRequestParameterException) HttpRequestMethodNotSupportedException(org.springframework.web.HttpRequestMethodNotSupportedException)

Example 27 with BindException

use of org.springframework.validation.BindException in project spring-framework by spring-projects.

the class DefaultHandlerExceptionResolverTests method handleBindException.

@Test
public void handleBindException() throws Exception {
    BindException ex = new BindException(new Object(), "name");
    ModelAndView mav = exceptionResolver.resolveException(request, response, null, ex);
    assertNotNull("No ModelAndView returned", mav);
    assertTrue("No Empty ModelAndView returned", mav.isEmpty());
    assertEquals("Invalid status code", 400, response.getStatus());
}
Also used : ModelAndView(org.springframework.web.servlet.ModelAndView) BindException(org.springframework.validation.BindException) Test(org.junit.Test)

Example 28 with BindException

use of org.springframework.validation.BindException in project spring-framework by spring-projects.

the class RequestContext method getErrors.

/**
	 * Retrieve the Errors instance for the given bind object.
	 * @param name name of the bind object
	 * @param htmlEscape create an Errors instance with automatic HTML escaping?
	 * @return the Errors instance, or {@code null} if not found
	 */
public Errors getErrors(String name, boolean htmlEscape) {
    if (this.errorsMap == null) {
        this.errorsMap = new HashMap<>();
    }
    Errors errors = this.errorsMap.get(name);
    boolean put = false;
    if (errors == null) {
        errors = (Errors) getModelObject(BindingResult.MODEL_KEY_PREFIX + name);
        // Check old BindException prefix for backwards compatibility.
        if (errors instanceof BindException) {
            errors = ((BindException) errors).getBindingResult();
        }
        if (errors == null) {
            return null;
        }
        put = true;
    }
    if (htmlEscape && !(errors instanceof EscapedErrors)) {
        errors = new EscapedErrors(errors);
        put = true;
    } else if (!htmlEscape && errors instanceof EscapedErrors) {
        errors = ((EscapedErrors) errors).getSource();
        put = true;
    }
    if (put) {
        this.errorsMap.put(name, errors);
    }
    return errors;
}
Also used : Errors(org.springframework.validation.Errors) EscapedErrors(org.springframework.web.bind.EscapedErrors) BindException(org.springframework.validation.BindException) EscapedErrors(org.springframework.web.bind.EscapedErrors)

Example 29 with BindException

use of org.springframework.validation.BindException in project spring-framework by spring-projects.

the class ResponseEntityExceptionHandlerTests method bindException.

@Test
public void bindException() {
    Exception ex = new BindException(new Object(), "name");
    testException(ex);
}
Also used : BindException(org.springframework.validation.BindException) MissingPathVariableException(org.springframework.web.bind.MissingPathVariableException) HttpMessageNotWritableException(org.springframework.http.converter.HttpMessageNotWritableException) NoHandlerFoundException(org.springframework.web.servlet.NoHandlerFoundException) MissingServletRequestPartException(org.springframework.web.multipart.support.MissingServletRequestPartException) BindException(org.springframework.validation.BindException) ConversionNotSupportedException(org.springframework.beans.ConversionNotSupportedException) AsyncRequestTimeoutException(org.springframework.web.context.request.async.AsyncRequestTimeoutException) MissingServletRequestParameterException(org.springframework.web.bind.MissingServletRequestParameterException) MethodArgumentNotValidException(org.springframework.web.bind.MethodArgumentNotValidException) ServletRequestBindingException(org.springframework.web.bind.ServletRequestBindingException) TypeMismatchException(org.springframework.beans.TypeMismatchException) HttpMessageNotReadableException(org.springframework.http.converter.HttpMessageNotReadableException) HttpMediaTypeNotSupportedException(org.springframework.web.HttpMediaTypeNotSupportedException) HttpRequestMethodNotSupportedException(org.springframework.web.HttpRequestMethodNotSupportedException) HttpMediaTypeNotAcceptableException(org.springframework.web.HttpMediaTypeNotAcceptableException) Test(org.junit.Test)

Example 30 with BindException

use of org.springframework.validation.BindException in project spring-framework by spring-projects.

the class BindTagTests method propertyExposing.

@Test
public void propertyExposing() throws JspException {
    PageContext pc = createPageContext();
    TestBean tb = new TestBean();
    tb.setName("name1");
    Errors errors = new BindException(tb, "tb");
    errors.rejectValue("name", "code1", null, "message & 1");
    errors.rejectValue("name", "code2", null, "message2");
    pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", errors);
    // test global property (should be null)
    BindTag tag = new BindTag();
    tag.setPageContext(pc);
    tag.setPath("tb");
    assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE);
    assertNull(tag.getProperty());
    // test property set (tb.name)
    tag.release();
    tag.setPageContext(pc);
    tag.setPath("tb.name");
    assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE);
    assertEquals("name", tag.getProperty());
}
Also used : Errors(org.springframework.validation.Errors) IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) TestBean(org.springframework.tests.sample.beans.TestBean) NestedTestBean(org.springframework.tests.sample.beans.NestedTestBean) BindException(org.springframework.validation.BindException) PageContext(javax.servlet.jsp.PageContext) Test(org.junit.Test)

Aggregations

BindException (org.springframework.validation.BindException)40 Errors (org.springframework.validation.Errors)15 LocationMonitorIdCommand (org.opennms.web.svclayer.model.LocationMonitorIdCommand)11 Test (org.junit.Test)9 ObjectError (org.springframework.validation.ObjectError)9 ThrowableAnticipator (org.opennms.test.ThrowableAnticipator)8 DistributedStatusDetailsCommand (org.opennms.web.svclayer.model.DistributedStatusDetailsCommand)8 OnmsLocationMonitor (org.opennms.netmgt.model.OnmsLocationMonitor)6 Map (java.util.Map)3 SimpleWebTable (org.opennms.web.svclayer.model.SimpleWebTable)3 BindingResult (org.springframework.validation.BindingResult)3 ModelAndView (org.springframework.web.servlet.ModelAndView)3 HashMap (java.util.HashMap)2 List (java.util.List)2 OnmsLocationSpecificStatus (org.opennms.netmgt.model.OnmsLocationSpecificStatus)2 StatisticsReportCommand (org.opennms.web.svclayer.model.StatisticsReportCommand)2 BeanWrapper (org.springframework.beans.BeanWrapper)2 BeanWrapperImpl (org.springframework.beans.BeanWrapperImpl)2 TypeMismatchException (org.springframework.beans.TypeMismatchException)2 FieldError (org.springframework.validation.FieldError)2