Commit 47b0aec4 authored by Steve Müller's avatar Steve Müller

improve driver exception code conversion

parent 25a1cced
...@@ -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;
...@@ -80,49 +79,69 @@ class Driver implements DriverInterface, ExceptionConverterDriver ...@@ -80,49 +79,69 @@ class Driver implements DriverInterface, ExceptionConverterDriver
*/ */
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) { case '1051':
return DBALException::ERROR_BAD_FIELD_NAME; case '1146':
} return DBALException::ERROR_UNKNOWN_TABLE;
if (strpos($exception->getMessage(), 'Cannot delete or update a parent row: a foreign key constraint fails') !== false) { case '1216':
case '1217':
case '1451':
case '1452':
return DBALException::ERROR_FOREIGN_KEY_CONSTRAINT; return DBALException::ERROR_FOREIGN_KEY_CONSTRAINT;
}
if (strpos($exception->getMessage(), 'Duplicate entry') !== false) { case '1062':
case '1557':
case '1569':
case '1586':
return DBALException::ERROR_DUPLICATE_KEY; return DBALException::ERROR_DUPLICATE_KEY;
}
if (strpos($exception->getMessage(), 'Column not found: 1054 Unknown column') !== false) { case '1054':
case '1166':
case '1611':
return DBALException::ERROR_BAD_FIELD_NAME; return DBALException::ERROR_BAD_FIELD_NAME;
}
if (strpos($exception->getMessage(), 'in field list is ambiguous') !== falsE) { case '1052':
case '1060':
case '1110':
return DBALException::ERROR_NON_UNIQUE_FIELD_NAME; return DBALException::ERROR_NON_UNIQUE_FIELD_NAME;
}
if (strpos($exception->getMessage(), 'You have an error in your SQL syntax; check the manual') !== false) { 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; return DBALException::ERROR_SYNTAX;
}
if (strpos($exception->getMessage(), 'Access denied for user') !== false) { case '1044':
case '1045':
case '1046':
case '1049':
case '1095':
case '1142':
case '1143':
case '1227':
case '1370':
case '2005':
return DBALException::ERROR_ACCESS_DENIED; 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 '1048':
case '1121':
case '1138':
case '1171':
case '1252':
case '1263':
case '1566':
return DBALException::ERROR_NOT_NULL; return DBALException::ERROR_NOT_NULL;
} }
......
...@@ -121,46 +121,78 @@ class Driver implements \Doctrine\DBAL\Driver, ExceptionConverterDriver ...@@ -121,46 +121,78 @@ class Driver implements \Doctrine\DBAL\Driver, ExceptionConverterDriver
*/ */
public function convertExceptionCode(\Exception $exception) public function convertExceptionCode(\Exception $exception)
{ {
switch ($exception->getCode()) { $errorCode = $exception->getCode();
case '42S02':
return DBALException::ERROR_UNKNOWN_TABLE; // Use driver-specific error code instead of SQLSTATE for PDO exceptions if available.
if ($exception instanceof \PDOException && null !== $exception->errorInfo[1]) {
$errorCode = $exception->errorInfo[1];
}
case '42S01': switch ($errorCode) {
case '1050':
return DBALException::ERROR_TABLE_ALREADY_EXISTS; return DBALException::ERROR_TABLE_ALREADY_EXISTS;
default: case '1051':
if (strpos($exception->getMessage(), 'Cannot delete or update a parent row: a foreign key constraint fails') !== false) { case '1146':
return DBALException::ERROR_UNKNOWN_TABLE;
case '1216':
case '1217':
case '1451':
case '1452':
return DBALException::ERROR_FOREIGN_KEY_CONSTRAINT; return DBALException::ERROR_FOREIGN_KEY_CONSTRAINT;
}
if (strpos($exception->getMessage(), 'Duplicate entry') !== false) { case '1062':
case '1557':
case '1569':
case '1586':
return DBALException::ERROR_DUPLICATE_KEY; return DBALException::ERROR_DUPLICATE_KEY;
}
if (strpos($exception->getMessage(), 'Column not found: 1054 Unknown column') !== false) { case '1054':
case '1166':
case '1611':
return DBALException::ERROR_BAD_FIELD_NAME; return DBALException::ERROR_BAD_FIELD_NAME;
}
if (strpos($exception->getMessage(), 'in field list is ambiguous') !== falsE) { case '1052':
case '1060':
case '1110':
return DBALException::ERROR_NON_UNIQUE_FIELD_NAME; return DBALException::ERROR_NON_UNIQUE_FIELD_NAME;
}
if (strpos($exception->getMessage(), 'You have an error in your SQL syntax; check the manual') !== false) { 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; return DBALException::ERROR_SYNTAX;
}
if (strpos($exception->getMessage(), 'Access denied for user') !== false) { case '1044':
case '1045':
case '1046':
case '1049':
case '1095':
case '1142':
case '1143':
case '1227':
case '1370':
case '2005':
return DBALException::ERROR_ACCESS_DENIED; return DBALException::ERROR_ACCESS_DENIED;
}
if (strpos($exception->getMessage(), 'getaddrinfo failed: Name or service not known') !== false) { case '1048':
return DBALException::ERROR_ACCESS_DENIED; case '1121':
} case '1138':
case '1171':
if (strpos($exception->getMessage(), ' cannot be null')) { case '1252':
case '1263':
case '1566':
return DBALException::ERROR_NOT_NULL; return DBALException::ERROR_NOT_NULL;
} }
}
return 0; return 0;
} }
......
...@@ -112,51 +112,41 @@ class Driver implements \Doctrine\DBAL\Driver, ExceptionConverterDriver ...@@ -112,51 +112,41 @@ class Driver implements \Doctrine\DBAL\Driver, ExceptionConverterDriver
*/ */
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") {
return DBALException::ERROR_TABLE_ALREADY_EXISTS;
}
if ($exception->getCode() === "23503") { case '23503':
return DBALException::ERROR_FOREIGN_KEY_CONSTRAINT; 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':
// 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; return DBALException::ERROR_ACCESS_DENIED;
} }
break;
}
return 0; return 0;
} }
} }
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