Handle errors in SQL queries with UTF8-incompatible encodings.

This should fix #5738, per the discussion in #13023.