Example #1
0
 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);
     }
 }
Example #2
0
 /**
  * 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;
 }
Example #3
0
 /**
  * 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);
 }
 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];
     }
 }
Example #5
0
 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();
 }
Example #6
0
 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());
 }
Example #7
0
 /**
  * @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());
 }
 /**
  * @param PDOException $exception
  */
 public function throwException(PDOException $exception)
 {
     if ($this->debug) {
         echo '<pre>';
         print_r(array($exception->getMessage(), $exception->getCode()));
         echo '</pre>';
         die;
     }
 }
 /**
  * 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>&nbsp;</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;'>&nbsp;</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;'>&nbsp;</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>&nbsp;</td></tr></table>";
     $subject = "DATABASE ERROR";
     $this->Postcase->sendGridEmail(SUPPORT_EMAIL, DEV_EMAIL, $subject, $message, 'Exception');
     $this->_outputMessage($this->template);
 }
Example #10
0
File: Db.php Project: bashedev/Db
 /**
  * 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());
         }
     }
 }
Example #11
0
 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;
 }
 /**
  * @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);
 }
Example #13
0
 /**
  * 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;
     }
 }
Example #14
0
	/**
	 * @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);
		}
	}
Example #15
0
 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);
     }
 }
 public function __construct($errorInfo)
 {
     $sqlState = isset($errorInfo[0]) ? $errorInfo[0] : '-';
     $driverMessage = isset($errorInfo[2]) ? $errorInfo[2] : 'Unknown';
     parent::__construct("{$sqlState} {$driverMessage}", 0);
     $this->code = $sqlState;
     $this->errorInfo = $errorInfo;
 }
Example #17
0
 public function __construct($message, $code = 0)
 {
     if (is_a($message, 'Exception')) {
         parent::__construct($message, intval($message->getCode()));
     } else {
         parent::__construct($message, intval($code));
     }
 }
Example #18
0
 public function convertException(\PDOException $e)
 {
     $code = isset($e->errorInfo[0]) ? $e->errorInfo[0] : NULL;
     if ($code === '0A000' && strpos($e->getMessage(), 'truncate') !== FALSE) {
         return Nette\Database\ForeignKeyConstraintViolationException::from($e);
     } elseif ($code === '23502') {
         return Nette\Database\NotNullConstraintViolationException::from($e);
     } elseif ($code === '23503') {
         return Nette\Database\ForeignKeyConstraintViolationException::from($e);
     } elseif ($code === '23505') {
         return Nette\Database\UniqueConstraintViolationException::from($e);
     } elseif ($code === '08006') {
         return Nette\Database\ConnectionException::from($e);
     } else {
         return Nette\Database\DriverException::from($e);
     }
 }
 /**
  * Constructor.
  *
  * @param \PDOException $exception The PDO exception to wrap.
  */
 public function __construct(\PDOException $exception)
 {
     parent::__construct($exception->getMessage(), 0, $exception);
     $this->code = $exception->getCode();
     $this->errorInfo = $exception->errorInfo;
     $this->errorCode = isset($exception->errorInfo[1]) ? $exception->errorInfo[1] : $exception->getCode();
     $this->sqlState = isset($exception->errorInfo[0]) ? $exception->errorInfo[0] : $exception->getCode();
 }
Example #20
0
 public function __construct(\PDOException $e, $queryString = null, $queryParams = null)
 {
     parent::__construct($e->getMessage(), 0, $e);
     $this->code = $e->getCode();
     $this->errorInfo = $e->errorInfo;
     $this->queryString = $queryString;
     $this->queryParams = $queryParams;
 }
Example #21
0
 /**
  * Constructor inicializa los atributos de la clase
  * @param string $message Mensaje de la exception lanzada
  * @param integer $code Codigo de error generado en la consulta Mysql
  */
 public function __construct($message = null, $code, $config)
 {
     parent::__construct($message, (int) $code);
     $this->message = $message;
     $this->code = $code;
     $this->config = $config;
     // $this->changemessage();
     $this->log();
 }
Example #22
0
 /**
  * 抛出错误信息
  *
  * @param PDOException $e
  */
 private function db_error(PDOException $e)
 {
     echo '出错了!!!<br />';
     echo '错误文件:', $e->getFile(), '<br />';
     echo '错误行号:', $e->getLine(), '<br />';
     echo '错误编码:', $e->getCode(), '<br />';
     echo '错误详情:', $e->getMessage(), '<br />';
     exit('END...');
 }
Example #23
0
 /**
  * Constructor
  *
  * @param \PDOException $e      PDO exception
  * @param string        $query  SQL query OPTIONAL
  * @param array         $params SQL query parameters OPTIONAL
  *
  * @return void
  */
 public function __construct(\PDOException $e, $query = null, array $params = array())
 {
     $code = $e->getCode();
     $message = $e->getMessage();
     // Remove user credentials
     if (strstr($message, 'SQLSTATE[') && preg_match('/SQLSTATE\\[(\\w+)\\] \\[(\\w+)\\] (.*)/', $message, $matches)) {
         $code = 'HT000' == $matches[1] ? $matches[2] : $matches[1];
         $message = $matches[3];
     }
     // Add additional information
     if ($query) {
         $message .= PHP_EOL . 'SQL query: ' . $query;
     }
     if ($params) {
         $message .= PHP_EOL . 'SQL query parameters: ' . var_export($params, true);
     }
     $this->code = intval($code);
     $this->message = $message;
 }
Example #24
0
 private static function reportProblem(PDOException $e)
 {
     $trace = $e->getTrace();
     $DBcall = $trace[1];
     $functionCall = $trace[2];
     $type = 'error with DB';
     $function = $DBcall['function'] . ' into ' . $functionCall['function'] . ' in file ' . $functionCall['file'];
     $message = serialize($DBcall['args']);
     Db::queryModify('INSERT INTO tickets (`type`, `title`, `message`, `timestamp`)
                      VALUES (?,?,?,NOW())', [$type, $function, $message]);
 }
Example #25
0
 /**
  * Constructor.
  */
 function __construct($message = "", $code = 0, $previous = null, $query_sql = null, $query_params = null)
 {
     parent::__construct($message, null, $previous);
     $this->code = $code;
     // PDOException has non-numeric code, we need to avoid check in \Exception::__construct
     if ($previous) {
         $this->errorInfo = $previous->errorInfo;
     }
     $this->query_sql = (string) $query_sql;
     $this->query_params = (array) $query_params;
 }
Example #26
0
 public static function convertPDOException(\PDOException $e)
 {
     $exceptions = array('\\Psc\\Doctrine\\ForeignKeyConstraintException', '\\Psc\\Doctrine\\UniqueConstraintException', '\\Psc\\Doctrine\\TooManyConnectionsException', '\\Psc\\Doctrine\\UnknownColumnException');
     /* grml. fix pdo */
     if ($e->errorInfo === NULL && mb_strlen($msg = $e->getMessage()) > 0) {
         //SQLSTATE[08004] [1040] Too many connections
         if (\Psc\Preg::match($msg, '/SQLSTATE\\[([0-9]+)\\]\\s*\\[([0-9]+)\\]\\s*(.*)?/s', $m)) {
             $e->errorInfo[0] = $m[1];
             $e->errorInfo[1] = $m[2];
             $e->errorInfo[2] = $m[3];
         }
     }
     foreach ($exceptions as $cname) {
         if ($cname::check($e)) {
             return new $cname($e);
         }
     }
     //throw new \Psc\Exception('unconverted PDO Exception: '.Code::varInfo($e->errorInfo),0,$e);
     print 'unconverted PDOException: ' . Code::varInfo($e->errorInfo);
     return $e;
 }
Example #27
0
 public function __construct($message = "", $code = 0, \PDOException $previous = null)
 {
     if ($previous) {
         $this->message = $previous->message;
         $this->code = $previous->code;
         $this->file = $previous->file;
         $this->line = $previous->line;
         $this->trace = $previous->getTrace();
         $this->previous = $previous;
     }
     $backtrace = debug_backtrace();
     // We need directory separator for Windows
     $ipDbPath = 'Ip' . DIRECTORY_SEPARATOR . 'Db.php';
     $pathLength = strlen($ipDbPath);
     // We usually want exception to show error in the code that uses Db class
     foreach ($backtrace as $info) {
         if (substr($info['file'], -$pathLength) != $ipDbPath) {
             $this->file = $info['file'];
             $this->line = $info['line'];
             break;
         }
     }
 }
Example #28
0
 public function __construct($message = "", $code = 0, PDOException $e = null)
 {
     if (is_subclass_of($message, 'PDOException')) {
         $e = $message;
         $code = $e->getCode();
         $message = $e->getMessage();
     }
     parent::__construct($message, $code, $e);
     $state = $this->getMessage();
     if (!strstr($state, 'SQLSTATE[')) {
         $state = $this->getCode();
     }
     if (strstr($state, 'SQLSTATE[')) {
         preg_match('/SQLSTATE\\[(\\w+)\\] \\[(\\w+)\\] (.*)/', $state, $matches);
         if (sizeof($matches) > 0) {
             $this->code = $matches[1] == 'HT000' ? $matches[2] : $matches[1];
             $this->message = $matches[3];
         }
     }
 }
Example #29
0
 public function handlePDOException(PDOException $e)
 {
     trigger_error('PHP PDO Error in ' . $e->getFile() . ' @' . strval($e->getLine()) . ' [' . strval($e->getCode()) . '] :: ' . $e->getMessage(), E_USER_WARNING);
     foreach ($e->getTrace() as $a => $b) {
         foreach ($b as $c => $d) {
             if ($c == 'args') {
                 foreach ($d as $e => $f) {
                     trigger_error('PHP PDO Error trace: ' . strval($a) . '# args: ' . $e . ': ' . $f . '', E_USER_WARNING);
                 }
             } else {
                 trigger_error('PHP PDO Error trace: ' . strval($a) . '# ' . $c . ': ' . $d . '', E_USER_WARNING);
             }
         }
     }
 }
Example #30
0
 protected final function checkConnection(\PDOException $pdoEx = NULL)
 {
     if ($pdoEx == NULL) {
         throw new NullPointerException();
     }
     $message = '';
     switch ($pdoEx->getCode()) {
         case 1045:
             $message .= 'Nesprávné údaje pro přihlášení k databázovému serveru: <b>' . $this->host . '</b><br>';
             break;
         case 2002:
             $message .= 'Nepodařilo se připojit k databázovému serveru: <b>' . $this->host . '</b><br>';
             break;
         case 1044:
             $message .= 'Nepodařilo se vybrat databázi na databázovém serveru: <b>' . $this->host . '</b><br>';
             break;
         default:
             $message .= 'Neočekávaná PDO chyba číslo: <b>' . $pdoEx->getCode() . '</b> při připojení k databázovému serveru: <b>' . $this->host . '</b><br>';
     }
     if ($this->log != FALSE) {
         $this->log->log($message);
     }
     die($message);
 }