4 * File for the CiviCRM APIv3 API wrapper
6 * @package CiviCRM_APIv3
9 * @copyright CiviCRM LLC (c) 2004-2014
13 * This api exception returns more information than the default one. The aim
14 * it let the api consumer know better what is exactly the error without
15 * having to parse the error message.
17 * If you consume an api that doesn't return an error_code or the extra data
18 * you need, consider improving the api and contribute.
20 class API_Exception
extends Exception
{
21 const UNAUTHORIZED
= 'unauthorized';
22 const NOT_IMPLEMENTED
= 'not-found';
24 private $extraParams = array();
29 * @param string $message
30 * The human friendly error message.
31 * @param mixed $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 $extraParams
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 Exception|NULL $previous
39 * A previous exception which caused this new exception.
41 public function __construct($message, $error_code = 0, $extraParams = array(), Exception
$previous = NULL) {
42 // Using int for error code "old way") ?
43 if (is_numeric($error_code)) {
49 parent
::__construct(ts($message), $code, $previous);
50 $this->extraParams
= $extraParams +
array('error_code' => $error_code);
54 * Custom string representation of object.
58 public function __toString() {
59 return __CLASS__
. ": [{$this->code}]: {$this->message}\n";
63 * Get extra parameters.
67 public function getExtraParams() {
68 return $this->extraParams
;
76 public function getErrorCodes() {
78 2000 => '$params was not an array',
79 2001 => 'Invalid Value for Date field',
80 2100 => 'String value is longer than permitted length',
81 self
::UNAUTHORIZED
=> 'Unauthorized',
82 self
::NOT_IMPLEMENTED
=> 'Entity or method is not implemented',
89 * This api exception returns more information than the default one. We are using it rather than
90 * API_Exception from the api wrapper as the namespace is more generic
92 class CiviCRM_API3_Exception
extends Exception
{
93 private $extraParams = array();
98 * @param string $message
99 * The human friendly error message.
100 * @param mixed $error_code
101 * A computer friendly error code. By convention, no space (but underscore
102 * allowed) (ex: mandatory_missing, duplicate, invalid_format).
103 * @param array $extraParams
104 * Extra params to return. eg an extra array of ids. It is not mandatory,
105 * but can help the computer using the api. Keep in mind the api consumer
106 * isn't to be trusted. eg. the database password is NOT a good extra data.
107 * @param Exception|NULL $previous
108 * A previous exception which caused this new exception.
110 public function __construct($message, $error_code, $extraParams = array(), Exception
$previous = NULL) {
111 parent
::__construct(ts($message));
112 $this->extraParams
= $extraParams +
array('error_code' => $error_code);
116 * Custom string representation of object.
120 public function __toString() {
121 return __CLASS__
. ": [{$this->extraParams['error_code']}: {$this->message}\n";
129 public function getErrorCode() {
130 return $this->extraParams
['error_code'];
134 * Get extra parameters.
138 public function getExtraParams() {
139 return $this->extraParams
;