Commit 6e6005b1 by Qiang Xue

Refactored code.

parent ae1ed5bd
......@@ -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 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) {
throw $e;
return $e;
} else {
$message = $e->getMessage() . "\nThe SQL being executed was: $rawSql";
$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
return $n;
} catch (\Exception $e) {
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
return $result;
} catch (\Exception $e) {
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
}
$token = 'Opening DB connection: ' . $this->dsn;
try {
Yii::trace($token, __METHOD__);
Yii::info($token, __METHOD__);
Yii::beginProfile($token, __METHOD__);
$this->pdo = $this->createPdoInstance();
$this->initConnection();
......
......@@ -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 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) {
throw $e;
} else {
return $e;
}
$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";
$errorInfo = $e instanceof \PDOException ? $e->errorInfo : null;
throw new $exceptionClass($message, $errorInfo, (int) $e->getCode(), $e);
}
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