@ExceptionHandler(ConstraintViolationException.class) public ResponseEntity<Map<String, Object>> handleConstraintViolationException( HttpServletRequest request, ConstraintViolationException ex ) { return handleError(request, BAD_REQUEST, new BadRequestException(ex.getMessage())); }
@JsonProperty("cause") public String getCause() { return cve.getMessage(); }
public static void serialize(SerializationStreamWriter streamWriter, ConstraintViolationException instance) throws SerializationException { streamWriter.writeString(instance.getMessage()); streamWriter.writeObject(instance.getConstraintViolations()); }
/** * Returns true of this Transaction can be committed on this object * * @param t is the transaction to commit, should be the same as the * one passed during the join(Transaction t) call. * @return true if the trsaction commiting would be successful */ public synchronized boolean canCommit(Transaction t) throws TransactionFailure { if (!isDeleted) { // HK2-127: validate only if not marked for deletion Set constraintViolations = beanValidator.validate(this.getProxy(this.getProxyType())); try { handleValidationException(constraintViolations); } catch (ConstraintViolationException constraintViolationException) { throw new TransactionFailure(constraintViolationException.getMessage(), constraintViolationException); } } return currentTx==t; }
@JsonProperty("cause") public String getCause() { return cve.getMessage(); }
@ExceptionHandler(ConstraintViolationException.class) @ResponseBody public ResponseEntity<List<ConstraintViolationInfo>> handleConstraintViolation(ConstraintViolationException e) { log.debug("ConstraintViolationException: {}, violations:\n{}", e.getMessage(), e.getConstraintViolations()); List<ConstraintViolationInfo> violationInfos = getConstraintViolationInfos(e.getConstraintViolations()); return new ResponseEntity<>(violationInfos, HttpStatus.BAD_REQUEST); }
/** * Constructor from errors. * * @param validation * validation exception containing errors. */ public ValidationJsonException(final ConstraintViolationException validation) { this(validation.getMessage()); validation.getConstraintViolations() .forEach(e -> errors.computeIfAbsent(getPropertyPath(e), k -> new ArrayList<>()).add(serializeHibernateValidationError(e))); }
@ExceptionHandler(ConstraintViolationException.class) @ResponseStatus(HttpStatus.BAD_REQUEST) public R handleError(ConstraintViolationException e) { log.warn("参数验证失败", e.getMessage()); Set<ConstraintViolation<?>> violations = e.getConstraintViolations(); ConstraintViolation<?> violation = violations.iterator().next(); String path = ((PathImpl) violation.getPropertyPath()).getLeafNode().getName(); String message = String.format("%s:%s", path, violation.getMessage()); return R.fail(ResultCode.PARAM_VALID_ERROR, message); }
@ResponseBody @ResponseStatus(HttpStatus.OK) @ExceptionHandler(ConstraintViolationException.class) public Result handleValidationException(ConstraintViolationException e) { log.error(ErrorStatus.ILLEGAL_DATA.getMessage() + ":" + e.getMessage()); List<Map<String, Object>> fields = new ArrayList<>(); for (ConstraintViolation<?> cv : e.getConstraintViolations()) { String fieldName = ((PathImpl) cv.getPropertyPath()).getLeafNode().asString(); String message = cv.getMessage(); Map<String, Object> field = new HashMap<>(); field.put("field", fieldName); field.put("message", message); fields.add(field); } return failure(ErrorStatus.ILLEGAL_DATA, fields); }
@ResponseStatus(HttpStatus.OK) @ExceptionHandler(ConstraintViolationException.class) @ResponseBody public ResponseMessage<ValidError> handlerConstraintViolationException(ConstraintViolationException exception) { ConstraintViolation violation = Exceptions.getFirstError(exception); if (violation != null) { logger.warn("Valid Exception:{}", violation.getMessage()); } else { logger.warn(exception.getMessage(), exception); } ValidError validError = new ValidError(violation.getPropertyPath().toString(), violation.getMessage()); return Result.error(ResponseMessageCodeEnum.ERROR.getCode(), validError.getMessage(), validError); }
private void validateIfRequired(File resourceDir, ResourceClassifier classifier) throws MojoExecutionException, MojoFailureException { try { if (!skipResourceValidation) { new ResourceValidator(resourceDir, classifier, log).validate(); } } catch (ConstraintViolationException e) { if (failOnValidationError) { log.error("[[R]]" + e.getMessage() + "[[R]]"); log.error("[[R]]use \"mvn -Dfabric8.skipResourceValidation=true\" option to skip the validation[[R]]"); throw new MojoFailureException("Failed to generate fabric8 descriptor"); } else { log.warn("[[Y]]" + e.getMessage() + "[[Y]]"); } } catch (Throwable e) { if (failOnValidationError) { throw new MojoExecutionException("Failed to validate resources", e); } else { log.warn("Failed to validate resources: %s", e.getMessage()); } } }
/** BeanValidation(JSR303)の制約例外 */ @ExceptionHandler(ConstraintViolationException.class) public ResponseEntity<Map<String, String[]>> handleConstraintViolation(ConstraintViolationException e) { log.warn(e.getMessage()); Warns warns = Warns.init(); e.getConstraintViolations().forEach((v) -> warns.add(v.getPropertyPath().toString(), v.getMessage())); return new ErrorHolder(msg, locale(), warns.list()).result(HttpStatus.BAD_REQUEST); }
public static void serialize(SerializationStreamWriter streamWriter, ConstraintViolationException instance) throws SerializationException { streamWriter.writeString(instance.getMessage()); streamWriter.writeObject(instance.getConstraintViolations()); }
public static void serialize(SerializationStreamWriter streamWriter, ConstraintViolationException instance) throws SerializationException { streamWriter.writeString(instance.getMessage()); streamWriter.writeObject(instance.getConstraintViolations()); }
/** * handles Hibernate validation exception */ public void afterThrowing(Method m, Object[] args, Object target, ConstraintViolationException e) { Logger log = LoggerFactory.getLogger(target.getClass()); LogMessage message = new LogMessage(mapLogCode(ValidationException.ERROR_CODE), excMessage(e)); log.debug("{}", message); ValidationException newException = new ValidationException(e.getMessage()); newException.setLogged(true); newException.setConstraintViolations(e.getConstraintViolations()); throw newException; }
/** * Returns true of this Transaction can be committed on this object * * @param t is the transaction to commit, should be the same as the * one passed during the join(Transaction t) call. * @return true if the trsaction commiting would be successful */ public synchronized boolean canCommit(Transaction t) throws TransactionFailure { Set constraintViolations = beanValidator.validate(this.getProxy(this.getProxyType())); try { handleValidationException(constraintViolations); } catch (ConstraintViolationException constraintViolationException) { throw new TransactionFailure(constraintViolationException.getMessage(), constraintViolationException); } return currentTx==t; }
/** * Returns true of this Transaction can be committed on this object * * @param t is the transaction to commit, should be the same as the * one passed during the join(Transaction t) call. * @return true if the trsaction commiting would be successful */ public synchronized boolean canCommit(Transaction t) throws TransactionFailure { if (!isDeleted) { // HK2-127: validate only if not marked for deletion Set constraintViolations = beanValidator.validate(this.getProxy(this.getProxyType())); try { handleValidationException(constraintViolations); } catch (ConstraintViolationException constraintViolationException) { throw new TransactionFailure(constraintViolationException.getMessage(), constraintViolationException); } } return currentTx==t; }
/** * Returns true of this Transaction can be committed on this object * * @param t is the transaction to commit, should be the same as the * one passed during the join(Transaction t) call. * @return true if the trsaction commiting would be successful */ public synchronized boolean canCommit(Transaction t) throws TransactionFailure { Set constraintViolations = beanValidator.validate(this.getProxy(this.getProxyType())); try { handleValidationException(constraintViolations); } catch (ConstraintViolationException constraintViolationException) { throw new TransactionFailure(constraintViolationException.getMessage(), constraintViolationException); } return currentTx==t; }
/** * Returns true of this Transaction can be committed on this object * * @param t is the transaction to commit, should be the same as the * one passed during the join(Transaction t) call. * @return true if the trsaction commiting would be successful */ public synchronized boolean canCommit(Transaction t) throws TransactionFailure { if (!isDeleted) { // HK2-127: validate only if not marked for deletion Set constraintViolations = beanValidator.validate(this.getProxy(this.getProxyType())); try { handleValidationException(constraintViolations); } catch (ConstraintViolationException constraintViolationException) { throw new TransactionFailure(constraintViolationException.getMessage(), constraintViolationException); } } return currentTx==t; }
@Override public HttpResponse<JsonError> handle(HttpRequest request, ConstraintViolationException exception) { Set<ConstraintViolation<?>> constraintViolations = exception.getConstraintViolations(); if (constraintViolations == null || constraintViolations.isEmpty()) { JsonError error = new JsonError(exception.getMessage() == null ? HttpStatus.BAD_REQUEST.getReason() : exception.getMessage()); error.link(Link.SELF, Link.of(request.getUri())); return HttpResponse.badRequest(error); } else if (constraintViolations.size() == 1) { ConstraintViolation<?> violation = constraintViolations.iterator().next(); JsonError error = new JsonError(buildMessage(violation)); error.link(Link.SELF, Link.of(request.getUri())); return HttpResponse.badRequest(error); } else { JsonError error = new JsonError(HttpStatus.BAD_REQUEST.getReason()); List<Resource> errors = new ArrayList<>(); for (ConstraintViolation<?> violation : constraintViolations) { errors.add(new JsonError(buildMessage(violation))); } error.embedded("errors", errors); error.link(Link.SELF, Link.of(request.getUri())); return HttpResponse.badRequest(error); } }