Skip to content

Commit 3e76123

Browse files
author
rkr
committed
- Better exception handling
1 parent 9cfab52 commit 3e76123

File tree

4 files changed

+31
-22
lines changed

4 files changed

+31
-22
lines changed

src/Builder/QueryStatement.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,7 @@ public function getStatement() {
4545
public function execute(array $params = []) {
4646
return $this->exceptionHandler(function () use ($params) {
4747
$timer = microtime(true);
48-
try {
49-
$response = $this->statement->execute($params);
50-
} catch (PDOException $e) {
51-
/** @link http://php.net/manual/en/class.exception.php#Hcom115813 (cHao's comment) */
52-
throw new PDOException($e->getMessage(), (int) $e->getCode());
53-
}
48+
$response = $this->statement->execute($params);
5449
$this->queryLoggers->log($this->query, microtime(true) - $timer);
5550
return $response;
5651
});
@@ -125,7 +120,6 @@ private function exceptionHandler($fn) {
125120
return call_user_func($fn);
126121
} catch (PDOException $e) {
127122
$this->exceptionInterpreter->throwMoreConcreteException($e);
128-
throw $e;
129123
}
130124
}
131125
}

src/Databases/MySQL.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,18 +88,15 @@ public function prepare($query) {
8888
* @return int
8989
*/
9090
public function exec($query, array $params = array()) {
91-
try {
91+
$this->exceptionHandler(function () use ($query, $params) {
9292
$stmt = $this->pdo->prepare($query);
9393
$timer = microtime(true);
9494
$stmt->execute($params);
9595
$this->queryLoggers->log($query, microtime(true) - $timer);
9696
$result = $stmt->rowCount();
9797
$stmt->closeCursor();
9898
return $result;
99-
} catch (PDOException $e) {
100-
$this->exceptionInterpreter->throwMoreConcreteException($e);
101-
throw $e;
102-
}
99+
});
103100
}
104101

105102
/**
@@ -158,8 +155,6 @@ public function quote($value) {
158155
$result = sprintf('(%s)', (string) $value);
159156
} elseif(is_array($value)) {
160157
$result = join(', ', array_map(function ($value) { return $this->quote($value); }, $value));
161-
/*} elseif(is_int(trim($value)) && strpos('123456789', substr(0, 1, trim($value))) !== null) {
162-
$result = $value;*/
163158
} else {
164159
$result = $this->pdo->quote($value);
165160
}
@@ -321,4 +316,16 @@ private function buildQueryStatement($query, $fn) {
321316
$stmtWrapper = new QueryStatement($stmt, $query, $this->exceptionInterpreter, $this->queryLoggers);
322317
return $stmtWrapper;
323318
}
319+
320+
/**
321+
* @param callable $fn
322+
* @return mixed
323+
*/
324+
private function exceptionHandler($fn) {
325+
try {
326+
return call_user_func($fn);
327+
} catch (PDOException $e) {
328+
$this->exceptionInterpreter->throwMoreConcreteException($e);
329+
}
330+
}
324331
}

src/Databases/MySQL/MySQLExceptionInterpreter.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22
namespace Kir\MySQL\Databases\MySQL;
33

4+
use Kir\MySQL\Exceptions\DatabaseHasGoneAwayException;
5+
use Kir\MySQL\Exceptions\SqlException;
46
use PDOException;
57
use Kir\MySQL\Exceptions\SqlDeadLockException;
68
use Kir\MySQL\Exceptions\DuplicateUniqueKeyException;
@@ -12,16 +14,17 @@ class MySQLExceptionInterpreter {
1214
* @throw PDOException
1315
*/
1416
public function throwMoreConcreteException(PDOException $exception) {
15-
$code = (int) $exception->errorInfo[1];
17+
$code = $exception->errorInfo[1];
1618
$message = (string) $exception->errorInfo[2];
17-
if($code === 1213) {
18-
throw new SqlDeadLockException($message, $code, $exception);
19+
switch($code) {
20+
case 2006: throw new DatabaseHasGoneAwayException($message, $code, $exception);
21+
case 1213: throw new SqlDeadLockException($message, $code, $exception);
22+
case 1205: throw new LockWaitTimeoutExceededException($message, $code, $exception);
23+
case 1062: throw new DuplicateUniqueKeyException($message, $code, $exception);
1924
}
20-
if($code === 1205) {
21-
throw new LockWaitTimeoutExceededException($message, $code, $exception);
22-
}
23-
if($code === 1062) {
24-
throw new DuplicateUniqueKeyException($message, $code, $exception);
25+
/** @link http://php.net/manual/en/class.exception.php#Hcom115813 (cHao's comment) */
26+
if(!is_string($message) || !is_int($code)) {
27+
throw new SqlException((string) $message, (int) $code, $exception);
2528
}
2629
throw $exception;
2730
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
namespace Kir\MySQL\Exceptions;
3+
4+
class DatabaseHasGoneAwayException extends SqlException {
5+
}

0 commit comments

Comments
 (0)