aaberg / sql2o

sql2o is a small library, which makes it easy to convert the result of your sql-statements into objects. No resultset hacking required. Kind of like an orm, but without the sql-generation capabilities. Supports named parameters.
http://sql2o.org
MIT License
1.14k stars 229 forks source link

MethodSetter setProperty failing with generic IllegalArgumentException #357

Open ghost opened 2 years ago

ghost commented 2 years ago

The method simply propagates the exception without offering any feedback.

Scenario, you make an incorrect column mapping class, let's say that you use java.util.Instant instead of java.sql.timestamp. Everything else is correct, just a type mismatch.

Then you do a simple select

var queryFormat = "Select * from %s where %s = :value";
return connection.createQuery(String.format(queryFormat, tableName, columnName))
        .addParameter("value", value)
        .setColumnMappings(provideColumnMapping())
        .executeAndFetch(clazz); // the class containing the incorrect column type

This eventually goes to: org.sql2o.reflection.MethodSetter#setProperty(Object obj, Object value)

Here, the try catch only cares about IllegalAccessException and InvocationTargetException. The IllegalArgumentException (or any other exception) is ignored and I don't get any feedback on what failed. What class? What method? What type? No idea.

I would expect the following, at least:

...
catch (IllegalArgumentException e) {
    throw new Sql2oException("error while calling setter method with name " + method.getName() + " on class " + obj.getClass().toString(), e);
}

But honestly, I'd even go a step further and do this for any exception.

EDIT: Using latest version, 1.6.0