1
|
|
|
<?php |
2
|
|
|
namespace Kir\MySQL\Databases\MySQL; |
3
|
|
|
|
4
|
|
|
use Kir\MySQL\Exceptions\DatabaseHasGoneAwayException; |
5
|
|
|
use Kir\MySQL\Exceptions\IntegrityConstraintViolationException; |
6
|
|
|
use Kir\MySQL\Exceptions\SqlException; |
7
|
|
|
use PDOException; |
8
|
|
|
use Kir\MySQL\Exceptions\SqlDeadLockException; |
9
|
|
|
use Kir\MySQL\Exceptions\DuplicateUniqueKeyException; |
10
|
|
|
use Kir\MySQL\Exceptions\LockWaitTimeoutExceededException; |
11
|
|
|
|
12
|
|
|
class MySQLExceptionInterpreter { |
13
|
|
|
/** |
14
|
|
|
* @param PDOException $exception |
15
|
|
|
* @throw PDOException |
16
|
|
|
*/ |
17
|
|
|
public function throwMoreConcreteException(PDOException $exception) { |
18
|
|
|
$errorInfo = $exception->errorInfo; |
19
|
|
|
$code = is_array($errorInfo) && isset($errorInfo[1]) ? ((int) $errorInfo[1]) : ((int) $exception->getCode()); |
20
|
|
|
$message = (string) $exception->getMessage(); |
21
|
|
|
switch($code) { |
22
|
|
|
case 2006: throw new DatabaseHasGoneAwayException($message, $code, $exception); |
23
|
|
|
case 1213: throw new SqlDeadLockException($message, $code, $exception); |
24
|
|
|
case 1205: throw new LockWaitTimeoutExceededException($message, $code, $exception); |
25
|
|
|
case 1022: |
26
|
|
|
case 1062: |
27
|
|
|
case 1169: |
28
|
|
|
case 1586: throw new DuplicateUniqueKeyException($message, $code, $exception); |
29
|
|
|
case 1216: |
30
|
|
|
case 1217: |
31
|
|
|
case 1452: throw new IntegrityConstraintViolationException($message, (int) $code, $exception); |
|
|
|
|
32
|
|
|
} |
33
|
|
|
/** @link http://php.net/manual/en/class.exception.php#Hcom115813 (cHao's comment) */ |
34
|
|
|
if(!is_string($message) || !is_int($code)) { |
35
|
|
|
throw new SqlException((string) $message, (int) $code, $exception); |
36
|
|
|
} |
37
|
|
|
throw $exception; |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.
}
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.