-
-
Notifications
You must be signed in to change notification settings - Fork 825
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
53b7f64
commit 623da56
Showing
4 changed files
with
124 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<?php | ||
/* | ||
+--------------------------------------------------------------------+ | ||
| Copyright CiviCRM LLC. All rights reserved. | | ||
| | | ||
| This work is published under the GNU AGPLv3 license with some | | ||
| permitted exceptions and without any warranty. For full license | | ||
| and copyright information, see https://civicrm.org/licensing | | ||
+--------------------------------------------------------------------+ | ||
*/ | ||
|
||
/** | ||
* Error when the syntax of a DB query is incorrect. | ||
* | ||
* @param string $message | ||
* The human friendly error message. | ||
* @param string $error_code | ||
* A computer friendly error code. By convention, no space (but underscore allowed). | ||
* ex: mandatory_missing, duplicate, invalid_format | ||
* @param array $data | ||
* - exception The original PEAR Exception | ||
* - query The attempted mysql query | ||
*/ | ||
class CRM_Core_Exception_DBQueryInvalidException extends CRM_Core_Exception { | ||
|
||
/** | ||
* Get a message suitable to be presented to the user. | ||
* | ||
* @return string | ||
*/ | ||
public function getUserMessage(): string { | ||
$matches = []; | ||
preg_match('/\[nativecode=\d* \*\* (.*)]/', $this->getUserInfo(), $matches); | ||
$errorMessage = $matches[1]; | ||
// Error 1054 is a missing field so safe to show. Other errors give the database name. | ||
// It would be nice to point towards helpful tutorials like https://sebhastian.com/mysql-error-1054-fix/ | ||
return $this->getErrorCode() === 1054 ? ts('Invalid Query') . ' ' . $errorMessage : ts('Invalid Query') . ' ' . \DB::errorMessage($this->getPEARErrorCode()); | ||
} | ||
|
||
/** | ||
* @return \PEAR_Exception | ||
*/ | ||
public function getPEARException(): PEAR_Exception { | ||
return $this->getErrorData()['exception']; | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
protected function getPEARErrorCode(): int { | ||
return $this->getDBError()->getCode(); | ||
} | ||
|
||
/** | ||
* @return \DB_Error | ||
*/ | ||
protected function getDBError(): DB_Error { | ||
return $this->getPEARException()->getCause(); | ||
} | ||
|
||
/** | ||
* Get the mysql error code. | ||
* | ||
* @see https://mariadb.com/kb/en/mariadb-error-codes/ | ||
* | ||
* @return int | ||
*/ | ||
public function getErrorCode(): int { | ||
$dbErrorMessage = $this->getUserInfo(); | ||
$matches = []; | ||
preg_match('/\[nativecode=(\d*) /', $dbErrorMessage, $matches); | ||
return (int) $matches[1]; | ||
} | ||
|
||
/** | ||
* Get the PEAR data intended to be use useful to the user. | ||
* | ||
* @return string | ||
*/ | ||
protected function getUserInfo(): string { | ||
return $this->getPEARException()->getCause()->getUserInfo(); | ||
} | ||
|
||
/** | ||
* Get the attempted sql. | ||
* | ||
* @return string | ||
*/ | ||
public function getSQL(): string { | ||
return $this->getDBError()->getDebugInfo(); | ||
} | ||
|
||
/** | ||
* Get the attempted sql. | ||
* | ||
* @return string | ||
*/ | ||
public function getDebugInfo(): string { | ||
return $this->getDBError()->getUserInfo(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters