From f475affaeb4d79da4778601ca032ae9004cc22e8 Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Sat, 10 Jun 2017 00:59:12 +0300 Subject: [PATCH] Removed duplicate handling for \Throwable and \Exception --- framework/base/ErrorHandler.php | 7 ++----- framework/base/View.php | 7 ------- framework/db/ActiveRecord.php | 9 --------- framework/db/Connection.php | 20 ++++---------------- framework/db/Migration.php | 10 +--------- framework/db/Transaction.php | 7 ------- framework/log/SyslogTarget.php | 2 +- framework/log/Target.php | 2 +- 8 files changed, 9 insertions(+), 55 deletions(-) diff --git a/framework/base/ErrorHandler.php b/framework/base/ErrorHandler.php index 619aed7..813dad5 100644 --- a/framework/base/ErrorHandler.php +++ b/framework/base/ErrorHandler.php @@ -116,11 +116,8 @@ abstract class ErrorHandler extends Component } exit(1); } - } catch (\Exception $e) { - // an other exception could be thrown while displaying the exception - $this->handleFallbackExceptionMessage($e, $exception); } catch (\Throwable $e) { - // additional check for \Throwable introduced in PHP 7 + // additional check for \Throwable $this->handleFallbackExceptionMessage($e, $exception); } @@ -129,7 +126,7 @@ abstract class ErrorHandler extends Component /** * Handles exception thrown during exception processing in [[handleException()]]. - * @param \Exception|\Throwable $exception Exception that was thrown during main exception processing. + * @param \Throwable $exception Exception that was thrown during main exception processing. * @param \Exception $previousException Main exception processed in [[handleException()]]. * @since 2.0.11 */ diff --git a/framework/base/View.php b/framework/base/View.php index 3838f67..a21085e 100644 --- a/framework/base/View.php +++ b/framework/base/View.php @@ -329,13 +329,6 @@ class View extends Component try { require($_file_); return ob_get_clean(); - } catch (\Exception $e) { - while (ob_get_level() > $_obInitialLevel_) { - if (!@ob_end_clean()) { - ob_clean(); - } - } - throw $e; } catch (\Throwable $e) { while (ob_get_level() > $_obInitialLevel_) { if (!@ob_end_clean()) { diff --git a/framework/db/ActiveRecord.php b/framework/db/ActiveRecord.php index 611c87c..08375d5 100644 --- a/framework/db/ActiveRecord.php +++ b/framework/db/ActiveRecord.php @@ -470,9 +470,6 @@ class ActiveRecord extends BaseActiveRecord $transaction->commit(); } return $result; - } catch (\Exception $e) { - $transaction->rollBack(); - throw $e; } catch (\Throwable $e) { $transaction->rollBack(); throw $e; @@ -579,9 +576,6 @@ class ActiveRecord extends BaseActiveRecord $transaction->commit(); } return $result; - } catch (\Exception $e) { - $transaction->rollBack(); - throw $e; } catch (\Throwable $e) { $transaction->rollBack(); throw $e; @@ -622,9 +616,6 @@ class ActiveRecord extends BaseActiveRecord $transaction->commit(); } return $result; - } catch (\Exception $e) { - $transaction->rollBack(); - throw $e; } catch (\Throwable $e) { $transaction->rollBack(); throw $e; diff --git a/framework/db/Connection.php b/framework/db/Connection.php index c7ced72..5de9ff8 100644 --- a/framework/db/Connection.php +++ b/framework/db/Connection.php @@ -449,7 +449,7 @@ class Connection extends Component * Use 0 to indicate that the cached data will never expire. * @param \yii\caching\Dependency $dependency the cache dependency associated with the cached query results. * @return mixed the return result of the callable - * @throws \Exception|\Throwable if there is any exception during query + * @throws \Throwable if there is any exception during query * @see enableQueryCache * @see queryCache * @see noCache() @@ -461,9 +461,6 @@ class Connection extends Component $result = call_user_func($callable, $this); array_pop($this->_queryCacheInfo); return $result; - } catch (\Exception $e) { - array_pop($this->_queryCacheInfo); - throw $e; } catch (\Throwable $e) { array_pop($this->_queryCacheInfo); throw $e; @@ -489,7 +486,7 @@ class Connection extends Component * @param callable $callable a PHP callable that contains DB queries which should not use query cache. * The signature of the callable is `function (Connection $db)`. * @return mixed the return result of the callable - * @throws \Exception|\Throwable if there is any exception during query + * @throws \Throwable if there is any exception during query * @see enableQueryCache * @see queryCache * @see cache() @@ -501,9 +498,6 @@ class Connection extends Component $result = call_user_func($callable, $this); array_pop($this->_queryCacheInfo); return $result; - } catch (\Exception $e) { - array_pop($this->_queryCacheInfo); - throw $e; } catch (\Throwable $e) { array_pop($this->_queryCacheInfo); throw $e; @@ -715,7 +709,7 @@ class Connection extends Component * @param callable $callback a valid PHP callback that performs the job. Accepts connection instance as parameter. * @param string|null $isolationLevel The isolation level to use for this transaction. * See [[Transaction::begin()]] for details. - * @throws \Exception|\Throwable if there is any exception during query. In this case the transaction will be rolled back. + * @throws \Throwable if there is any exception during query. In this case the transaction will be rolled back. * @return mixed result of callback function */ public function transaction(callable $callback, $isolationLevel = null) @@ -728,9 +722,6 @@ class Connection extends Component if ($transaction->isActive && $transaction->level === $level) { $transaction->commit(); } - } catch (\Exception $e) { - $this->rollbackTransactionOnLevel($transaction, $level); - throw $e; } catch (\Throwable $e) { $this->rollbackTransactionOnLevel($transaction, $level); throw $e; @@ -981,7 +972,7 @@ class Connection extends Component * @param callable $callback a PHP callable to be executed by this method. Its signature is * `function (Connection $db)`. Its return value will be returned by this method. * @return mixed the return value of the callback - * @throws \Exception|\Throwable if there is any exception thrown from the callback + * @throws \Throwable if there is any exception thrown from the callback */ public function useMaster(callable $callback) { @@ -989,9 +980,6 @@ class Connection extends Component $this->enableSlaves = false; try { $result = call_user_func($callback, $this); - } catch (\Exception $e) { - $this->enableSlaves = true; - throw $e; } catch (\Throwable $e) { $this->enableSlaves = true; throw $e; diff --git a/framework/db/Migration.php b/framework/db/Migration.php index f8a060d..05dadca 100644 --- a/framework/db/Migration.php +++ b/framework/db/Migration.php @@ -109,10 +109,6 @@ class Migration extends Component implements MigrationInterface return false; } $transaction->commit(); - } catch (\Exception $e) { - $this->printException($e); - $transaction->rollBack(); - return false; } catch (\Throwable $e) { $this->printException($e); $transaction->rollBack(); @@ -138,10 +134,6 @@ class Migration extends Component implements MigrationInterface return false; } $transaction->commit(); - } catch (\Exception $e) { - $this->printException($e); - $transaction->rollBack(); - return false; } catch (\Throwable $e) { $this->printException($e); $transaction->rollBack(); @@ -152,7 +144,7 @@ class Migration extends Component implements MigrationInterface } /** - * @param \Throwable|\Exception $e + * @param \Throwable $e */ private function printException($e) { diff --git a/framework/db/Transaction.php b/framework/db/Transaction.php index 93558ba..3098260 100644 --- a/framework/db/Transaction.php +++ b/framework/db/Transaction.php @@ -25,19 +25,12 @@ use yii\base\InvalidConfigException; * $connection->createCommand($sql2)->execute(); * //.... other SQL executions * $transaction->commit(); - * } catch (\Exception $e) { - * $transaction->rollBack(); - * throw $e; * } catch (\Throwable $e) { * $transaction->rollBack(); * throw $e; * } * ``` * - * > Note: in the above code we have two catch-blocks for compatibility - * > with PHP 5.x and PHP 7.x. `\Exception` implements the [`\Throwable` interface](http://php.net/manual/en/class.throwable.php) - * > since PHP 7.0, so you can skip the part with `\Exception` if your app uses only PHP 7.0 and higher. - * * @property bool $isActive Whether this transaction is active. Only an active transaction can [[commit()]] or * [[rollBack()]]. This property is read-only. * @property string $isolationLevel The transaction isolation level to use for this transaction. This can be diff --git a/framework/log/SyslogTarget.php b/framework/log/SyslogTarget.php index 2c817b7..e5a5206 100644 --- a/framework/log/SyslogTarget.php +++ b/framework/log/SyslogTarget.php @@ -80,7 +80,7 @@ class SyslogTarget extends Target $level = Logger::getLevelName($level); if (!is_string($text)) { // exceptions may not be serializable if in the call stack somewhere is a Closure - if ($text instanceof \Throwable || $text instanceof \Exception) { + if ($text instanceof \Throwable) { $text = (string) $text; } else { $text = VarDumper::export($text); diff --git a/framework/log/Target.php b/framework/log/Target.php index 9133961..8f9a301 100644 --- a/framework/log/Target.php +++ b/framework/log/Target.php @@ -256,7 +256,7 @@ abstract class Target extends Component $level = Logger::getLevelName($level); if (!is_string($text)) { // exceptions may not be serializable if in the call stack somewhere is a Closure - if ($text instanceof \Throwable || $text instanceof \Exception) { + if ($text instanceof \Throwable) { $text = (string) $text; } else { $text = VarDumper::export($text);