Drupal 7 db_error() equivalent? I'm trying to figure out how to get the last database error after a query fails, and I'm having trouble finding the appropriate function. It seems up until Drupal 7 there was a db_error() function that would return the information.

How should I retrieve a database error in Drupal 7?

My code:

try {
    $q = db_query($q);
} catch (Exception $e) {
    $error = db_error(); // this is what I'd like to do
    $transaction-&gt;rollback();
    watchdog_exception('my_module', $e, t('Caught an error: ' . $error));
}


I'm aware that I can get some information from the exception, but I'd like to get it directly from the Drupal 7 API.
The Drupal 7 database API piggy-backs straight onto PDO. It doesn't define its own exceptions, or messages; it actually doesn't even attempt to handle any exceptions, it leaves them for the caller to handle directly.

The most descriptive error message you can get is in the exception object.

catch (\PDOException $e) {
  $error = $e-&gt;getMessage();