Commit bea7cf84 by Alexander Makarov

Moved exception handling to schema

parent 5cb24302
......@@ -284,7 +284,7 @@ class Command extends \yii\base\Component
return $n;
} catch (\Exception $e) {
Yii::endProfile($token, __METHOD__);
$this->handleException($e, $rawSql);
$this->db->getSchema()->handleException($e, $rawSql);
}
}
......@@ -417,7 +417,7 @@ class Command extends \yii\base\Component
return $result;
} catch (\Exception $e) {
Yii::endProfile($token, 'yii\db\Command::query');
$this->handleException($e, $rawSql);
$this->db->getSchema()->handleException($e, $rawSql);
}
}
......@@ -767,31 +767,4 @@ class Command extends \yii\base\Component
return $this->setSql($sql);
}
/**
* Handles database error
*
* @param \Exception $e
* @param string $rawSql SQL that produced exception
* @throws Exception
*/
protected function handleException(\Exception $e, $rawSql)
{
if ($e instanceof Exception) {
throw $e;
} else {
$exceptionClass = '\yii\db\Exception';
$schema = $this->db->getSchema();
$exceptionMap = $schema->exceptionMap;
foreach ($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);
}
}
}
......@@ -478,4 +478,29 @@ abstract class Schema extends Object
return 'string';
}
}
/**
* Handles database error
*
* @param \Exception $e
* @param string $rawSql SQL that produced exception
* @throws Exception
*/
protected function handleException(\Exception $e, $rawSql)
{
if ($e instanceof Exception) {
throw $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";
$errorInfo = $e instanceof \PDOException ? $e->errorInfo : null;
throw 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