backport sqlite3 exception fix

https://github.com/rails/rails/pull/13291