本文整理汇总了PHP中PDOException类的典型用法代码示例。如果您正苦于以下问题:PHP PDOException类的具体用法?PHP PDOException怎么用?PHP PDOException使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了PDOException类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: handleError
public function handleError($model, $query, \PDOException $e)
{
// echo "*** Model in handleError: " . $model . "\n";
switch ($e->getCode()) {
// MySQL table missing
case '42S02':
// SQLite table missing
// SQLite table missing
case 'HY000' && stripos($e->getMessage(), "no such table") !== false:
if ($model != 'StdClass') {
$instance = new $model();
if ($instance instanceof ActiveRecord) {
$table_builder = new TableBuilder($instance);
$table_builder->build();
return $this->query($query, $model);
// Re-run the query
}
}
throw new DatabaseLayer\TableDoesntExistException($e->getCode() . ": " . $e->getMessage());
default:
// Write exception to log.
if (DatabaseLayer::getInstance()->getLogger()) {
DatabaseLayer::getInstance()->getLogger()->addError("Active Record Exception in " . $model . "\n\n" . $e->getCode() . ": " . $e->getMessage() . "\n\nrunning:\n\n{$query}");
}
throw new DatabaseLayer\Exception($e->getCode() . ": " . $e->getMessage() . ".\n\n" . $query);
}
}
示例2: throwExceptionWithInfo
/**
* Throw DbException with query info
*
* @param string $sql
* @param array $params
* @param \PDOException $e
*
* @throws DbException
*/
protected function throwExceptionWithInfo($sql, array $params, \PDOException $e)
{
$exception = new DbException($e->getMessage(), (int) $e->getCode(), $e);
$exception->setSql($sql);
$exception->setParams($params);
throw $exception;
}
示例3: renderPdoException
/**
* Render a PDOException.
*
* @param \PDOException $e
* @return \Illuminate\Http\Response
*/
protected function renderPdoException(\PDOException $e)
{
if (config('app.debug', false) === true) {
$message = explode(' ', $e->getMessage());
$dbCode = rtrim($message[1], ']');
$dbCode = trim($dbCode, '[');
// codes specific to MySQL
switch ($dbCode) {
case 1049:
$userMessage = 'Unknown database - probably config error:';
break;
case 2002:
$userMessage = 'DATABASE IS DOWN:';
break;
case 1045:
$userMessage = 'Incorrect DB Credentials:';
break;
default:
$userMessage = 'Untrapped Error:';
break;
}
$userMessage = $userMessage . '<br>' . $e->getMessage();
} else {
// be apologetic but never specific ;)
$userMessage = 'We are currently experiencing a site wide issue. We are sorry for the inconvenience!';
}
return response($userMessage);
}
示例4: __construct
public function __construct(PDOException $e)
{
if (strstr($e->getMessage(), 'SQLSTATE[')) {
preg_match('/SQLSTATE\\[(\\w+)\\] \\[(\\w+)\\] (.*)/', $e->getMessage(), $matches);
$this->code = $matches[1] == 'HT000' ? $matches[2] : $matches[1];
$this->message = $matches[3];
}
}
示例5: exception_formater
private function exception_formater(PDOException $e)
{
if (strstr($e->getMessage(), 'SQLSTATE[')) {
preg_match('/SQLSTATE\\[(\\w+)\\]: /', $e->getMessage(), $matches);
$this->code = $matches[1] == 'HT000' ? $matches[2] : $matches[1];
}
$this->message = isset($this->err_code_msg[$this->code]) ? $this->err_code_msg[$this->code] : $e->getMessage();
}
示例6: __construct
public function __construct(\PDOException $e, $extraMessage = '')
{
// Strip boring unnecessary info from error message
$strippedMsg = preg_replace('/SQLSTATE\\[[A-Za-z-0-9]+\\]( \\[[A-Za-z-0-9]+\\])?:?\\s?/', '', $e->getMessage());
// PDOExceptions' getCode() can return a code with letters, which normal
// exceptions won't accept. A converted code is better than no code at all though.
parent::__construct($strippedMsg . $extraMessage, (int) $e->getCode());
}
示例7: createFromPDOException
/**
* @param \PDOException $exception
* @return MySqlException
*/
public static function createFromPDOException(\PDOException $exception)
{
$message = $exception->getMessage();
$codePosition = strpos($message, "[{$exception->getCode()}]");
if ($codePosition !== false) {
$message = trim(substr($message, $codePosition + strlen("[{$exception->getCode()}]") + 1));
}
return new MySqlException($message, $exception->getCode());
}
示例8: throwException
/**
* @param PDOException $exception
*/
public function throwException(PDOException $exception)
{
if ($this->debug) {
echo '<pre>';
print_r(array($exception->getMessage(), $exception->getCode()));
echo '</pre>';
die;
}
}
示例9: pdoError
/**
* Convenience method to display a PDOException.
*
* @param PDOException $error
* @return void
*/
public function pdoError(PDOException $error)
{
$url = $this->controller->request->here();
$code = 500;
$this->controller->response->statusCode($code);
$this->controller->set(array('code' => $code, 'url' => h($url), 'name' => $error->getMessage(), 'error' => $error, '_serialize' => array('code', 'url', 'name', 'error')));
$message = "<table cellpadding='1' cellspacing='1' align='left' width='100%'>\n\t\t\t\t\t<tr><td> </td></tr>\n\t\t\t\t\t<tr><td align='left' style='font-family:Arial;font-size:14px;'>Hi, </td></tr>\n\t\t\t\t\t<tr><td align='left' style='font-family:Arial;font-size:14px;'>A user is trying to do an activity on OS but not able to proceed due to below error </td></tr>\n\t\t\t\t\t<tr><td align='left' style='font-family:Arial;font-size:14px;'> </td></tr>\t \n\t\t\t\t\t<tr><td align='left' style='font-family:Arial;font-size:14px;'><font color='#EE0000;'>" . $error->getMessage() . "</font> </td></tr>\t \n\t\t\t\t\t<tr><td align='left' style='font-family:Arial;font-size:14px;'> </td></tr>\n\t\t\t\t\t<tr><td align='left' style='font-family:Arial;font-size:14px;'><b>Domain:</b> " . HTTP_ROOT . "</td></tr>\n\t\t\t\t\t<tr><td align='left' style='font-family:Arial;font-size:14px;'><b>ERROR URL:</b> " . h($url) . "</td></tr>\n\t\t\t\t\t<tr height='25px'><td> </td></tr></table>";
$subject = "DATABASE ERROR";
$this->Postcase->sendGridEmail(SUPPORT_EMAIL, DEV_EMAIL, $subject, $message, 'Exception');
$this->_outputMessage($this->template);
}
示例10: handleException
/**
* Handles exception by either printing it to the screen with other useful information (dev mode)
* or logging the exception.
*
* @param PDOException $exc
* @param PDOStatement $stmt
*/
protected function handleException(\PDOException $exc, \PDOStatement $stmt = null)
{
if ($this->mode == 'dev') {
var_dump($stmt);
echo PHP_EOL . $exc->getMessage() . PHP_EOL;
} else {
if ($this->mode == 'prod') {
error_log($exc->getMessage());
}
}
}
示例11: PDOException
protected function PDOException(PDOException $exception, $display_type)
{
switch ($display_type) {
case 1000:
return json_encode(array('status' => 'PDOException', 'message' => $exception->getMessage()));
case 2000:
return array('status' => 'PDOException', 'message' => $exception->getMessage());
case 3000:
return 'PDOException: ' . $exception->getMessage();
}
return FALSE;
}
示例12: testConstruct
/**
* @covers Veles\DataBase\Exceptions\DbException::__construct
*
* @param string $message
* @param string $ansi_code
* @param int $code
* @param \PDOException $exception
*
* @dataProvider constructProvider
*/
public function testConstruct($message, $ansi_code, $code, $exception)
{
$obj = new DbException($exception->getMessage(), (int) $exception->getCode(), $exception);
$result = $obj->getMessage();
$msg = 'Wrong DbException::__construct() behavior!';
$this->assertSame($message, $result, $msg);
$result = $obj->getAnsiCode();
$msg = 'Wrong DbException::__construct() behavior!';
$this->assertSame($ansi_code, $result, $msg);
$result = $obj->getCode();
$msg = 'Wrong DbException::__construct() behavior!';
$this->assertSame($code, $result, $msg);
}
示例13: raw_query
/**
* Run RAW Query
*
* @param string $sql
*
* @return Zend_Db_Statement_Interface
* @throws PDOException
*/
public function raw_query($sql)
{
try {
return $this->query($sql);
} catch (Zend_Db_Statement_Exception $e) {
// Convert to PDOException to maintain backwards compatibility with usage of MySQL adapter
$e = $e->getPrevious();
if (!$e instanceof PDOException) {
$e = new PDOException($e->getMessage(), $e->getCode());
}
throw $e;
}
}
示例14: processPDOException
/**
* @param \PDOException
* @throws \Nella\Models\Exception
* @throws \Nella\Models\EmptyValueException
* @throws \Nella\Models\DuplicateEntryException
*/
protected function processPDOException(\PDOException $e)
{
$info = $e->errorInfo;
if ($info[0] == 23000 && $info[1] == 1062) { // unique fail
// @todo how to detect column name ?
throw new \Nella\Models\DuplicateEntryException($e->getMessage(), NULL, $e);
} elseif ($info[0] == 23000 && $info[1] == 1048) { // notnull fail
// @todo convert table column name to entity column name
$name = substr($info[2], strpos($info[2], "'") + 1);
$name = substr($name, 0, strpos($name, "'"));
throw new \Nella\Models\EmptyValueException($e->getMessage(), $name, $e);
} else { // other fail
throw new \Nella\Models\Exception($e->getMessage(), 0, $e);
}
}
示例15: convertException
public function convertException(\PDOException $e)
{
$code = isset($e->errorInfo[1]) ? $e->errorInfo[1] : NULL;
$msg = $e->getMessage();
if ($code !== 19) {
return Nette\Database\DriverException::from($e);
} elseif (strpos($msg, 'must be unique') !== FALSE || strpos($msg, 'is not unique') !== FALSE || strpos($msg, 'UNIQUE constraint failed') !== FALSE) {
return Nette\Database\UniqueConstraintViolationException::from($e);
} elseif (strpos($msg, 'may not be NULL') !== FALSE || strpos($msg, 'NOT NULL constraint failed') !== FALSE) {
return Nette\Database\NotNullConstraintViolationException::from($e);
} elseif (strpos($msg, 'foreign key constraint failed') !== FALSE || strpos($msg, 'FOREIGN KEY constraint failed') !== FALSE) {
return Nette\Database\ForeignKeyConstraintViolationException::from($e);
} else {
return Nette\Database\ConstraintViolationException::from($e);
}
}