Skip to content

Commit c36f826

Browse files
committed
Merge pull request doctrine#3536 from jwage/improve-exception-messages
Improve consistency of exception message formatting.
2 parents 9e5af55 + fb13d7d commit c36f826

File tree

88 files changed

+613
-248
lines changed

Some content is hidden

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

88 files changed

+613
-248
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)) {
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\Driver\Mysqli\Exception;
6+
7+
use Doctrine\DBAL\Driver\Mysqli\MysqliException;
8+
use mysqli;
9+
10+
final class ConnectionError extends MysqliException
11+
{
12+
public static function new(mysqli $connection) : self
13+
{
14+
return new self($connection->error, $connection->sqlstate ?: null, $connection->errno);
15+
}
16+
}
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\Driver\Mysqli\Exception;
6+
7+
use Doctrine\DBAL\Driver\Mysqli\MysqliException;
8+
use function sprintf;
9+
10+
final class FailedReadingStreamOffset extends MysqliException
11+
{
12+
public static function new(int $offset) : self
13+
{
14+
return new self(sprintf('Failed reading the stream resource for parameter offset %d.', $offset));
15+
}
16+
}
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\Driver\Mysqli\Exception;
6+
7+
use Doctrine\DBAL\Driver\Mysqli\MysqliException;
8+
use mysqli_stmt;
9+
10+
final class StatementError extends MysqliException
11+
{
12+
public static function new(mysqli_stmt $statement) : self
13+
{
14+
return new self($statement->error, $statement->sqlstate ?: null, $statement->errno);
15+
}
16+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\DBAL\Driver\Mysqli\Exception;
6+
7+
use Doctrine\DBAL\Driver\Mysqli\MysqliException;
8+
use function sprintf;
9+
10+
final class UnknownFetchMode extends MysqliException
11+
{
12+
/**
13+
* @param mixed $fetchMode
14+
*/
15+
public static function new($fetchMode) : self
16+
{
17+
return new self(sprintf('Unknown fetch mode %d.', $fetchMode));
18+
}
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\DBAL\Driver\Mysqli\Exception;
6+
7+
use Doctrine\DBAL\Driver\Mysqli\MysqliException;
8+
use function sprintf;
9+
10+
final class UnknownType extends MysqliException
11+
{
12+
/**
13+
* @param mixed $type
14+
*/
15+
public static function new($type) : self
16+
{
17+
return new self(sprintf('Unknown type, %d given.', $type));
18+
}
19+
}

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Doctrine\DBAL\Driver\Mysqli;
66

77
use Doctrine\DBAL\Driver\Connection;
8+
use Doctrine\DBAL\Driver\Mysqli\Exception\ConnectionError;
89
use Doctrine\DBAL\Driver\PingableConnection;
910
use Doctrine\DBAL\Driver\ResultStatement;
1011
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
@@ -73,7 +74,7 @@ public function __construct(array $params, $username, $password, array $driverOp
7374
});
7475
try {
7576
if (! $this->conn->real_connect($host, $username, $password, $dbname, $port, $socket, $flags)) {
76-
throw MysqliException::fromConnectionError($this->conn);
77+
throw ConnectionError::new($this->conn);
7778
}
7879
} finally {
7980
restore_error_handler();
@@ -161,7 +162,7 @@ public function quote(string $input) : string
161162
public function exec(string $statement) : int
162163
{
163164
if ($this->conn->query($statement) === false) {
164-
throw MysqliException::fromConnectionError($this->conn);
165+
throw ConnectionError::new($this->conn);
165166
}
166167

167168
return $this->conn->affected_rows;
@@ -189,7 +190,7 @@ public function beginTransaction() : void
189190
public function commit() : void
190191
{
191192
if (! $this->conn->commit()) {
192-
throw MysqliException::fromConnectionError($this->conn);
193+
throw ConnectionError::new($this->conn);
193194
}
194195
}
195196

@@ -199,7 +200,7 @@ public function commit() : void
199200
public function rollBack() : void
200201
{
201202
if (! $this->conn->rollback()) {
202-
throw MysqliException::fromConnectionError($this->conn);
203+
throw ConnectionError::new($this->conn);
203204
}
204205
}
205206

@@ -242,7 +243,7 @@ private function setDriverOptions(array $driverOptions = [])
242243
continue;
243244
}
244245

245-
throw MysqliException::fromConnectionError($this->conn);
246+
throw ConnectionError::new($this->conn);
246247
}
247248
}
248249

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

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,10 @@
55
namespace Doctrine\DBAL\Driver\Mysqli;
66

77
use Doctrine\DBAL\Driver\AbstractDriverException;
8-
use mysqli;
9-
use mysqli_stmt;
108

119
/**
1210
* Exception thrown in case the mysqli driver errors.
1311
*/
1412
class MysqliException extends AbstractDriverException
1513
{
16-
public static function fromConnectionError(mysqli $connection) : self
17-
{
18-
return new self($connection->error, $connection->sqlstate ?: null, $connection->errno);
19-
}
20-
21-
public static function fromStatementError(mysqli_stmt $statement) : self
22-
{
23-
return new self($statement->error, $statement->sqlstate ?: null, $statement->errno);
24-
}
2514
}

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

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
namespace Doctrine\DBAL\Driver\Mysqli;
66

77
use Doctrine\DBAL\Driver\DriverException;
8+
use Doctrine\DBAL\Driver\Mysqli\Exception\ConnectionError;
9+
use Doctrine\DBAL\Driver\Mysqli\Exception\FailedReadingStreamOffset;
10+
use Doctrine\DBAL\Driver\Mysqli\Exception\StatementError;
11+
use Doctrine\DBAL\Driver\Mysqli\Exception\UnknownFetchMode;
12+
use Doctrine\DBAL\Driver\Mysqli\Exception\UnknownType;
813
use Doctrine\DBAL\Driver\Statement;
914
use Doctrine\DBAL\Driver\StatementIterator;
1015
use Doctrine\DBAL\Exception\InvalidArgumentException;
@@ -25,7 +30,6 @@
2530
use function is_array;
2631
use function is_int;
2732
use function is_resource;
28-
use function sprintf;
2933
use function str_repeat;
3034

3135
class MysqliStatement implements IteratorAggregate, Statement
@@ -87,7 +91,7 @@ public function __construct(mysqli $conn, $prepareString)
8791
$stmt = $conn->prepare($prepareString);
8892

8993
if ($stmt === false) {
90-
throw MysqliException::fromConnectionError($this->_conn);
94+
throw ConnectionError::new($this->_conn);
9195
}
9296

9397
$this->_stmt = $stmt;
@@ -109,7 +113,7 @@ public function bindParam($column, &$variable, $type = ParameterType::STRING, $l
109113
assert(is_int($column));
110114

111115
if (! isset(self::$_paramTypeMap[$type])) {
112-
throw new MysqliException(sprintf("Unknown type: '%s'", $type));
116+
throw UnknownType::new($type);
113117
}
114118

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

126130
if (! isset(self::$_paramTypeMap[$type])) {
127-
throw new MysqliException(sprintf("Unknown type: '%s'", $type));
131+
throw UnknownType::new($type);
128132
}
129133

130134
$this->_values[$param] = $value;
@@ -139,14 +143,14 @@ public function execute($params = null) : void
139143
{
140144
if ($params !== null && count($params) > 0) {
141145
if (! $this->bindUntypedValues($params)) {
142-
throw MysqliException::fromStatementError($this->_stmt);
146+
throw StatementError::new($this->_stmt);
143147
}
144148
} else {
145149
$this->bindTypedParameters();
146150
}
147151

148152
if (! $this->_stmt->execute()) {
149-
throw MysqliException::fromStatementError($this->_stmt);
153+
throw StatementError::new($this->_stmt);
150154
}
151155

152156
if ($this->_columnNames === null) {
@@ -193,7 +197,7 @@ public function execute($params = null) : void
193197
}
194198

195199
if (! $this->_stmt->bind_result(...$refs)) {
196-
throw MysqliException::fromStatementError($this->_stmt);
200+
throw StatementError::new($this->_stmt);
197201
}
198202
}
199203

@@ -232,7 +236,7 @@ private function bindTypedParameters()
232236
}
233237

234238
if (count($values) > 0 && ! $this->_stmt->bind_param($types, ...$values)) {
235-
throw MysqliException::fromStatementError($this->_stmt);
239+
throw StatementError::new($this->_stmt);
236240
}
237241

238242
$this->sendLongData($streams);
@@ -250,11 +254,11 @@ private function sendLongData($streams)
250254
$chunk = fread($stream, 8192);
251255

252256
if ($chunk === false) {
253-
throw new MysqliException("Failed reading the stream resource for parameter offset ${paramNr}.");
257+
throw FailedReadingStreamOffset::new($paramNr);
254258
}
255259

256260
if (! $this->_stmt->send_long_data($paramNr - 1, $chunk)) {
257-
throw MysqliException::fromStatementError($this->_stmt);
261+
throw StatementError::new($this->_stmt);
258262
}
259263
}
260264
}
@@ -322,7 +326,7 @@ public function fetch($fetchMode = null, ...$args)
322326
}
323327

324328
if ($values === false) {
325-
throw MysqliException::fromStatementError($this->_stmt);
329+
throw StatementError::new($this->_stmt);
326330
}
327331

328332
if ($fetchMode === FetchMode::NUMERIC) {
@@ -344,7 +348,7 @@ public function fetch($fetchMode = null, ...$args)
344348
return (object) $assoc;
345349

346350
default:
347-
throw new MysqliException(sprintf("Unknown fetch type '%s'", $fetchMode));
351+
throw UnknownFetchMode::new($fetchMode);
348352
}
349353
}
350354

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

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

161161
if ($currentLiteralDelimiter) {
162162
throw new OCI8Exception(sprintf(
163-
'The statement contains non-terminated string literal starting at offset %d',
163+
'The statement contains non-terminated string literal starting at offset %d.',
164164
$tokenOffset - 1
165165
));
166166
}
@@ -411,7 +411,7 @@ public function fetch($fetchMode = null, ...$args)
411411
}
412412

413413
if (! isset(self::$fetchModeMap[$fetchMode])) {
414-
throw new InvalidArgumentException('Invalid fetch style: ' . $fetchMode);
414+
throw new InvalidArgumentException(sprintf('Invalid fetch mode %d.', $fetchMode));
415415
}
416416

417417
return oci_fetch_array(
@@ -438,7 +438,7 @@ public function fetchAll($fetchMode = null, ...$args)
438438
}
439439

440440
if (! isset(self::$fetchModeMap[$fetchMode])) {
441-
throw new InvalidArgumentException('Invalid fetch style: ' . $fetchMode);
441+
throw new InvalidArgumentException(sprintf('Invalid fetch mode %d.', $fetchMode));
442442
}
443443

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

0 commit comments

Comments
 (0)