Skip to content

Commit 46809de

Browse files
committed
Improve consistency of exception message formatting.
1 parent e46c09b commit 46809de

File tree

78 files changed

+487
-221
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+487
-221
lines changed

lib/Doctrine/DBAL/Cache/ArrayStatement.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use function array_values;
1616
use function count;
1717
use function reset;
18+
use function sprintf;
1819

1920
class ArrayStatement implements IteratorAggregate, ResultStatement
2021
{
@@ -48,7 +49,7 @@ public function __construct(array $data)
4849
*/
4950
public function closeCursor() : void
5051
{
51-
unset($this->data);
52+
$this->data = null;
5253
}
5354

5455
/**
@@ -77,7 +78,7 @@ public function rowCount() : int
7778
public function setFetchMode($fetchMode, ...$args) : void
7879
{
7980
if (count($args) > 0) {
80-
throw new InvalidArgumentException('Caching layer does not support 2nd/3rd argument to setFetchMode()');
81+
throw new InvalidArgumentException('Caching layer does not support 2nd/3rd argument to setFetchMode().');
8182
}
8283

8384
$this->defaultFetchMode = $fetchMode;
@@ -121,7 +122,9 @@ public function fetch($fetchMode = null, ...$args)
121122
return reset($row);
122123
}
123124

124-
throw new InvalidArgumentException('Invalid fetch-style given for fetching result.');
125+
throw new InvalidArgumentException(
126+
sprintf('Invalid fetch mode given for fetching result, %d given.', $fetchMode)
127+
);
125128
}
126129

127130
/**

lib/Doctrine/DBAL/DBALException.php

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,28 @@ class DBALException extends Exception
2828
*/
2929
public static function driverExceptionDuringQuery(Driver $driver, Throwable $driverEx, $sql, array $params = [])
3030
{
31-
$msg = "An exception occurred while executing '" . $sql . "'";
32-
if ($params) {
33-
$msg .= ' with params ' . self::formatParameters($params);
34-
}
35-
$msg .= ":\n\n" . $driverEx->getMessage();
31+
$messageFormat = <<<'MESSAGE'
32+
An exception occurred while executing "%s"%s:
33+
34+
%s
35+
MESSAGE;
36+
37+
$message = sprintf(
38+
$messageFormat,
39+
$sql,
40+
$params !== [] ? sprintf(' with params %s', self::formatParameters($params)) : '',
41+
$driverEx->getMessage()
42+
);
3643

37-
return static::wrapException($driver, $driverEx, $msg);
44+
return static::wrapException($driver, $driverEx, $message);
3845
}
3946

4047
/**
4148
* @return self
4249
*/
4350
public static function driverException(Driver $driver, Throwable $driverEx)
4451
{
45-
return static::wrapException($driver, $driverEx, 'An exception occurred in driver: ' . $driverEx->getMessage());
52+
return static::wrapException($driver, $driverEx, sprintf('An exception occurred in driver with message: %s', $driverEx->getMessage()));
4653
}
4754

4855
/**
@@ -64,11 +71,9 @@ private static function wrapException(Driver $driver, Throwable $driverEx, $msg)
6471
* Returns a human-readable representation of an array of parameters.
6572
* This properly handles binary data by returning a hex representation.
6673
*
67-
* @param mixed[] $params
68-
*
69-
* @return string
74+
* @param array<mixed, mixed> $params
7075
*/
71-
private static function formatParameters(array $params)
76+
private static function formatParameters(array $params) : string
7277
{
7378
return '[' . implode(', ', array_map(static function ($param) {
7479
if (is_resource($param)) {

lib/Doctrine/DBAL/Driver/Mysqli/MysqliStatement.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public function bindParam($column, &$variable, $type = ParameterType::STRING, $l
109109
assert(is_int($column));
110110

111111
if (! isset(self::$_paramTypeMap[$type])) {
112-
throw new MysqliException(sprintf("Unknown type: '%s'", $type));
112+
throw new MysqliException(sprintf('Unknown type, %d given.', $type));
113113
}
114114

115115
$this->_bindedValues[$column] =& $variable;
@@ -124,7 +124,7 @@ public function bindValue($param, $value, $type = ParameterType::STRING) : void
124124
assert(is_int($param));
125125

126126
if (! isset(self::$_paramTypeMap[$type])) {
127-
throw new MysqliException(sprintf("Unknown type: '%s'", $type));
127+
throw new MysqliException(sprintf('Unknown type, %d given.', $type));
128128
}
129129

130130
$this->_values[$param] = $value;
@@ -250,7 +250,7 @@ private function sendLongData($streams)
250250
$chunk = fread($stream, 8192);
251251

252252
if ($chunk === false) {
253-
throw new MysqliException("Failed reading the stream resource for parameter offset ${paramNr}.");
253+
throw new MysqliException(sprintf('Failed reading the stream resource for parameter offset %d.', $paramNr));
254254
}
255255

256256
if (! $this->_stmt->send_long_data($paramNr - 1, $chunk)) {
@@ -344,7 +344,7 @@ public function fetch($fetchMode = null, ...$args)
344344
return (object) $assoc;
345345

346346
default:
347-
throw new MysqliException(sprintf("Unknown fetch type '%s'", $fetchMode));
347+
throw new MysqliException(sprintf('Unknown fetch mode %d.', $fetchMode));
348348
}
349349
}
350350

lib/Doctrine/DBAL/Driver/OCI8/OCI8Statement.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public static function convertPositionalToNamedPlaceholders($statement)
156156

157157
if ($currentLiteralDelimiter) {
158158
throw new OCI8Exception(sprintf(
159-
'The statement contains non-terminated string literal starting at offset %d',
159+
'The statement contains non-terminated string literal starting at offset %d.',
160160
$tokenOffset - 1
161161
));
162162
}
@@ -407,7 +407,7 @@ public function fetch($fetchMode = null, ...$args)
407407
}
408408

409409
if (! isset(self::$fetchModeMap[$fetchMode])) {
410-
throw new InvalidArgumentException('Invalid fetch style: ' . $fetchMode);
410+
throw new InvalidArgumentException(sprintf('Invalid fetch mode %d.', $fetchMode));
411411
}
412412

413413
return oci_fetch_array(
@@ -434,7 +434,7 @@ public function fetchAll($fetchMode = null, ...$args)
434434
}
435435

436436
if (! isset(self::$fetchModeMap[$fetchMode])) {
437-
throw new InvalidArgumentException('Invalid fetch style: ' . $fetchMode);
437+
throw new InvalidArgumentException(sprintf('Invalid fetch mode %d.', $fetchMode));
438438
}
439439

440440
if (self::$fetchModeMap[$fetchMode] === OCI_BOTH) {

lib/Doctrine/DBAL/Driver/PDOStatement.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ private function convertParamType(int $type) : int
206206
if (! isset(self::PARAM_TYPE_MAP[$type])) {
207207
// TODO: next major: throw an exception
208208
@trigger_error(sprintf(
209-
'Using a PDO parameter type (%d given) is deprecated and will cause an error in Doctrine 3.0',
209+
'Using a PDO parameter type (%d given) is deprecated and will cause an error in Doctrine 3.0.',
210210
$type
211211
), E_USER_DEPRECATED);
212212

@@ -227,7 +227,7 @@ private function convertFetchMode(int $fetchMode) : int
227227
// TODO: next major: throw an exception
228228
@trigger_error(sprintf(
229229
'Using a PDO fetch mode or their combination (%d given)' .
230-
' is deprecated and will cause an error in Doctrine 3.0',
230+
' is deprecated and will cause an error in Doctrine 3.0.',
231231
$fetchMode
232232
), E_USER_DEPRECATED);
233233

lib/Doctrine/DBAL/Driver/SQLAnywhere/SQLAnywhereStatement.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Doctrine\DBAL\Driver\Statement;
88
use Doctrine\DBAL\Driver\StatementIterator;
9+
use Doctrine\DBAL\Exception\GetVariableType;
910
use Doctrine\DBAL\Exception\InvalidColumnIndex;
1011
use Doctrine\DBAL\FetchMode;
1112
use Doctrine\DBAL\ParameterType;
@@ -17,7 +18,6 @@
1718
use function array_key_exists;
1819
use function count;
1920
use function func_get_args;
20-
use function gettype;
2121
use function is_array;
2222
use function is_int;
2323
use function is_object;
@@ -73,7 +73,10 @@ class SQLAnywhereStatement implements IteratorAggregate, Statement
7373
public function __construct($conn, $sql)
7474
{
7575
if (! is_resource($conn)) {
76-
throw new SQLAnywhereException('Invalid SQL Anywhere connection resource: ' . $conn);
76+
throw new SQLAnywhereException(sprintf(
77+
'Invalid SQL Anywhere connection resource, "%s" given.',
78+
(new GetVariableType())->__invoke($conn)
79+
));
7780
}
7881

7982
$this->conn = $conn;
@@ -108,7 +111,7 @@ public function bindParam($column, &$variable, $type = ParameterType::STRING, $l
108111
break;
109112

110113
default:
111-
throw new SQLAnywhereException('Unknown type: ' . $type);
114+
throw new SQLAnywhereException(sprintf('Unknown type %d.', $type));
112115
}
113116

114117
$this->boundValues[$column] =& $variable;
@@ -215,7 +218,7 @@ public function fetch($fetchMode = null, ...$args)
215218
return sasql_fetch_object($this->result);
216219

217220
default:
218-
throw new SQLAnywhereException('Fetch mode is not supported: ' . $fetchMode);
221+
throw new SQLAnywhereException(sprintf('Fetch mode is not supported %d.', $fetchMode));
219222
}
220223
}
221224

@@ -314,8 +317,8 @@ private function castObject(stdClass $sourceObject, $destinationClass, array $ct
314317
if (! is_string($destinationClass)) {
315318
if (! is_object($destinationClass)) {
316319
throw new SQLAnywhereException(sprintf(
317-
'Destination class has to be of type string or object, %s given.',
318-
gettype($destinationClass)
320+
'Destination class has to be of type string or object, "%s" given.',
321+
(new GetVariableType())->__invoke($destinationClass)
319322
));
320323
}
321324
} else {

lib/Doctrine/DBAL/Driver/SQLSrv/Driver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class Driver extends AbstractSQLServerDriver
1717
public function connect(array $params, $username = null, $password = null, array $driverOptions = [])
1818
{
1919
if (! isset($params['host'])) {
20-
throw new SQLSrvException("Missing 'host' in configuration for sqlsrv driver.");
20+
throw new SQLSrvException('Missing "host" in configuration for sqlsrv driver.');
2121
}
2222

2323
$serverName = $params['host'];

lib/Doctrine/DBAL/Driver/SQLSrv/SQLSrvStatement.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ public function fetch($fetchMode = null, ...$args)
345345
return sqlsrv_fetch_object($this->stmt, $className, $ctorArgs) ?: false;
346346
}
347347

348-
throw new SQLSrvException('Fetch mode is not supported!');
348+
throw new SQLSrvException('Fetch mode is not supported.');
349349
}
350350

351351
/**

lib/Doctrine/DBAL/Exception/DriverRequired.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ public static function new(?string $url = null) : self
1717
if ($url !== null) {
1818
return new self(
1919
sprintf(
20-
"The options 'driver' or 'driverClass' are mandatory if a connection URL without scheme "
21-
. 'is given to DriverManager::getConnection(). Given URL: %s',
20+
'The options "driver" or "driverClass" are mandatory if a connection URL without scheme '
21+
. 'is given to DriverManager::getConnection(). Given URL "%s".',
2222
$url
2323
)
2424
);
2525
}
2626

2727
return new self(
28-
"The options 'driver' or 'driverClass' are mandatory if no PDO "
28+
'The options "driver" or "driverClass" are mandatory if no PDO '
2929
. 'instance is given to DriverManager::getConnection().'
3030
);
3131
}

lib/Doctrine/DBAL/Exception/EmptyCriteriaNotAllowed.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ final class EmptyCriteriaNotAllowed extends InvalidArgumentException
88
{
99
public static function new() : self
1010
{
11-
return new self('Empty criteria was used, expected non-empty criteria');
11+
return new self('Empty criteria was used, expected non-empty criteria.');
1212
}
1313
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\DBAL\Exception;
6+
7+
use function get_class;
8+
use function get_resource_type;
9+
use function gettype;
10+
use function is_bool;
11+
use function is_object;
12+
use function is_resource;
13+
14+
final class GetVariableType
15+
{
16+
/**
17+
* @param mixed $value
18+
*/
19+
public function __invoke($value) : string
20+
{
21+
if (is_object($value)) {
22+
return get_class($value);
23+
}
24+
25+
if (is_resource($value)) {
26+
return get_resource_type($value);
27+
}
28+
29+
if (is_bool($value)) {
30+
return $value === true ? 'true' : 'false';
31+
}
32+
33+
return gettype($value);
34+
}
35+
}

lib/Doctrine/DBAL/Exception/InvalidDriverClass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public static function new(string $driverClass) : self
1414
{
1515
return new self(
1616
sprintf(
17-
"The given 'driverClass' %s has to implement the %s interface.",
17+
'The given "driverClass" %s has to implement the %s interface.',
1818
$driverClass,
1919
Driver::class
2020
)

lib/Doctrine/DBAL/Exception/InvalidPdoInstance.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ final class InvalidPdoInstance extends DBALException
1010
{
1111
public static function new() : self
1212
{
13-
return new self("The 'pdo' option was used in DriverManager::getConnection() but no instance of PDO was given.");
13+
return new self('The "pdo" option was used in DriverManager::getConnection() but no instance of PDO was given.');
1414
}
1515
}

lib/Doctrine/DBAL/Exception/InvalidPlatformType.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Doctrine\DBAL\DBALException;
88
use Doctrine\DBAL\Platforms\AbstractPlatform;
99
use function get_class;
10-
use function gettype;
1110
use function is_object;
1211
use function sprintf;
1312

@@ -21,7 +20,7 @@ public static function new($invalidPlatform) : self
2120
if (is_object($invalidPlatform)) {
2221
return new self(
2322
sprintf(
24-
"Option 'platform' must be a subtype of '%s', instance of '%s' given",
23+
'Option "platform" must be a subtype of %s, instance of %s given.',
2524
AbstractPlatform::class,
2625
get_class($invalidPlatform)
2726
)
@@ -30,9 +29,9 @@ public static function new($invalidPlatform) : self
3029

3130
return new self(
3231
sprintf(
33-
"Option 'platform' must be an object and subtype of '%s'. Got '%s'",
32+
'Option "platform" must be an object and subtype of %s. Got "%s".',
3433
AbstractPlatform::class,
35-
gettype($invalidPlatform)
34+
(new GetVariableType())->__invoke($invalidPlatform)
3635
)
3736
);
3837
}

lib/Doctrine/DBAL/Exception/InvalidWrapperClass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public static function new(string $wrapperClass) : self
1414
{
1515
return new self(
1616
sprintf(
17-
"The given 'wrapperClass' %s has to be a subtype of %s.",
17+
'The given "wrapperClass" %s has to be a subtype of %s.',
1818
$wrapperClass,
1919
Connection::class
2020
)

lib/Doctrine/DBAL/Exception/MissingSQLParam.php renamed to lib/Doctrine/DBAL/Exception/MissingArrayParameter.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@
77
use Doctrine\DBAL\SQLParserUtilsException;
88
use function sprintf;
99

10-
final class MissingSQLParam extends SQLParserUtilsException
10+
final class MissingArrayParameter extends SQLParserUtilsException
1111
{
1212
public static function new(string $paramName) : self
1313
{
14-
return new self(
15-
sprintf('Value for :%1$s not found in params array. Params array key should be "%1$s"', $paramName)
16-
);
14+
return new self(sprintf('Parameter "%s" is missing.', $paramName));
1715
}
1816
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\DBAL\Exception;
6+
7+
use Doctrine\DBAL\SQLParserUtilsException;
8+
use function sprintf;
9+
10+
final class MissingArrayParameterType extends SQLParserUtilsException
11+
{
12+
public static function new(string $paramName) : self
13+
{
14+
return new self(sprintf('Type of array parameter "%s" is missing.', $paramName));
15+
}
16+
}

0 commit comments

Comments
 (0)