Add filename to api code doc
[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 * Class constructor.
28 *
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.
40 */
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)) {
44 $code = $error_code;
45 }
46 else {
47 $code = 0;
48 }
49 parent::__construct(ts($message), $code, $previous);
50 $this->extraParams = $extraParams + array('error_code' => $error_code);
51 }
52
53 /**
54 * Custom string representation of object.
55 *
56 * @return string
57 */
58 public function __toString() {
59 return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
60 }
61
62 /**
63 * Get extra parameters.
64 *
65 * @return array
66 */
67 public function getExtraParams() {
68 return $this->extraParams;
69 }
70
71 /**
72 * Get error codes.
73 *
74 * @return array
75 */
76 public function getErrorCodes() {
77 return array(
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',
83 );
84 }
85
86 }
87
88 /**
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
91 */
92 class CiviCRM_API3_Exception extends Exception {
93 private $extraParams = array();
94
95 /**
96 * Class constructor.
97 *
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.
109 */
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);
113 }
114
115 /**
116 * Custom string representation of object.
117 *
118 * @return string
119 */
120 public function __toString() {
121 return __CLASS__ . ": [{$this->extraParams['error_code']}: {$this->message}\n";
122 }
123
124 /**
125 * Get error code.
126 *
127 * @return mixed
128 */
129 public function getErrorCode() {
130 return $this->extraParams['error_code'];
131 }
132
133 /**
134 * Get extra parameters.
135 *
136 * @return array
137 */
138 public function getExtraParams() {
139 return $this->extraParams;
140 }
141
142 }