/** * {@inheritDoc} */ @Override public Response toResponse(final ConstraintViolationException exception) { logger.trace(LocalizationMessages.CONSTRAINT_VIOLATIONS_ENCOUNTERED(), exception); Response.Status status = ValidationHelper.getResponseStatus(exception); ErrorMessage errorMessage = ErrorMessage.fromStatus(status.getStatusCode()); errorMessage.setThrowable(exception); errorMessage.setCode(Hashing.murmur3_32().hashUnencodedChars(exception.getClass().getName()).toString()); List<Result.Error> errors = ValidationHelper.constraintViolationToValidationErrors(exception); errorMessage.setErrors(errors); return Response.status(status) .entity(errorMessage) .type(ExceptionMapperUtils.getResponseType()) .build(); }
@Override public Response toResponse(final ValidationException exception) { if (exception instanceof ConstraintViolationException) { LOGGER.log(Level.FINER, LocalizationMessages.CONSTRAINT_VIOLATIONS_ENCOUNTERED(), exception); final ConstraintViolationException cve = (ConstraintViolationException) exception; final Response.ResponseBuilder response = Response.status(getStatus(cve)); // Entity final List<Variant> variants = Variant.mediaTypes( MediaType.APPLICATION_XML_TYPE, MediaType.APPLICATION_JSON_TYPE).build(); final Variant variant = request.get().selectVariant(variants); if (variant != null) { response.type(variant.getMediaType()); } else { /* * default media type which will be used only when none media type from {@value variants} is in * accept header of original request. */ response.type(MediaType.TEXT_PLAIN_TYPE); } response.entity( new GenericEntity<List<ValidationError>>( getEntity(cve.getConstraintViolations()), new GenericType<List<ValidationError>>() {}.getType() ) ); return response.build(); } else { LOGGER.log(Level.WARNING, LocalizationMessages.VALIDATION_EXCEPTION_RAISED(), exception); return Response.serverError().entity(exception.getMessage()).build(); } }
/** * implement toResponse with code, message. * * @param e the validation exception object * @param code the code * @param message the message * * @return the response */ private Response toResponse(ConstraintViolationException e, int code, String message) { LOGGER.log(Level.FINER, LocalizationMessages.CONSTRAINT_VIOLATIONS_ENCOUNTERED(), e); List<ValidationError> errors = ValidationHelper.constraintViolationToValidationErrors(e); ParsecErrorResponse<ValidationError> errorResponse = ValidateUtil.buildErrorResponse(errors, code, message); return Response.status(Response.Status.BAD_REQUEST) .entity(new GenericEntity<ParsecErrorResponse<ValidationError>>(errorResponse) { }).build(); }