| 1 | <?php |
| 2 | /** |
| 3 | * @file |
| 4 | * File for the CiviCRM APIv3 API wrapper |
| 5 | * |
| 6 | * @package CiviCRM_APIv3 |
| 7 | * |
| 8 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
| 9 | */ |
| 10 | |
| 11 | /** |
| 12 | * This api exception returns more information than the default one. The aim |
| 13 | * it let the api consumer know better what is exactly the error without |
| 14 | * having to parse the error message. |
| 15 | * |
| 16 | * If you consume an api that doesn't return an error_code or the extra data |
| 17 | * you need, consider improving the api and contribute. |
| 18 | */ |
| 19 | class API_Exception extends Exception { |
| 20 | const UNAUTHORIZED = 'unauthorized'; |
| 21 | const NOT_IMPLEMENTED = 'not-found'; |
| 22 | |
| 23 | private $extraParams = []; |
| 24 | |
| 25 | /** |
| 26 | * Class constructor. |
| 27 | * |
| 28 | * @param string $message |
| 29 | * The human friendly error message. |
| 30 | * @param mixed $error_code |
| 31 | * A computer friendly error code. By convention, no space (but underscore |
| 32 | * allowed) (ex: mandatory_missing, duplicate, invalid_format). |
| 33 | * @param array $extraParams |
| 34 | * Extra params to return. eg an extra array of ids. It is not mandatory, |
| 35 | * but can help the computer using the api. Keep in mind the api consumer |
| 36 | * isn't to be trusted. eg. the database password is NOT a good extra data. |
| 37 | * @param Exception|NULL $previous |
| 38 | * A previous exception which caused this new exception. |
| 39 | */ |
| 40 | public function __construct($message, $error_code = 0, $extraParams = [], Exception $previous = NULL) { |
| 41 | // Using int for error code "old way") ? |
| 42 | if (is_numeric($error_code)) { |
| 43 | $code = $error_code; |
| 44 | } |
| 45 | else { |
| 46 | $code = 0; |
| 47 | } |
| 48 | parent::__construct(ts($message), $code, $previous); |
| 49 | $this->extraParams = $extraParams + ['error_code' => $error_code]; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Custom string representation of object. |
| 54 | * |
| 55 | * @return string |
| 56 | */ |
| 57 | public function __toString() { |
| 58 | return __CLASS__ . ": [{$this->code}]: {$this->message}\n"; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Get extra parameters. |
| 63 | * |
| 64 | * @return array |
| 65 | */ |
| 66 | public function getExtraParams() { |
| 67 | return $this->extraParams; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Get error codes. |
| 72 | * |
| 73 | * @return array |
| 74 | */ |
| 75 | public function getErrorCodes() { |
| 76 | return [ |
| 77 | 2000 => '$params was not an array', |
| 78 | 2001 => 'Invalid Value for Date field', |
| 79 | 2100 => 'String value is longer than permitted length', |
| 80 | self::UNAUTHORIZED => 'Unauthorized', |
| 81 | self::NOT_IMPLEMENTED => 'Entity or method is not implemented', |
| 82 | ]; |
| 83 | } |
| 84 | |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * This api exception returns more information than the default one. We are using it rather than |
| 89 | * API_Exception from the api wrapper as the namespace is more generic |
| 90 | */ |
| 91 | class CiviCRM_API3_Exception extends Exception { |
| 92 | private $extraParams = []; |
| 93 | |
| 94 | /** |
| 95 | * Class constructor. |
| 96 | * |
| 97 | * @param string $message |
| 98 | * The human friendly error message. |
| 99 | * @param mixed $error_code |
| 100 | * A computer friendly error code. By convention, no space (but underscore |
| 101 | * allowed) (ex: mandatory_missing, duplicate, invalid_format). |
| 102 | * @param array $extraParams |
| 103 | * Extra params to return. eg an extra array of ids. It is not mandatory, |
| 104 | * but can help the computer using the api. Keep in mind the api consumer |
| 105 | * isn't to be trusted. eg. the database password is NOT a good extra data. |
| 106 | * @param Exception|NULL $previous |
| 107 | * A previous exception which caused this new exception. |
| 108 | */ |
| 109 | public function __construct($message, $error_code = 0, $extraParams = [], Exception $previous = NULL) { |
| 110 | parent::__construct(ts($message)); |
| 111 | $this->extraParams = $extraParams + ['error_code' => $error_code]; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Custom string representation of object. |
| 116 | * |
| 117 | * @return string |
| 118 | */ |
| 119 | public function __toString() { |
| 120 | return __CLASS__ . ": [{$this->extraParams['error_code']}: {$this->message}\n"; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Get error code. |
| 125 | * |
| 126 | * @return mixed |
| 127 | */ |
| 128 | public function getErrorCode() { |
| 129 | return $this->extraParams['error_code']; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Get extra parameters. |
| 134 | * |
| 135 | * @return array |
| 136 | */ |
| 137 | public function getExtraParams() { |
| 138 | return $this->extraParams; |
| 139 | } |
| 140 | |
| 141 | } |