406997be46c8af9bd38aded5da7027dcde9675f1
[civicrm-core.git] / api / Exception.php
1 <?php
2 /**
3 * @file
4 * File for the CiviCRM APIv3 API wrapper
5 *
6 * @package CiviCRM_APIv3
7 * @subpackage API
8 *
9 * @copyright CiviCRM LLC (c) 2004-2014
10 */
11
12 /**
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.
16 *
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.
19 */
20 class API_Exception extends Exception {
21 const UNAUTHORIZED = 'unauthorized';
22 const NOT_IMPLEMENTED = 'not-found';
23
24 private $extraParams = array();
25
26 /**
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.
38 */
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)) {
42 $code = $error_code;
43 }
44 else {
45 $code = 0;
46 }
47 parent::__construct(ts($message), $code, $previous);
48 $this->extraParams = $extraParams + array('error_code' => $error_code);
49 }
50
51 /**
52 * custom string representation of object
53 * @return string
54 */
55 public function __toString() {
56 return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
57 }
58
59 /**
60 * @return array
61 */
62 public function getExtraParams() {
63 return $this->extraParams;
64 }
65
66 /**
67 * @return array
68 */
69 public function getErrorCodes() {
70 return array(
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',
76 );
77 }
78
79 }
80
81 /**
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
84 */
85 class CiviCRM_API3_Exception extends Exception {
86 private $extraParams = array();
87
88 /**
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.
100 */
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);
104 }
105
106 /**
107 * custom string representation of object
108 * @return string
109 */
110 public function __toString() {
111 return __CLASS__ . ": [{$this->extraParams['error_code']}: {$this->message}\n";
112 }
113
114 /**
115 * @return mixed
116 */
117 public function getErrorCode() {
118 return $this->extraParams['error_code'];
119 }
120
121 /**
122 * @return array
123 */
124 public function getExtraParams() {
125 return $this->extraParams;
126 }
127
128 }