X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=api%2FException.php;h=ea4705820e010eeb8997a1ef0eb8cd6996d4dfdb;hb=6a0cf2c64391e7feee08a10de8fa82e821b69aeb;hp=e8f24e4e0157ac9bd745a24ed23eaee64719f10d;hpb=07638b4c7fe4e09b5728e35f9171901cf566abfb;p=civicrm-core.git diff --git a/api/Exception.php b/api/Exception.php index e8f24e4e01..ea4705820e 100644 --- a/api/Exception.php +++ b/api/Exception.php @@ -48,3 +48,31 @@ class API_Exception extends Exception ); } } +/** + * This api exception returns more information than the default one. We are using it rather than + * API_Exception from the api wrapper as the namespace is more generic + * @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 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 + */ +class CiviCRM_API3_Exception extends Exception +{ + private $extraParams = array(); + public function __construct($message, $error_code, $extraParams = array(),Exception $previous = null) { + parent::__construct(ts($message)); + $this->extraParams = $extraParams + array('error_code' => $error_code); + } + + // custom string representation of object + public function __toString() { + return __CLASS__ . ": [{$this->extraParams['error_code']}: {$this->message}\n"; + } + + public function getErrorCode() { + return $this->extraParams['error_code']; + } + public function getExtraParams() { + return $this->extraParams; + } +}