private void assertNotPrimitiveTargetType(@Nullable TypeDescriptor sourceType, TypeDescriptor targetType) { if (targetType.isPrimitive()) { throw new ConversionFailedException(sourceType, targetType, null, new IllegalArgumentException("A null value cannot be assigned to a primitive type")); } }
@Test public void convertListOfNonStringifiable() { List<Object> list = Arrays.asList(new TestEntity(1L), new TestEntity(2L)); assertTrue(conversionService.canConvert(list.getClass(), String.class)); try { conversionService.convert(list, String.class); } catch (ConversionFailedException ex) { assertTrue(ex.getMessage().contains(list.getClass().getName())); assertTrue(ex.getCause() instanceof ConverterNotFoundException); assertTrue(ex.getCause().getMessage().contains(TestEntity.class.getName())); } }
private String getMessage(BindException cause) { ConversionFailedException conversionFailure = findCause(cause, ConversionFailedException.class); if (conversionFailure != null) { return "failed to convert " + conversionFailure.getSourceType() + " to " + conversionFailure.getTargetType(); } Throwable failure = cause; while (failure.getCause() != null) { failure = failure.getCause(); } return (StringUtils.hasText(failure.getMessage()) ? failure.getMessage() : cause.getMessage()); }
@Test public void scalarMapNotGenericSourceField() throws Exception { Map<String, String> map = new HashMap<>(); map.put("1", "9"); map.put("2", "37"); TypeDescriptor sourceType = new TypeDescriptor(getClass().getField("notGenericMapSource")); TypeDescriptor targetType = new TypeDescriptor(getClass().getField("scalarMapTarget")); assertTrue(conversionService.canConvert(sourceType, targetType)); try { conversionService.convert(map, sourceType, targetType); } catch (ConversionFailedException ex) { assertTrue(ex.getCause() instanceof ConverterNotFoundException); } conversionService.addConverterFactory(new StringToNumberConverterFactory()); assertTrue(conversionService.canConvert(sourceType, targetType)); @SuppressWarnings("unchecked") Map<Integer, Integer> result = (Map<Integer, Integer>) conversionService.convert(map, sourceType, targetType); assertFalse(map.equals(result)); assertEquals((Integer) 9, result.get(1)); assertEquals((Integer) 37, result.get(2)); }
} else if (e instanceof ConversionFailedException) { httpStatusCode = HttpStatus.UNPROCESSABLE_ENTITY.value(); message = getText(webRequest, "typeMismatch", ((ConversionFailedException) e).getValue(), ((ConversionFailedException) e).getTargetType()); if (!StringUtils.hasText(message)) { message = "data.valid.failed";
@ExceptionHandler(ConversionFailedException.class) @ResponseStatus(BAD_REQUEST) @ResponseBody public ErrorMessageResponse handleConversionFailedException(ConversionFailedException e) { LOG.info("ConversionFailedException occurred", e); return new ErrorMessageResponse(new ErrorMessage(e.getMessage())); }
private Collection<String> findValidValues(BindException ex) { ConversionFailedException conversionFailure = findCause(ex, ConversionFailedException.class); if (conversionFailure != null) { Object[] enumConstants = conversionFailure.getTargetType().getType() .getEnumConstants(); if (enumConstants != null) { return Stream.of(enumConstants).map(Object::toString) .collect(Collectors.toCollection(TreeSet::new)); } } return Collections.emptySet(); }
@Test public void scalarList() throws Exception { List<String> list = new ArrayList<>(); list.add("9"); list.add("37"); TypeDescriptor sourceType = TypeDescriptor.forObject(list); TypeDescriptor targetType = new TypeDescriptor(getClass().getField("scalarListTarget")); assertTrue(conversionService.canConvert(sourceType, targetType)); try { conversionService.convert(list, sourceType, targetType); } catch (ConversionFailedException ex) { assertTrue(ex.getCause() instanceof ConverterNotFoundException); } conversionService.addConverterFactory(new StringToNumberConverterFactory()); assertTrue(conversionService.canConvert(sourceType, targetType)); @SuppressWarnings("unchecked") List<Integer> result = (List<Integer>) conversionService.convert(list, sourceType, targetType); assertFalse(list.equals(result)); assertEquals(9, result.get(0).intValue()); assertEquals(37, result.get(1).intValue()); }
httpStatusCode = HttpStatus.UNPROCESSABLE_ENTITY.value(); message = getText(webRequest, "typeMismatch", ((ConversionFailedException) error).getValue(), ((ConversionFailedException) error).getTargetType()); if (!StringUtils.hasText(message)) { message = "data.valid.failed";
private String convertValueToDurationIfPossible(final String value) { try { val service = applicationContext.getEnvironment().getConversionService(); val dur = service.convert(value, Duration.class); if (dur != null) { return String.valueOf(dur.toMillis()); } } catch (final ConversionFailedException e) { LOGGER.trace(e.getMessage()); } return null; } }
@Nullable public static Object invokeConverter(GenericConverter converter, @Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { try { return converter.convert(source, sourceType, targetType); } catch (ConversionFailedException ex) { throw ex; } catch (Throwable ex) { throw new ConversionFailedException(sourceType, targetType, source, ex); } }
@Test public void collectionMap() throws Exception { Map<String, List<String>> map = new HashMap<>(); map.put("1", Arrays.asList("9", "12")); map.put("2", Arrays.asList("37", "23")); TypeDescriptor sourceType = TypeDescriptor.forObject(map); TypeDescriptor targetType = new TypeDescriptor(getClass().getField("collectionMapTarget")); assertTrue(conversionService.canConvert(sourceType, targetType)); try { conversionService.convert(map, sourceType, targetType); } catch (ConversionFailedException ex) { assertTrue(ex.getCause() instanceof ConverterNotFoundException); } conversionService.addConverter(new CollectionToCollectionConverter(conversionService)); conversionService.addConverterFactory(new StringToNumberConverterFactory()); assertTrue(conversionService.canConvert(sourceType, targetType)); @SuppressWarnings("unchecked") Map<Integer, List<Integer>> result = (Map<Integer, List<Integer>>) conversionService.convert(map, sourceType, targetType); assertFalse(map.equals(result)); assertEquals(Arrays.asList(9, 12), result.get(1)); assertEquals(Arrays.asList(37, 23), result.get(2)); }
public URL convert(String source) { try { return source == null ? null : new URL(source); } catch (MalformedURLException e) { throw new ConversionFailedException(SOURCE, TARGET, source, e); } } }
@Test public void scalarMap() throws Exception { Map<String, String> map = new HashMap<>(); map.put("1", "9"); map.put("2", "37"); TypeDescriptor sourceType = TypeDescriptor.forObject(map); TypeDescriptor targetType = new TypeDescriptor(getClass().getField("scalarMapTarget")); assertTrue(conversionService.canConvert(sourceType, targetType)); try { conversionService.convert(map, sourceType, targetType); } catch (ConversionFailedException ex) { assertTrue(ex.getCause() instanceof ConverterNotFoundException); } conversionService.addConverterFactory(new StringToNumberConverterFactory()); assertTrue(conversionService.canConvert(sourceType, targetType)); @SuppressWarnings("unchecked") Map<Integer, Integer> result = (Map<Integer, Integer>) conversionService.convert(map, sourceType, targetType); assertFalse(map.equals(result)); assertEquals((Integer) 9, result.get(1)); assertEquals((Integer) 37, result.get(2)); }
private void assertNotPrimitiveTargetType(@Nullable TypeDescriptor sourceType, TypeDescriptor targetType) { if (targetType.isPrimitive()) { throw new ConversionFailedException(sourceType, targetType, null, new IllegalArgumentException("A null value cannot be assigned to a primitive type")); } }
@Test public void testDefaultFormattersOff() throws Exception { FormattingConversionServiceFactoryBean factory = new FormattingConversionServiceFactoryBean(); factory.setRegisterDefaultFormatters(false); factory.afterPropertiesSet(); FormattingConversionService fcs = factory.getObject(); TypeDescriptor descriptor = new TypeDescriptor(TestBean.class.getDeclaredField("pattern")); try { fcs.convert("15,00", TypeDescriptor.valueOf(String.class), descriptor); fail("This format should not be parseable"); } catch (ConversionFailedException ex) { assertTrue(ex.getCause() instanceof NumberFormatException); } }
@Override public Object convert(@Nullable Object source, @Nullable TypeDescriptor sourceType, TypeDescriptor targetType) { throw new ConversionFailedException(sourceType, targetType, source, null); } });
if (e.getCause() instanceof ConverterNotFoundException && isArrayOrCollection(value)) { return null; } else {
@Nullable public static Object invokeConverter(GenericConverter converter, @Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { try { return converter.convert(source, sourceType, targetType); } catch (ConversionFailedException ex) { throw ex; } catch (Throwable ex) { throw new ConversionFailedException(sourceType, targetType, source, ex); } }
throw new ConversionFailedException(sourceType, targetType, source, ex.getTargetException()); throw new ConversionFailedException(sourceType, targetType, source, ex);