Fix query error handling
Adding Nicolas Patry as contributor

The last commit uses his test case.