Skip to content

Instantly share code, notes, and snippets.

@chrisguitarguy
Created May 6, 2013 03:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisguitarguy/e021918900e93dca304d to your computer and use it in GitHub Desktop.
Save chrisguitarguy/e021918900e93dca304d to your computer and use it in GitHub Desktop.
<?php
abstract class AbstractPlatform
{
//...
/**
* Throw a more specific exception based on a SQL error code.
*
* @param \PDOException $e
* @throws \Doctrine\DBAL\DBALException
* @return void
*/
public function throwException(\PDOException $e)
{
$error_class = substr((string)$e->getCode(), 0, 2);
switch ($error_class) {
// Connection Exception
case '08':
// pass, connection exceptions seem to be handled elsewhere?
break;
case '0A':
case '0a':
$exception = 'FeatureNotSupportedException';
break;
case '21':
$exception = 'CardinalityViolationException';
break;
case '22':
$exception = 'DataException';
break;
case '23':
$exception = 'IntegrityConstraintException';
break;
case '24':
$exception = 'InvalidCursorStateException';
break;
case '25':
$exception = 'InvalidTransationStateException';
break;
// ... how many of these do the drivers actual use?
}
if (isset($exception)) {
$exception = 'Doctrine\\DBAL\\Exception\\' . $exception;
throw new $exception($e->getMessage(), 0, $e);
}
// if all else fails, just throw a DBALException
throw new DBALException($e->getMessage(), 0, $e);
}
// ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment