Commit 2a3d6cbf authored by Benjamin Eberlei's avatar Benjamin Eberlei

Merge pull request #423 from deeky666/improve-driver-exception-conversion

Improve driver exception code conversion
parents cd4e7bbd af37d45b
...@@ -20,7 +20,6 @@ ...@@ -20,7 +20,6 @@
namespace Doctrine\DBAL\Driver\Mysqli; namespace Doctrine\DBAL\Driver\Mysqli;
use Doctrine\DBAL\Driver as DriverInterface; use Doctrine\DBAL\Driver as DriverInterface;
use Doctrine\DBAL\Driver\Mysqli\MysqliException;
use Doctrine\DBAL\DBALException; use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver\ExceptionConverterDriver; use Doctrine\DBAL\Driver\ExceptionConverterDriver;
...@@ -77,53 +76,77 @@ class Driver implements DriverInterface, ExceptionConverterDriver ...@@ -77,53 +76,77 @@ class Driver implements DriverInterface, ExceptionConverterDriver
/** /**
* {@inheritdoc} * {@inheritdoc}
*
* @link http://dev.mysql.com/doc/refman/5.7/en/error-messages-client.html
* @link http://dev.mysql.com/doc/refman/5.7/en/error-messages-server.html
*/ */
public function convertExceptionCode(\Exception $exception) public function convertExceptionCode(\Exception $exception)
{ {
if (strpos($exception->getMessage(), 'Table') === 0) { switch ($exception->getCode()) {
if (strpos($exception->getMessage(), 'doesn\'t exist') !== false) { case '1050':
return DBALException::ERROR_UNKNOWN_TABLE;
}
if (strpos($exception->getMessage(), 'already exists') !== false) {
return DBALException::ERROR_TABLE_ALREADY_EXISTS; return DBALException::ERROR_TABLE_ALREADY_EXISTS;
}
}
if (strpos($exception->getMessage(), 'Unknown column') === 0) {
return DBALException::ERROR_BAD_FIELD_NAME;
}
if (strpos($exception->getMessage(), 'Cannot delete or update a parent row: a foreign key constraint fails') !== false) { case '1051':
return DBALException::ERROR_FOREIGN_KEY_CONSTRAINT; case '1146':
} return DBALException::ERROR_UNKNOWN_TABLE;
if (strpos($exception->getMessage(), 'Duplicate entry') !== false) {
return DBALException::ERROR_DUPLICATE_KEY;
}
if (strpos($exception->getMessage(), 'Column not found: 1054 Unknown column') !== false) {
return DBALException::ERROR_BAD_FIELD_NAME;
}
if (strpos($exception->getMessage(), 'in field list is ambiguous') !== falsE) {
return DBALException::ERROR_NON_UNIQUE_FIELD_NAME;
}
if (strpos($exception->getMessage(), 'You have an error in your SQL syntax; check the manual') !== false) {
return DBALException::ERROR_SYNTAX;
}
if (strpos($exception->getMessage(), 'Access denied for user') !== false) {
return DBALException::ERROR_ACCESS_DENIED;
}
if (strpos($exception->getMessage(), 'getaddrinfo failed: Name or service not known') !== false) {
return DBALException::ERROR_ACCESS_DENIED;
}
if (strpos($exception->getMessage(), ' cannot be null')) { case '1216':
return DBALException::ERROR_NOT_NULL; case '1217':
case '1451':
case '1452':
return DBALException::ERROR_FOREIGN_KEY_CONSTRAINT;
case '1062':
case '1557':
case '1569':
case '1586':
return DBALException::ERROR_DUPLICATE_KEY;
case '1054':
case '1166':
case '1611':
return DBALException::ERROR_BAD_FIELD_NAME;
case '1052':
case '1060':
case '1110':
return DBALException::ERROR_NON_UNIQUE_FIELD_NAME;
case '1064':
case '1149':
case '1287':
case '1341':
case '1342':
case '1343':
case '1344':
case '1382':
case '1479':
case '1541':
case '1554':
case '1626':
return DBALException::ERROR_SYNTAX;
case '1044':
case '1045':
case '1046':
case '1049':
case '1095':
case '1142':
case '1143':
case '1227':
case '1370':
case '2002':
case '2005':
return DBALException::ERROR_ACCESS_DENIED;
case '1048':
case '1121':
case '1138':
case '1171':
case '1252':
case '1263':
case '1566':
return DBALException::ERROR_NOT_NULL;
} }
return 0; return 0;
......
...@@ -118,48 +118,84 @@ class Driver implements \Doctrine\DBAL\Driver, ExceptionConverterDriver ...@@ -118,48 +118,84 @@ class Driver implements \Doctrine\DBAL\Driver, ExceptionConverterDriver
/** /**
* {@inheritdoc} * {@inheritdoc}
*
* @link http://dev.mysql.com/doc/refman/5.7/en/error-messages-client.html
* @link http://dev.mysql.com/doc/refman/5.7/en/error-messages-server.html
*/ */
public function convertExceptionCode(\Exception $exception) public function convertExceptionCode(\Exception $exception)
{ {
switch ($exception->getCode()) { $errorCode = $exception->getCode();
case '42S02':
return DBALException::ERROR_UNKNOWN_TABLE;
case '42S01':
return DBALException::ERROR_TABLE_ALREADY_EXISTS;
default:
if (strpos($exception->getMessage(), 'Cannot delete or update a parent row: a foreign key constraint fails') !== false) {
return DBALException::ERROR_FOREIGN_KEY_CONSTRAINT;
}
if (strpos($exception->getMessage(), 'Duplicate entry') !== false) {
return DBALException::ERROR_DUPLICATE_KEY;
}
if (strpos($exception->getMessage(), 'Column not found: 1054 Unknown column') !== false) { // Use driver-specific error code instead of SQLSTATE for PDO exceptions if available.
return DBALException::ERROR_BAD_FIELD_NAME; if ($exception instanceof \PDOException && null !== $exception->errorInfo[1]) {
} $errorCode = $exception->errorInfo[1];
}
if (strpos($exception->getMessage(), 'in field list is ambiguous') !== falsE) {
return DBALException::ERROR_NON_UNIQUE_FIELD_NAME;
}
if (strpos($exception->getMessage(), 'You have an error in your SQL syntax; check the manual') !== false) {
return DBALException::ERROR_SYNTAX;
}
if (strpos($exception->getMessage(), 'Access denied for user') !== false) { switch ($errorCode) {
return DBALException::ERROR_ACCESS_DENIED; case '1050':
} return DBALException::ERROR_TABLE_ALREADY_EXISTS;
if (strpos($exception->getMessage(), 'getaddrinfo failed: Name or service not known') !== false) { case '1051':
return DBALException::ERROR_ACCESS_DENIED; case '1146':
} return DBALException::ERROR_UNKNOWN_TABLE;
if (strpos($exception->getMessage(), ' cannot be null')) { case '1216':
return DBALException::ERROR_NOT_NULL; case '1217':
} case '1451':
case '1452':
return DBALException::ERROR_FOREIGN_KEY_CONSTRAINT;
case '1062':
case '1557':
case '1569':
case '1586':
return DBALException::ERROR_DUPLICATE_KEY;
case '1054':
case '1166':
case '1611':
return DBALException::ERROR_BAD_FIELD_NAME;
case '1052':
case '1060':
case '1110':
return DBALException::ERROR_NON_UNIQUE_FIELD_NAME;
case '1064':
case '1149':
case '1287':
case '1341':
case '1342':
case '1343':
case '1344':
case '1382':
case '1479':
case '1541':
case '1554':
case '1626':
return DBALException::ERROR_SYNTAX;
case '1044':
case '1045':
case '1046':
case '1049':
case '1095':
case '1142':
case '1143':
case '1227':
case '1370':
case '2002':
case '2005':
return DBALException::ERROR_ACCESS_DENIED;
case '1048':
case '1121':
case '1138':
case '1171':
case '1252':
case '1263':
case '1566':
return DBALException::ERROR_NOT_NULL;
} }
return 0; return 0;
......
...@@ -116,54 +116,46 @@ class Driver implements \Doctrine\DBAL\Driver, ExceptionConverterDriver ...@@ -116,54 +116,46 @@ class Driver implements \Doctrine\DBAL\Driver, ExceptionConverterDriver
/** /**
* {@inheritdoc} * {@inheritdoc}
*
* @link http://www.postgresql.org/docs/9.3/static/errcodes-appendix.html
*/ */
public function convertExceptionCode(\Exception $exception) public function convertExceptionCode(\Exception $exception)
{ {
if (strpos($exception->getMessage(), 'duplicate key value violates unique constraint') !== false) { switch ($exception->getCode()) {
return DBALException::ERROR_DUPLICATE_KEY; case '23502':
} return DBALException::ERROR_NOT_NULL;
if ($exception->getCode() === "42P01") {
return DBALException::ERROR_UNKNOWN_TABLE;
}
if ($exception->getCode() === "42P07") { case '23503':
return DBALException::ERROR_TABLE_ALREADY_EXISTS; return DBALException::ERROR_FOREIGN_KEY_CONSTRAINT;
}
if ($exception->getCode() === "23503") {
return DBALException::ERROR_FOREIGN_KEY_CONSTRAINT;
}
if ($exception->getCode() === "23502") { case '23505':
return DBALException::ERROR_NOT_NULL; return DBALException::ERROR_DUPLICATE_KEY;
}
if ($exception->getCode() === "42703") { case '42601':
return DBALException::ERROR_BAD_FIELD_NAME; return DBALException::ERROR_SYNTAX;
}
if ($exception->getCode() === "42702") { case '42702':
return DBALException::ERROR_NON_UNIQUE_FIELD_NAME; return DBALException::ERROR_NON_UNIQUE_FIELD_NAME;
}
if ($exception->getCode() === "42601") { case '42703':
return DBALException::ERROR_SYNTAX; return DBALException::ERROR_BAD_FIELD_NAME;
}
if (stripos($exception->getMessage(), 'password authentication failed for user') !== false) { case '42P01':
return DBALException::ERROR_ACCESS_DENIED; return DBALException::ERROR_UNKNOWN_TABLE;
}
if (stripos($exception->getMessage(), 'Name or service not known') !== false) { case '42P07':
return DBALException::ERROR_ACCESS_DENIED; return DBALException::ERROR_TABLE_ALREADY_EXISTS;
}
if (stripos($exception->getMessage(), 'does not exist') !== false) { case '7':
return DBALException::ERROR_ACCESS_DENIED; // In some case (mainly connection errors) the PDO exception does not provide a SQLSTATE via its code.
// The exception code is always set to 7 here.
// We have to match against the SQLSTATE in the error message in these cases.
if (strpos($exception->getMessage(), 'SQLSTATE[08006]') !== false) {
return DBALException::ERROR_ACCESS_DENIED;
}
break;
} }
return 0; return 0;
} }
} }
...@@ -123,6 +123,8 @@ class Driver implements \Doctrine\DBAL\Driver, ExceptionConverterDriver ...@@ -123,6 +123,8 @@ class Driver implements \Doctrine\DBAL\Driver, ExceptionConverterDriver
/** /**
* {@inheritdoc} * {@inheritdoc}
*
* @link http://www.sqlite.org/c3ref/c_abort.html
*/ */
public function convertExceptionCode(\Exception $exception) public function convertExceptionCode(\Exception $exception)
{ {
......
...@@ -74,6 +74,8 @@ class Driver implements \Doctrine\DBAL\Driver, ExceptionConverterDriver ...@@ -74,6 +74,8 @@ class Driver implements \Doctrine\DBAL\Driver, ExceptionConverterDriver
/** /**
* {@inheritdoc} * {@inheritdoc}
*
* @link http://dcx.sybase.com/index.html#sa160/en/saerrors/sqlerror.html
*/ */
public function convertExceptionCode(\Exception $exception) public function convertExceptionCode(\Exception $exception)
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment