Merge remote-tracking branch 'upstream/4.4' into 4.4-4.5-2014-09-08-20-42-29
[civicrm-core.git] / api / Exception.php
1 <?php
2 /**
3 * File for the CiviCRM APIv3 API wrapper
4 *
5 * @package CiviCRM_APIv3
6 * @subpackage API
7 *
8 * @copyright CiviCRM LLC (c) 2004-2014
9 */
10
11 /**
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
19 * @param array $data
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
21 */
22 class API_Exception extends Exception
23 {
24 const UNAUTHORIZED = 'unauthorized';
25 const NOT_IMPLEMENTED = 'not-found';
26
27 private $extraParams = array();
28
29 /**
30 * @param string $message
31 * @param int $error_code
32 * @param array $extraParams
33 * @param Exception $previous
34 */
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")
37 $code = $error_code;
38 else
39 $code=0;
40 parent::__construct(ts($message), $code, $previous);
41 $this->extraParams = $extraParams + array('error_code' => $error_code);
42 }
43
44 // custom string representation of object
45 /**
46 * @return string
47 */
48 public function __toString() {
49 return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
50 }
51
52 /**
53 * @return array
54 */
55 public function getExtraParams() {
56 return $this->extraParams;
57 }
58
59 /**
60 * @return array
61 */
62 public function getErrorCodes(){
63 return array(
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',
69 );
70 }
71 }
72 /**
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
79 */
80 class CiviCRM_API3_Exception extends Exception
81 {
82 private $extraParams = array();
83
84 /**
85 * @param string $message
86 * @param int $error_code
87 * @param array $extraParams
88 * @param Exception $previous
89 */
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);
93 }
94
95 // custom string representation of object
96 /**
97 * @return string
98 */
99 public function __toString() {
100 return __CLASS__ . ": [{$this->extraParams['error_code']}: {$this->message}\n";
101 }
102
103 public function getErrorCode() {
104 return $this->extraParams['error_code'];
105 }
106
107 /**
108 * @return array
109 */
110 public function getExtraParams() {
111 return $this->extraParams;
112 }
113 }