@ExceptionHandler(value=Exception.class) public ResultGeekQ<String> exceptionHandler(HttpServletRequest request , Exception e){ e.printStackTrace(); if(e instanceof GlobleException){ GlobleException ex= (GlobleException)e; return ResultGeekQ.error(ex.getStatus()); }else if( e instanceof BindException){ BindException ex = (BindException) e ; List<ObjectError> errors = ex.getAllErrors(); ObjectError error = errors.get(0); String msg = error.getDefaultMessage(); /** * 打印堆栈信息 */ logger.error(String.format(msg, msg)); return ResultGeekQ.error(SESSION_ERROR); }else { return ResultGeekQ.error(SYSTEM_ERROR); } } }
@Test public void modelAndView() throws Exception { BindException bindException = new BindException(new Object(), "target"); bindException.reject("errorCode"); ModelAndView mav = new ModelAndView("viewName"); mav.addObject("attrName", "attrValue"); mav.addObject(BindingResult.MODEL_KEY_PREFIX + "attrName", bindException); this.mvcResult.setMav(mav); this.handler.handle(this.mvcResult); assertValue("ModelAndView", "View name", "viewName"); assertValue("ModelAndView", "View", null); assertValue("ModelAndView", "Attribute", "attrName"); assertValue("ModelAndView", "value", "attrValue"); assertValue("ModelAndView", "errors", bindException.getAllErrors()); }
private ExceptionDetails getBindValidationExceptionDetails(Throwable rootFailure) { BindValidationException validationException = findCause(rootFailure, BindValidationException.class); if (validationException != null) { BindException target = findCause(rootFailure, BindException.class); List<ObjectError> errors = validationException.getValidationErrors() .getAllErrors(); return new ExceptionDetails(errors, target, validationException); } org.springframework.validation.BindException bindException = findCause( rootFailure, org.springframework.validation.BindException.class); if (bindException != null) { List<ObjectError> errors = bindException.getAllErrors(); return new ExceptionDetails(errors, bindException.getTarget(), bindException); } return null; }
@Test public void shouldFailIfGivenNull() { validator.validate(null, errors); Assert.assertTrue(errors.hasErrors()); Assert.assertEquals("error.general", errors.getAllErrors().get(0).getCode()); }
@ExceptionHandler(BindException.class) @ResponseStatus(HttpStatus.UNPROCESSABLE_ENTITY) @ResponseBody public List<ObjectError> handle(BindException exception) { return exception.getAllErrors(); }
@ExceptionHandler(BindException.class) @ResponseStatus(HttpStatus.UNPROCESSABLE_ENTITY) @ResponseBody public List<ObjectError> handle(BindException exception) { return exception.getAllErrors(); }
@ExceptionHandler(BindException.class) @ResponseStatus(HttpStatus.UNPROCESSABLE_ENTITY) @ResponseBody public List<ObjectError> handle(BindException exception) { return exception.getAllErrors(); }
@ExceptionHandler(BindException.class) @ResponseStatus(HttpStatus.UNPROCESSABLE_ENTITY) @ResponseBody public List<ObjectError> handle(BindException exception) { return exception.getAllErrors(); }
@ExceptionHandler(BindException.class) @ResponseStatus(HttpStatus.UNPROCESSABLE_ENTITY) @ResponseBody public List<ObjectError> handle(BindException exception) { return exception.getAllErrors(); }
@ExceptionHandler(BindException.class) @ResponseStatus(HttpStatus.UNPROCESSABLE_ENTITY) @ResponseBody public List<ObjectError> handle(BindException exception) { return exception.getAllErrors(); }
@ExceptionHandler(BindException.class) @ResponseStatus(BAD_REQUEST) @ResponseBody public UiError bindErrorHandler(BindException bindException) { log.error("Can't process request", bindException); return createResponse(bindException.getMessage(), bindException.getAllErrors().toString(), BAD_REQUEST); }
/** * 参数绑定异常处理 * * @param cause 异常 * @return 响应结果 */ @ExceptionHandler(BindException.class) @ResponseBody public RespData handleBindException(BindException cause, HttpServletRequest request) { // 输出错误信息 log.info("== 请求参数异常 =="); List<ObjectError> errors = cause.getAllErrors(); for (ObjectError error : errors) { log.info(error.toString()); } log.info(cause.getMessage(), cause); return validator(errors, request); }
@ExceptionHandler(value = Exception.class)//拦截所有异常 public Result<String> exceptionHandler(HttpServletRequest request, Exception e){ e.printStackTrace(); if(e instanceof GlobalException) { GlobalException ex = (GlobalException)e; return Result.error(ex.getCodeMsg()); }else if(e instanceof BindException) { BindException ex = (BindException)e; List<ObjectError> errors = ex.getAllErrors();//绑定错误返回很多错误,是一个错误列表,只需要第一个错误 ObjectError error = errors.get(0); String msg = error.getDefaultMessage(); return Result.error(CodeMsg.BIND_ERROR.fillArgs(msg));//给状态码填充参数 }else { return Result.error(CodeMsg.SERVER_ERROR); } } }
/** Controllerへのリクエスト紐付け例外 */ @ExceptionHandler(BindException.class) public ResponseEntity<Map<String, String[]>> handleBind(BindException e) { log.warn(e.getMessage()); return new ErrorHolder(msg, locale(), convert(e.getAllErrors()).list()).result(HttpStatus.BAD_REQUEST); }
@ExceptionHandler(value=Exception.class) public Result<String> exceptionHandler(HttpServletRequest request, Exception e){ e.printStackTrace(); if(e instanceof HfbinException) { HfbinException ex = (HfbinException)e; return Result.error(ex.getCm()); }else if(e instanceof BindException) { BindException ex = (BindException)e; List<ObjectError> errors = ex.getAllErrors(); ObjectError error = errors.get(0); String msg = error.getDefaultMessage(); return Result.error(CodeMsg.BIND_ERROR.fillArgs(msg)); }else { return Result.error(CodeMsg.SERVER_ERROR); } } }
@ExceptionHandler(value = Exception.class) public Result<String> exceptionHandler(HttpServletRequest request, Exception e) { e.printStackTrace(); if (e instanceof GlobalException) { GlobalException ex = (GlobalException) e; return Result.error(ex.getCodeMsg()); } else if (e instanceof BindException) { BindException ex = (BindException) e; List<ObjectError> errors = ex.getAllErrors(); ObjectError error = errors.get(0); String msg = error.getDefaultMessage(); return Result.error(CodeMsg.BIND_ERROR.fillArgs(msg)); } else { return Result.error(CodeMsg.SERVER_ERROR); } } }
@Override public boolean matches(Object item) { BindException ex = (BindException) ((Exception) item).getCause(); ObjectError error = ex.getAllErrors().get(0); boolean messageMatches = message.equals(error.getDefaultMessage()); if (field == null) { return messageMatches; } String fieldErrors = ((FieldError) error).getField(); return messageMatches && fieldErrors.equals(field); }
@ExceptionHandler(value=Exception.class) public Result<String> exceptionHandler(HttpServletRequest request, Exception e){ e.printStackTrace(); if(e instanceof GlobalException) { GlobalException ex = (GlobalException)e; return Result.error(ex.getCm()); }else if(e instanceof BindException) { BindException ex = (BindException)e; List<ObjectError> errors = ex.getAllErrors(); ObjectError error = errors.get(0); String msg = error.getDefaultMessage(); return Result.error(CodeMsg.BIND_ERROR.fillArgs(msg)); }else { return Result.error(CodeMsg.SERVER_ERROR); } } }
@Test public void test_handleBindException_returns_errors() throws Exception { BindException be = mock(BindException.class); ObjectError fakeError1 = new ObjectError("orgEnrollmentConfig", "fakeErrorMessage1"); ObjectError fakeError2 = new ObjectError("orgEnrollmentConfig", "fakeErrorMessage2"); when(be.getAllErrors()).thenReturn(Arrays.asList(fakeError1, fakeError2)); HttpServletResponse response = mock(HttpServletResponse.class); ResponseEntity<List<ObjectError>> errors = autoEnrollmentController.handleBindException(be, response); assertTrue(errors.getBody() != null && errors.getBody().size() == 2); assertTrue(errors.getStatusCode() == HttpStatus.BAD_REQUEST); }
public static void main(String[] args) throws Exception { // below is output *before* logging is configured so will appear on console logVersionInfo(); try { SpringApplication.exit(new SpringApplicationBuilder(VacuumTool.class) .properties("spring.config.location:${config:null}") .properties("spring.profiles.active:" + Modules.REPLICATION) .properties("instance.home:${user.home}") .properties("instance.name:${source-catalog.name}_${replica-catalog.name}") .bannerMode(Mode.OFF) .registerShutdownHook(true) .build() .run(args)); } catch (BeanCreationException e) { if (e.getMostSpecificCause() instanceof BindException) { printVacuumToolHelp(((BindException) e.getMostSpecificCause()).getAllErrors()); } throw e; } }