Skip to content

Commit

Permalink
Merge pull request #907 from jdbi/type-missing-error
Browse files Browse the repository at this point in the history
Improve error message thrown when a generic type cannot be inferred and you must #bindByType
  • Loading branch information
qualidafial committed Oct 8, 2017
2 parents 332f2b3 + 7bd6244 commit c504b67
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
14 changes: 13 additions & 1 deletion core/src/main/java/org/jdbi/v3/core/statement/SqlStatement.java
Expand Up @@ -18,6 +18,7 @@
import java.io.InputStream;
import java.io.Reader;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.math.BigDecimal;
import java.net.URL;
import java.sql.Blob;
Expand Down Expand Up @@ -1067,7 +1068,18 @@ private Argument toArgument(Object value) {

private Argument toArgument(Type type, Object value) {
return getConfig(Arguments.class).findFor(type, value)
.orElseThrow(() -> new UnsupportedOperationException("No argument factory registered for '" + value + "' of type " + type));
.orElseThrow(() -> factoryNotFound(type, value));
}

private UnsupportedOperationException factoryNotFound(Type type, Object value) {
if (type instanceof Class<?>) { // not a ParameterizedType
final TypeVariable<?>[] params = ((Class<?>) type).getTypeParameters();
if (params.length > 0) {
return new UnsupportedOperationException("No type parameters found for erased type '" + type + Arrays.toString(params) +
"'. To bind a generic type, prefer using bindByType.");
}
}
return new UnsupportedOperationException("No argument factory registered for '" + value + "' of type " + type);
}

/**
Expand Down
@@ -0,0 +1,38 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jdbi.v3.core.argument;

import static org.assertj.core.api.Assertions.assertThatThrownBy;

import java.util.Collections;

import org.jdbi.v3.core.Handle;
import org.jdbi.v3.core.rule.PgDatabaseRule;
import org.junit.Rule;
import org.junit.Test;

public class TestCollectionArguments {
@Rule
public PgDatabaseRule db = new PgDatabaseRule();

@Test
public void testBindTypeErased() throws Exception {
try (final Handle h = db.openHandle()) {
assertThatThrownBy( () ->
h.execute("SELECT * FROM something WHERE id = ANY(:ids)", Collections.singleton(1)))
.isInstanceOf(UnsupportedOperationException.class)
.hasMessageContaining("No type parameters found");
}
}
}

0 comments on commit c504b67

Please sign in to comment.