Move .permissions.php and .listAll.php
[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-2013
9 */
10
11 /* 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.
12 * If you consume an api that doesn't return an error_code or the extra data you need, consider improving the api and contribute
13 * @param string $message
14 * the human friendly error message
15 * @param string $error_code
16 * a computer friendly error code. By convention, no space (but underscore allowed)
17 * ex: mandatory_missing, duplicate, invalid_format
18 * @param array $data
19 * 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
20 */
21 class API_Exception extends Exception
22 {
23 private $extraParams = array();
24 public function __construct($message, $error_code = 0, $extraParams = array(),Exception $previous = null) {
25 if (is_numeric ($error_code)) // using int for error code "old way")
26 $code = $error_code;
27 else
28 $code=0;
29 parent::__construct(ts($message), $code, $previous);
30 $this->extraParams = $extraParams + array('error_code' => $error_code);
31 }
32
33 // custom string representation of object
34 public function __toString() {
35 return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
36 }
37
38 public function getExtraParams() {
39 return $this->extraParams;
40 }
41
42 public function getErrorCodes(){
43 return array(
44 2000 => '$params was not an array',
45 2001 => 'Invalid Value for Date field',
46 2100 => 'String value is longer than permitted length'
47 );
48 }
49 }