3 * File for the CiviCRM APIv3 API wrapper
5 * @package CiviCRM_APIv3
8 * @copyright CiviCRM LLC (c) 2004-2014
12 * This api exception returns more information than the default one. The aim it let the api consumer know better what is exactly the error without having to parse the error message.
13 * If you consume an api that doesn't return an error_code or the extra data you need, consider improving the api and contribute
14 * @param string $message
15 * the human friendly error message
16 * @param string $error_code
17 * a computer friendly error code. By convention, no space (but underscore allowed)
18 * ex: mandatory_missing, duplicate, invalid_format
20 * 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
22 class API_Exception
extends Exception
24 const UNAUTHORIZED
= 'unauthorized';
25 const NOT_IMPLEMENTED
= 'not-found';
27 private $extraParams = array();
30 * @param string $message
31 * @param int $error_code
32 * @param array $extraParams
33 * @param Exception $previous
35 public function __construct($message, $error_code = 0, $extraParams = array(),Exception
$previous = null) {
36 if (is_numeric ($error_code)) // using int for error code "old way")
40 parent
::__construct(ts($message), $code, $previous);
41 $this->extraParams
= $extraParams +
array('error_code' => $error_code);
44 // custom string representation of object
48 public function __toString() {
49 return __CLASS__
. ": [{$this->code}]: {$this->message}\n";
55 public function getExtraParams() {
56 return $this->extraParams
;
62 public function getErrorCodes(){
64 2000 => '$params was not an array',
65 2001 => 'Invalid Value for Date field',
66 2100 => 'String value is longer than permitted length',
67 self
::UNAUTHORIZED
=> 'Unauthorized',
68 self
::NOT_IMPLEMENTED
=> 'Entity or method is not implemented',
73 * This api exception returns more information than the default one. We are using it rather than
74 * API_Exception from the api wrapper as the namespace is more generic
75 * @param string $message the human friendly error message
76 * @param string $error_code a computer friendly error code. By convention, no space (but underscore allowed)
77 * ex: mandatory_missing, duplicate, invalid_format
78 * @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
80 class CiviCRM_API3_Exception
extends Exception
82 private $extraParams = array();
85 * @param string $message
86 * @param int $error_code
87 * @param array $extraParams
88 * @param Exception $previous
90 public function __construct($message, $error_code, $extraParams = array(),Exception
$previous = null) {
91 parent
::__construct(ts($message));
92 $this->extraParams
= $extraParams +
array('error_code' => $error_code);
95 // custom string representation of object
99 public function __toString() {
100 return __CLASS__
. ": [{$this->extraParams['error_code']}: {$this->message}\n";
103 public function getErrorCode() {
104 return $this->extraParams
['error_code'];
110 public function getExtraParams() {
111 return $this->extraParams
;