Commit 6e6005b1 by Qiang Xue

Refactored code.

parent ae1ed5bd
...@@ -502,20 +502,20 @@ class Schema extends Object ...@@ -502,20 +502,20 @@ class Schema extends Object
} }
/** /**
* Handles database error * Converts a DB exception to a more concrete one if possible.
* *
* @param \Exception $e * @param \Exception $e
* @param string $rawSql SQL that produced exception * @param string $rawSql SQL that produced exception
* @throws Exception * @return Exception
*/ */
public function handleException(\Exception $e, $rawSql) public function convertException(\Exception $e, $rawSql)
{ {
if ($e instanceof Exception) { if ($e instanceof Exception) {
throw $e; return $e;
} else { } else {
$message = $e->getMessage() . "\nThe SQL being executed was: $rawSql"; $message = $e->getMessage() . "\nThe SQL being executed was: $rawSql";
$errorInfo = $e instanceof \PDOException ? $e->errorInfo : null; $errorInfo = $e instanceof \PDOException ? $e->errorInfo : null;
throw new Exception($message, $errorInfo, (int) $e->getCode(), $e); return new Exception($message, $errorInfo, (int) $e->getCode(), $e);
} }
} }
......
...@@ -321,7 +321,7 @@ class Command extends \yii\base\Component ...@@ -321,7 +321,7 @@ class Command extends \yii\base\Component
return $n; return $n;
} catch (\Exception $e) { } catch (\Exception $e) {
Yii::endProfile($token, __METHOD__); Yii::endProfile($token, __METHOD__);
$this->db->getSchema()->handleException($e, $rawSql); throw $this->db->getSchema()->convertException($e, $rawSql);
} }
} }
...@@ -456,7 +456,7 @@ class Command extends \yii\base\Component ...@@ -456,7 +456,7 @@ class Command extends \yii\base\Component
return $result; return $result;
} catch (\Exception $e) { } catch (\Exception $e) {
Yii::endProfile($token, 'yii\db\Command::query'); Yii::endProfile($token, 'yii\db\Command::query');
$this->db->getSchema()->handleException($e, $rawSql); throw $this->db->getSchema()->convertException($e, $rawSql);
} }
} }
......
...@@ -432,7 +432,7 @@ class Connection extends Component ...@@ -432,7 +432,7 @@ class Connection extends Component
} }
$token = 'Opening DB connection: ' . $this->dsn; $token = 'Opening DB connection: ' . $this->dsn;
try { try {
Yii::trace($token, __METHOD__); Yii::info($token, __METHOD__);
Yii::beginProfile($token, __METHOD__); Yii::beginProfile($token, __METHOD__);
$this->pdo = $this->createPdoInstance(); $this->pdo = $this->createPdoInstance();
$this->initConnection(); $this->initConnection();
......
...@@ -496,28 +496,27 @@ abstract class Schema extends Object ...@@ -496,28 +496,27 @@ abstract class Schema extends Object
} }
/** /**
* Handles database error * Converts a DB exception to a more concrete one if possible.
* *
* @param \Exception $e * @param \Exception $e
* @param string $rawSql SQL that produced exception * @param string $rawSql SQL that produced exception
* @throws Exception * @return Exception
*/ */
public function handleException(\Exception $e, $rawSql) public function convertException(\Exception $e, $rawSql)
{ {
if ($e instanceof Exception) { if ($e instanceof Exception) {
throw $e; return $e;
} else { }
$exceptionClass = '\yii\db\Exception';
foreach ($this->exceptionMap as $error => $class) {
if (strpos($e->getMessage(), $error) !== false) {
$exceptionClass = $class;
}
}
$message = $e->getMessage() . "\nThe SQL being executed was: $rawSql"; $exceptionClass = '\yii\db\Exception';
$errorInfo = $e instanceof \PDOException ? $e->errorInfo : null; foreach ($this->exceptionMap as $error => $class) {
throw new $exceptionClass($message, $errorInfo, (int) $e->getCode(), $e); if (strpos($e->getMessage(), $error) !== false) {
$exceptionClass = $class;
}
} }
$message = $e->getMessage() . "\nThe SQL being executed was: $rawSql";
$errorInfo = $e instanceof \PDOException ? $e->errorInfo : null;
return new $exceptionClass($message, $errorInfo, (int) $e->getCode(), $e);
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment