Commit | Line | Data |
---|---|---|
e7c0be9a | 1 | <?php |
2 | /** | |
3 | * File for the CiviCRM APIv3 API wrapper | |
4 | * | |
5 | * @package CiviCRM_APIv3 | |
6 | * @subpackage API | |
7 | * | |
731a0992 | 8 | * @copyright CiviCRM LLC (c) 2004-2014 |
e7c0be9a | 9 | */ |
10 | ||
11 | /** | |
7cdbcb16 TO |
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. | |
e7c0be9a | 18 | */ |
8882ff5c | 19 | class API_Exception extends Exception { |
5fda6437 TO |
20 | const UNAUTHORIZED = 'unauthorized'; |
21 | const NOT_IMPLEMENTED = 'not-found'; | |
22 | ||
e7c0be9a | 23 | private $extraParams = array(); |
aa1b1481 EM |
24 | |
25 | /** | |
26 | * @param string $message | |
8882ff5c TO |
27 | * The human friendly error message. |
28 | * @param mixed $error_code | |
29 | * A computer friendly error code. By convention, no space (but underscore | |
30 | * allowed) (ex: mandatory_missing, duplicate, invalid_format). | |
aa1b1481 | 31 | * @param array $extraParams |
8882ff5c TO |
32 | * Extra params to return. eg an extra array of ids. It is not mandatory, |
33 | * but can help the computer using the api. Keep in mind the api consumer | |
34 | * isn't to be trusted. eg. the database password is NOT a good extra data. | |
35 | * @param Exception|NULL $previous | |
36 | * A previous exception which caused this new exception. | |
aa1b1481 | 37 | */ |
8882ff5c TO |
38 | public function __construct($message, $error_code = 0, $extraParams = array(), Exception $previous = NULL) { |
39 | // Using int for error code "old way") ? | |
40 | if (is_numeric($error_code)) { | |
e7c0be9a | 41 | $code = $error_code; |
8882ff5c TO |
42 | } |
43 | else { | |
44 | $code = 0; | |
45 | } | |
e7c0be9a | 46 | parent::__construct(ts($message), $code, $previous); |
47 | $this->extraParams = $extraParams + array('error_code' => $error_code); | |
48 | } | |
49 | ||
50 | // custom string representation of object | |
aa1b1481 EM |
51 | /** |
52 | * @return string | |
53 | */ | |
e7c0be9a | 54 | public function __toString() { |
55 | return __CLASS__ . ": [{$this->code}]: {$this->message}\n"; | |
56 | } | |
57 | ||
aa1b1481 EM |
58 | /** |
59 | * @return array | |
60 | */ | |
e7c0be9a | 61 | public function getExtraParams() { |
62 | return $this->extraParams; | |
63 | } | |
64 | ||
aa1b1481 EM |
65 | /** |
66 | * @return array | |
67 | */ | |
8882ff5c | 68 | public function getErrorCodes() { |
e7c0be9a | 69 | return array( |
8882ff5c TO |
70 | 2000 => '$params was not an array', |
71 | 2001 => 'Invalid Value for Date field', | |
72 | 2100 => 'String value is longer than permitted length', | |
73 | self::UNAUTHORIZED => 'Unauthorized', | |
74 | self::NOT_IMPLEMENTED => 'Entity or method is not implemented', | |
75 | ); | |
e7c0be9a | 76 | } |
77 | } | |
8882ff5c | 78 | |
6b359437 | 79 | /** |
80 | * This api exception returns more information than the default one. We are using it rather than | |
81 | * API_Exception from the api wrapper as the namespace is more generic | |
6b359437 | 82 | */ |
8882ff5c | 83 | class CiviCRM_API3_Exception extends Exception { |
6b359437 | 84 | private $extraParams = array(); |
aa1b1481 EM |
85 | |
86 | /** | |
87 | * @param string $message | |
8882ff5c TO |
88 | * The human friendly error message. |
89 | * @param mixed $error_code | |
90 | * A computer friendly error code. By convention, no space (but underscore | |
91 | * allowed) (ex: mandatory_missing, duplicate, invalid_format). | |
aa1b1481 | 92 | * @param array $extraParams |
8882ff5c TO |
93 | * Extra params to return. eg an extra array of ids. It is not mandatory, |
94 | * but can help the computer using the api. Keep in mind the api consumer | |
95 | * isn't to be trusted. eg. the database password is NOT a good extra data. | |
96 | * @param Exception|NULL $previous | |
97 | * A previous exception which caused this new exception. | |
aa1b1481 | 98 | */ |
8882ff5c | 99 | public function __construct($message, $error_code, $extraParams = array(), Exception $previous = NULL) { |
6b359437 | 100 | parent::__construct(ts($message)); |
101 | $this->extraParams = $extraParams + array('error_code' => $error_code); | |
102 | } | |
103 | ||
104 | // custom string representation of object | |
aa1b1481 EM |
105 | /** |
106 | * @return string | |
107 | */ | |
6b359437 | 108 | public function __toString() { |
109 | return __CLASS__ . ": [{$this->extraParams['error_code']}: {$this->message}\n"; | |
110 | } | |
111 | ||
8882ff5c TO |
112 | /** |
113 | * @return mixed | |
114 | */ | |
6b359437 | 115 | public function getErrorCode() { |
116 | return $this->extraParams['error_code']; | |
117 | } | |
aa1b1481 EM |
118 | |
119 | /** | |
120 | * @return array | |
121 | */ | |
6b359437 | 122 | public function getExtraParams() { |
123 | return $this->extraParams; | |
124 | } | |
125 | } |