Skip to content

Commit 63f36a2

Browse files
committed
Improve consistency of exception message formatting.
1 parent e46c09b commit 63f36a2

File tree

72 files changed

+268
-175
lines changed

Some content is hidden

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

72 files changed

+268
-175
lines changed

lib/Doctrine/DBAL/Cache/ArrayStatement.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public function rowCount() : int
7777
public function setFetchMode($fetchMode, ...$args) : void
7878
{
7979
if (count($args) > 0) {
80-
throw new InvalidArgumentException('Caching layer does not support 2nd/3rd argument to setFetchMode()');
80+
throw new InvalidArgumentException('Caching layer does not support 2nd/3rd argument to setFetchMode().');
8181
}
8282

8383
$this->defaultFetchMode = $fetchMode;

lib/Doctrine/DBAL/DBALException.php

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,17 @@
88
use Doctrine\DBAL\Driver\ExceptionConverterDriver;
99
use Doctrine\DBAL\Exception\DriverException;
1010
use Exception;
11+
use InvalidArgumentException;
1112
use Throwable;
1213
use function array_map;
1314
use function bin2hex;
15+
use function get_class;
16+
use function get_resource_type;
17+
use function gettype;
1418
use function implode;
19+
use function is_array;
20+
use function is_bool;
21+
use function is_object;
1522
use function is_resource;
1623
use function is_string;
1724
use function json_encode;
@@ -28,21 +35,57 @@ class DBALException extends Exception
2835
*/
2936
public static function driverExceptionDuringQuery(Driver $driver, Throwable $driverEx, $sql, array $params = [])
3037
{
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();
38+
$messageFormat = <<<MESSAGE
39+
An exception occurred while executing "%s"%s:
40+
41+
%s.
42+
MESSAGE;
43+
44+
$message = sprintf(
45+
$messageFormat,
46+
$sql,
47+
$params !== [] ? sprintf(' with params %s', self::formatParameters($params)) : '',
48+
$driverEx->getMessage()
49+
);
3650

37-
return static::wrapException($driver, $driverEx, $msg);
51+
return static::wrapException($driver, $driverEx, $message);
3852
}
3953

4054
/**
4155
* @return self
4256
*/
4357
public static function driverException(Driver $driver, Throwable $driverEx)
4458
{
45-
return static::wrapException($driver, $driverEx, 'An exception occurred in driver: ' . $driverEx->getMessage());
59+
return static::wrapException($driver, $driverEx, sprintf('An exception occurred in driver with message: %s', $driverEx->getMessage()));
60+
}
61+
62+
public static function formatValue($value) : string
63+
{
64+
if ($value === null) {
65+
return 'NULL';
66+
}
67+
68+
if (is_object($value)) {
69+
return get_class($value);
70+
}
71+
72+
if (is_resource($value)) {
73+
return get_resource_type($value);
74+
}
75+
76+
if (is_bool($value)) {
77+
return $value === true ? 'true' : 'false';
78+
}
79+
80+
if (is_array($value)) {
81+
return self::formatParameters($value);
82+
}
83+
84+
if (is_string($value)) {
85+
return $value;
86+
}
87+
88+
throw new InvalidArgumentException(sprintf('Could not format value of type "%s".', gettype($value)));
4689
}
4790

4891
/**

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 "%s".', $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 "%s".', $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: 3 additions & 3 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

@@ -226,8 +226,8 @@ private function convertFetchMode(int $fetchMode) : int
226226
if (! isset(self::FETCH_MODE_MAP[$fetchMode])) {
227227
// TODO: next major: throw an exception
228228
@trigger_error(sprintf(
229-
'Using a PDO fetch mode or their combination (%d given)' .
230-
' is deprecated and will cause an error in Doctrine 3.0',
229+
'Using a PDO fetch mode or their combination ("%d" given)' .
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
@@ -4,6 +4,7 @@
44

55
namespace Doctrine\DBAL\Driver\SQLAnywhere;
66

7+
use Doctrine\DBAL\DBALException;
78
use Doctrine\DBAL\Driver\Statement;
89
use Doctrine\DBAL\Driver\StatementIterator;
910
use Doctrine\DBAL\Exception\InvalidColumnIndex;
@@ -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+
DBALException::formatValue($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+
DBALException::formatValue($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
}

lib/Doctrine/DBAL/Exception/InvalidColumnIndex.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ final class InvalidColumnIndex extends DBALException
1212
public static function new(int $index, int $count) : self
1313
{
1414
return new self(sprintf(
15-
'Invalid column index %d. The statement result contains %d column%s.',
15+
'Invalid column index "%d". The statement result contains "%d" column%s.',
1616
$index,
1717
$count,
1818
$count === 1 ? '' : 's'

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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public static function new($invalidPlatform) : self
2121
if (is_object($invalidPlatform)) {
2222
return new self(
2323
sprintf(
24-
"Option 'platform' must be a subtype of '%s', instance of '%s' given",
24+
'Option "platform" must be a subtype of "%s", instance of "%s" given.',
2525
AbstractPlatform::class,
2626
get_class($invalidPlatform)
2727
)
@@ -30,7 +30,7 @@ public static function new($invalidPlatform) : self
3030

3131
return new self(
3232
sprintf(
33-
"Option 'platform' must be an object and subtype of '%s'. Got '%s'",
33+
'Option "platform" must be an object and subtype of "%s". Got "%s".',
3434
AbstractPlatform::class,
3535
gettype($invalidPlatform)
3636
)

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/MissingSQLType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ final class MissingSQLType extends SQLParserUtilsException
1212
public static function new(string $typeName) : self
1313
{
1414
return new self(
15-
sprintf('Value for :%1$s not found in types array. Types array key should be "%1$s"', $typeName)
15+
sprintf('Value for :%1$s not found in types array. Types array key should be "%1$s"', $typeName) // what is this syntax?
1616
);
1717
}
1818
}

lib/Doctrine/DBAL/Exception/UnknownDriver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public static function new(string $unknownDriverName, array $knownDrivers) : sel
1717
{
1818
return new self(
1919
sprintf(
20-
"The given 'driver' %s is unknown, Doctrine currently supports only the following drivers: %s",
20+
'The given "driver" "%s" is unknown, Doctrine currently supports only the following drivers: %s',
2121
$unknownDriverName,
2222
implode(', ', $knownDrivers)
2323
)

lib/Doctrine/DBAL/Id/TableGenerator.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Throwable;
1313
use const CASE_LOWER;
1414
use function array_change_key_case;
15+
use function sprintf;
1516

1617
/**
1718
* Table ID Generator for those poor languages that are missing sequences.
@@ -126,7 +127,7 @@ public function nextValue($sequenceName)
126127
$rows = $this->conn->executeUpdate($sql, [$sequenceName, $row['sequence_value']]);
127128

128129
if ($rows !== 1) {
129-
throw new DBALException('Race-condition detected while updating sequence. Aborting generation');
130+
throw new DBALException('Race-condition detected while updating sequence. Aborting generation.');
130131
}
131132
} else {
132133
$this->conn->insert(
@@ -139,7 +140,7 @@ public function nextValue($sequenceName)
139140
$this->conn->commit();
140141
} catch (Throwable $e) {
141142
$this->conn->rollBack();
142-
throw new DBALException('Error occurred while generating ID with TableGenerator, aborted generation: ' . $e->getMessage(), 0, $e);
143+
throw new DBALException(sprintf('Error occurred while generating ID with TableGenerator, aborted generation with error: %s', $e->getMessage()), 0, $e);
143144
}
144145

145146
return $value;

0 commit comments

Comments
 (0)