doctrine /
dbal
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Doctrine\DBAL; |
||
| 6 | |||
| 7 | use Doctrine\DBAL\Driver\DriverException as DriverExceptionInterface; |
||
| 8 | use Doctrine\DBAL\Driver\ExceptionConverterDriver; |
||
| 9 | use Doctrine\DBAL\Exception\DriverException; |
||
| 10 | use Exception; |
||
| 11 | use Throwable; |
||
| 12 | use function array_map; |
||
| 13 | use function bin2hex; |
||
| 14 | use function implode; |
||
| 15 | use function is_resource; |
||
| 16 | use function is_string; |
||
| 17 | use function json_encode; |
||
| 18 | use function preg_replace; |
||
| 19 | use function sprintf; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @psalm-immutable |
||
| 23 | */ |
||
| 24 | class DBALException extends Exception |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * @param mixed[] $params |
||
| 28 | */ |
||
| 29 | 2280 | public static function driverExceptionDuringQuery(Driver $driver, Throwable $driverEx, string $sql, array $params = []) : self |
|
| 30 | { |
||
| 31 | $messageFormat = <<<'MESSAGE' |
||
| 32 | 2280 | An exception occurred while executing "%s"%s: |
|
| 33 | |||
| 34 | %s |
||
| 35 | MESSAGE; |
||
| 36 | |||
| 37 | 2280 | $message = sprintf( |
|
| 38 | 2280 | $messageFormat, |
|
| 39 | 2280 | $sql, |
|
| 40 | 2280 | $params !== [] ? sprintf(' with params %s', self::formatParameters($params)) : '', |
|
| 41 | 2280 | $driverEx->getMessage() |
|
| 42 | ); |
||
| 43 | |||
| 44 | 2280 | return static::wrapException($driver, $driverEx, $message); |
|
| 45 | } |
||
| 46 | |||
| 47 | 47 | public static function driverException(Driver $driver, Throwable $driverEx) : self |
|
| 48 | { |
||
| 49 | 47 | return static::wrapException($driver, $driverEx, sprintf('An exception occurred in driver with message: %s', $driverEx->getMessage())); |
|
| 50 | } |
||
| 51 | |||
| 52 | 2327 | private static function wrapException(Driver $driver, Throwable $driverEx, string $msg) : self |
|
| 53 | { |
||
| 54 | 2327 | if ($driverEx instanceof DriverException) { |
|
| 55 | 22 | return $driverEx; |
|
| 56 | } |
||
| 57 | |||
| 58 | 2305 | if ($driver instanceof ExceptionConverterDriver && $driverEx instanceof DriverExceptionInterface) { |
|
| 59 | 2013 | return $driver->convertException($msg, $driverEx); |
|
| 60 | } |
||
| 61 | |||
| 62 | 292 | return new self($msg, 0, $driverEx); |
|
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Returns a human-readable representation of an array of parameters. |
||
| 67 | * This properly handles binary data by returning a hex representation. |
||
| 68 | * |
||
| 69 | * @param array<mixed, mixed> $params |
||
| 70 | */ |
||
| 71 | 250 | private static function formatParameters(array $params) : string |
|
| 72 | { |
||
| 73 | return '[' . implode(', ', array_map(static function ($param) : string { |
||
| 74 | 250 | if (is_resource($param)) { |
|
| 75 | 22 | return (string) $param; |
|
| 76 | } |
||
| 77 | |||
| 78 | 228 | $json = @json_encode($param); |
|
| 79 | |||
| 80 | 228 | if (! is_string($json) || $json === 'null' && is_string($param)) { |
|
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 81 | // JSON encoding failed, this is not a UTF-8 string. |
||
| 82 | 22 | return sprintf('"%s"', preg_replace('/.{2}/', '\\x$0', bin2hex($param))); |
|
| 83 | } |
||
| 84 | |||
| 85 | 228 | return $json; |
|
| 86 | 250 | }, $params)) . ']'; |
|
| 87 | } |
||
| 88 | } |
||
| 89 |