3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
13 * Base class for exceptions generated by CiviCRM.
14 * This Exception returns more information than the default one. We are using it from the
15 * form layer to allow redirects to occur without having redirects in the BAO
17 * Extra params to return. eg an extra array of ids. It is not mandatory, but can help the computer using the api. Keep in mind the api consumer isn't to be trusted. eg. the database password is NOT a good extra data.
19 class CRM_Core_Exception
extends PEAR_Exception
{
21 const UNAUTHORIZED
= 'unauthorized';
22 const NOT_IMPLEMENTED
= 'not-found';
24 private $errorData = [];
29 * @param string $message
30 * The human friendly error message.
31 * @param int|string $error_code
32 * A computer friendly error code. By convention, no space (but underscore
33 * allowed) (ex: mandatory_missing, duplicate, invalid_format).
34 * @param array $errorData
35 * Extra params to return. eg an extra array of ids. It is not mandatory,
36 * but can help the computer using the api. Keep in mind the api consumer
37 * isn't to be trusted. eg. the database password is NOT a good extra data.
38 * @param Throwable|NULL $previous
39 * A previous exception which caused this new exception.
41 public function __construct($message, $error_code = 0, $errorData = [], $previous = NULL) {
42 // Using int for error code "old way") ?
43 if (is_numeric($error_code)) {
49 // DIVERGENCE: CRM_Core_Exception used `$message`. API_Exception and CiviCRM_API3_Exception used `ts($message)`.
50 // IMHO, this seems like a terrible place to put `ts()` - civistrings won't detect, and it's hard dependency on l10n services.
51 parent
::__construct($message, $code, $previous);
52 $this->errorData
= $errorData +
['error_code' => $error_code];
56 * Custom string representation of object.
60 public function __toString() {
61 return __CLASS__
. ": [{$this->errorData['error_code']}: {$this->message}\n";
69 public function getErrorCode() {
70 return $this->errorData
['error_code'];
74 * Return specific error information.
76 * (Can be used for more detailed error messages or translation.)
78 * This method may be overridden in child exception classes in order
79 * to add functionality not present in PEAR_Exception and is a placeholder
82 * The returned array must be an associative array of parameter => value like so:
84 * array('name' => $name, 'context' => array(...))
88 public function getErrorData() {
89 return $this->errorData
;
93 * Get extra parameters.
96 * @deprecated Remove in v6.0. Compatibility with older API_Exception/CiviCRM_API3_Exception contracts.
98 public function getExtraParams() {
99 return $this->errorData
;
105 * DIVERGENCE: API_Exception defined a discoverable list of error-codes. CRM_Core_Exception and CiviCRM_API3_Exception did not.
109 public function getErrorCodes() {
111 2000 => '$params was not an array',
112 2001 => 'Invalid Value for Date field',
113 2100 => 'String value is longer than permitted length',
114 self
::UNAUTHORIZED
=> 'Unauthorized',
115 self
::NOT_IMPLEMENTED
=> 'Entity or method is not implemented',