406997be46c8af9bd38aded5da7027dcde9675f1
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();
27 * @param string $message
28 * The human friendly error message.
29 * @param mixed $error_code
30 * A computer friendly error code. By convention, no space (but underscore
31 * allowed) (ex: mandatory_missing, duplicate, invalid_format).
32 * @param array $extraParams
33 * Extra params to return. eg an extra array of ids. It is not mandatory,
34 * but can help the computer using the api. Keep in mind the api consumer
35 * isn't to be trusted. eg. the database password is NOT a good extra data.
36 * @param Exception|NULL $previous
37 * A previous exception which caused this new exception.
39 public function __construct($message, $error_code = 0, $extraParams = array(), Exception
$previous = NULL) {
40 // Using int for error code "old way") ?
41 if (is_numeric($error_code)) {
47 parent
::__construct(ts($message), $code, $previous);
48 $this->extraParams
= $extraParams +
array('error_code' => $error_code);
52 * custom string representation of object
55 public function __toString() {
56 return __CLASS__
. ": [{$this->code}]: {$this->message}\n";
62 public function getExtraParams() {
63 return $this->extraParams
;
69 public function getErrorCodes() {
71 2000 => '$params was not an array',
72 2001 => 'Invalid Value for Date field',
73 2100 => 'String value is longer than permitted length',
74 self
::UNAUTHORIZED
=> 'Unauthorized',
75 self
::NOT_IMPLEMENTED
=> 'Entity or method is not implemented',
82 * This api exception returns more information than the default one. We are using it rather than
83 * API_Exception from the api wrapper as the namespace is more generic
85 class CiviCRM_API3_Exception
extends Exception
{
86 private $extraParams = array();
89 * @param string $message
90 * The human friendly error message.
91 * @param mixed $error_code
92 * A computer friendly error code. By convention, no space (but underscore
93 * allowed) (ex: mandatory_missing, duplicate, invalid_format).
94 * @param array $extraParams
95 * Extra params to return. eg an extra array of ids. It is not mandatory,
96 * but can help the computer using the api. Keep in mind the api consumer
97 * isn't to be trusted. eg. the database password is NOT a good extra data.
98 * @param Exception|NULL $previous
99 * A previous exception which caused this new exception.
101 public function __construct($message, $error_code, $extraParams = array(), Exception
$previous = NULL) {
102 parent
::__construct(ts($message));
103 $this->extraParams
= $extraParams +
array('error_code' => $error_code);
107 * custom string representation of object
110 public function __toString() {
111 return __CLASS__
. ": [{$this->extraParams['error_code']}: {$this->message}\n";
117 public function getErrorCode() {
118 return $this->extraParams
['error_code'];
124 public function getExtraParams() {
125 return $this->extraParams
;