More safely format large exception messages.

The immediate reason for this is that SQLAlchemy may return an excessively
large error message if, say the MySQL server goes away because you're trying to
insert a large object. Trying to log the entire 25MB insert statement is not
that helpful, and breaks various things.