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);
}
}
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());
}
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;
}
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);
}
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());
}
Aggregations