Skip to content

Commit

Permalink
Better error handling when wrong type in default
Browse files Browse the repository at this point in the history
  • Loading branch information
pontusmelke committed Jul 12, 2016
1 parent b469fd9 commit ee54249
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
Expand Up @@ -53,7 +53,7 @@ interface NeoValueConverter
{
AnyType type();
Object toNeoValue( Object javaValue ) throws ProcedureException;
Optional<Object> defaultValue(Name parameter);
Optional<Object> defaultValue(Name parameter) throws ProcedureException;
}

private final Map<Type,NeoValueConverter> javaToNeo = new HashMap<>();
Expand Down Expand Up @@ -139,7 +139,7 @@ public void registerType( Class<?> javaClass, NeoValueConverter toNeo )

private NeoValueConverter toList( NeoValueConverter inner )
{
return new SimpleConverter( NTList( inner.type() ), List.class,s -> {
return new SimpleConverter( NTList( inner.type() ), List.class, s -> {
throw new UnsupportedOperationException("Default values for type List is not supported" );
} );
}
Expand Down Expand Up @@ -175,7 +175,7 @@ public SimpleConverter( AnyType type, Class<?> javaClass, Function<String,Object
this.defaultConverter = defaultConverter;
}

public Optional<Object> defaultValue(Name parameter)
public Optional<Object> defaultValue(Name parameter) throws ProcedureException
{
String defaultValue = parameter.defaultValue();
if ( defaultValue.equals( Name.DEFAULT_VALUE ) )
Expand All @@ -184,7 +184,15 @@ public Optional<Object> defaultValue(Name parameter)
}
else
{
return Optional.of( defaultConverter.apply( defaultValue ) );
try
{
return Optional.of( defaultConverter.apply( defaultValue ) );
}
catch ( Exception e )
{
throw new ProcedureException( Status.Procedure.ProcedureRegistrationFailed,
"Default value `%s` could not be parsed as a %s", parameter.defaultValue(), javaClass.getSimpleName() );
}
}
}

Expand Down
Expand Up @@ -123,6 +123,19 @@ public void shouldFailIfMisplacedDefaultValue() throws Throwable
compile( ClassWithProcedureWithMisplacedDefault.class );
}

@Test
public void shouldFailIfWronglyTypedDefaultValue() throws Throwable
{
// Expect
exception.expect( ProcedureException.class );
exception.expectMessage( String.format("Argument `a` at position 0 in `defaultValues` with%n" +
"type `long` cannot be converted to a Neo4j type: Default value `forty-two` could not be parsed as a " +
"Long" ));

// When
compile( ClassWithProcedureWithBadlyTypedDefault.class );
}

public static class MyOutputRecord
{
public String name;
Expand Down Expand Up @@ -188,6 +201,15 @@ public Stream<MyOutputRecord> defaultValues( @Name( "a" ) String a , @Name( valu
}
}

public static class ClassWithProcedureWithBadlyTypedDefault
{
@Procedure
public Stream<MyOutputRecord> defaultValues( @Name( value = "a", defaultValue = "forty-two") long b)
{
return Stream.empty();
}
}

private List<CallableProcedure> compile( Class<?> clazz ) throws KernelException
{
return new ReflectiveProcedureCompiler( new TypeMappers(), new ComponentRegistry() ).compile( clazz );
Expand Down

0 comments on commit ee54249

Please sign in to comment.