mirror of
				https://github.com/yiisoft/yii2.git
				synced 2025-11-04 06:37:55 +08:00 
			
		
		
		
	Merge pull request #13217 from yiisoft/throwable
Catch `\Throwable` in critical places
This commit is contained in:
		@ -439,6 +439,9 @@ class ActiveRecord extends BaseActiveRecord
 | 
			
		||||
        } catch (\Exception $e) {
 | 
			
		||||
            $transaction->rollBack();
 | 
			
		||||
            throw $e;
 | 
			
		||||
        } catch (\Throwable $e) {
 | 
			
		||||
            $transaction->rollBack();
 | 
			
		||||
            throw $e;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@ -545,6 +548,9 @@ class ActiveRecord extends BaseActiveRecord
 | 
			
		||||
        } catch (\Exception $e) {
 | 
			
		||||
            $transaction->rollBack();
 | 
			
		||||
            throw $e;
 | 
			
		||||
        } catch (\Throwable $e) {
 | 
			
		||||
            $transaction->rollBack();
 | 
			
		||||
            throw $e;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@ -585,6 +591,9 @@ class ActiveRecord extends BaseActiveRecord
 | 
			
		||||
        } catch (\Exception $e) {
 | 
			
		||||
            $transaction->rollBack();
 | 
			
		||||
            throw $e;
 | 
			
		||||
        } catch (\Throwable $e) {
 | 
			
		||||
            $transaction->rollBack();
 | 
			
		||||
            throw $e;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -420,7 +420,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 if there is any exception during query
 | 
			
		||||
     * @throws \Exception|\Throwable if there is any exception during query
 | 
			
		||||
     * @see enableQueryCache
 | 
			
		||||
     * @see queryCache
 | 
			
		||||
     * @see noCache()
 | 
			
		||||
@ -435,6 +435,9 @@ class Connection extends Component
 | 
			
		||||
        } catch (\Exception $e) {
 | 
			
		||||
            array_pop($this->_queryCacheInfo);
 | 
			
		||||
            throw $e;
 | 
			
		||||
        } catch (\Throwable $e) {
 | 
			
		||||
            array_pop($this->_queryCacheInfo);
 | 
			
		||||
            throw $e;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@ -457,7 +460,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 if there is any exception during query
 | 
			
		||||
     * @throws \Exception|\Throwable if there is any exception during query
 | 
			
		||||
     * @see enableQueryCache
 | 
			
		||||
     * @see queryCache
 | 
			
		||||
     * @see cache()
 | 
			
		||||
@ -472,6 +475,9 @@ class Connection extends Component
 | 
			
		||||
        } catch (\Exception $e) {
 | 
			
		||||
            array_pop($this->_queryCacheInfo);
 | 
			
		||||
            throw $e;
 | 
			
		||||
        } catch (\Throwable $e) {
 | 
			
		||||
            array_pop($this->_queryCacheInfo);
 | 
			
		||||
            throw $e;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@ -671,7 +677,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
 | 
			
		||||
     * @throws \Exception|\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)
 | 
			
		||||
@ -689,6 +695,11 @@ class Connection extends Component
 | 
			
		||||
                $transaction->rollBack();
 | 
			
		||||
            }
 | 
			
		||||
            throw $e;
 | 
			
		||||
        } catch (\Throwable $e) {
 | 
			
		||||
            if ($transaction->isActive && $transaction->level === $level) {
 | 
			
		||||
                $transaction->rollBack();
 | 
			
		||||
            }
 | 
			
		||||
            throw $e;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return $result;
 | 
			
		||||
 | 
			
		||||
@ -95,15 +95,16 @@ class Migration extends Component implements MigrationInterface
 | 
			
		||||
        try {
 | 
			
		||||
            if ($this->safeUp() === false) {
 | 
			
		||||
                $transaction->rollBack();
 | 
			
		||||
 | 
			
		||||
                return false;
 | 
			
		||||
            }
 | 
			
		||||
            $transaction->commit();
 | 
			
		||||
        } catch (\Exception $e) {
 | 
			
		||||
            echo 'Exception: ' . $e->getMessage() . ' (' . $e->getFile() . ':' . $e->getLine() . ")\n";
 | 
			
		||||
            echo $e->getTraceAsString() . "\n";
 | 
			
		||||
            $this->printException($e);
 | 
			
		||||
            $transaction->rollBack();
 | 
			
		||||
            return false;
 | 
			
		||||
        } catch (\Throwable $e) {
 | 
			
		||||
            $this->printException($e);
 | 
			
		||||
            $transaction->rollBack();
 | 
			
		||||
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@ -123,21 +124,31 @@ class Migration extends Component implements MigrationInterface
 | 
			
		||||
        try {
 | 
			
		||||
            if ($this->safeDown() === false) {
 | 
			
		||||
                $transaction->rollBack();
 | 
			
		||||
 | 
			
		||||
                return false;
 | 
			
		||||
            }
 | 
			
		||||
            $transaction->commit();
 | 
			
		||||
        } catch (\Exception $e) {
 | 
			
		||||
            echo 'Exception: ' . $e->getMessage() . ' (' . $e->getFile() . ':' . $e->getLine() . ")\n";
 | 
			
		||||
            echo $e->getTraceAsString() . "\n";
 | 
			
		||||
            $this->printException($e);
 | 
			
		||||
            $transaction->rollBack();
 | 
			
		||||
            return false;
 | 
			
		||||
        } catch (\Throwable $e) {
 | 
			
		||||
            $this->printException($e);
 | 
			
		||||
            $transaction->rollBack();
 | 
			
		||||
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return null;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @param \Throwable|\Exception $e
 | 
			
		||||
     */
 | 
			
		||||
    private function printException($e)
 | 
			
		||||
    {
 | 
			
		||||
        echo 'Exception: ' . $e->getMessage() . ' (' . $e->getFile() . ':' . $e->getLine() . ")\n";
 | 
			
		||||
        echo $e->getTraceAsString() . "\n";
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * This method contains the logic to be executed when applying this migration.
 | 
			
		||||
     * This method differs from [[up()]] in that the DB logic implemented here will
 | 
			
		||||
 | 
			
		||||
@ -28,9 +28,16 @@ use yii\base\InvalidConfigException;
 | 
			
		||||
 * } 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
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user