Merge pull request #363 from davecivicrm/CRM-12308
[civicrm-core.git] / api / Exception.php
CommitLineData
6a488035
TO
1<?php\r
2/**\r
3 * File for the CiviCRM APIv3 API wrapper\r
4 *\r
5 * @package CiviCRM_APIv3\r
6 * @subpackage API\r
7 *\r
8 * @copyright CiviCRM LLC (c) 2004-2013\r
9 */\r
10\r
11e09c59
TO
11/**\r
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.\r
6a488035
TO
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 \r
14 * @param string $message \r
15 * the human friendly error message\r
16 * @param string $error_code\r
17 * a computer friendly error code. By convention, no space (but underscore allowed)\r
18 * ex: mandatory_missing, duplicate, invalid_format\r
19 * @param array $data\r
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 \r
21 */\r
22class API_Exception extends Exception\r
23{\r
24 private $extraParams = array();\r
25 public function __construct($message, $error_code = 0, $extraParams = array(),Exception $previous = null) {\r
26 if (is_numeric ($error_code)) // using int for error code "old way")\r
27 $code = $error_code;\r
28 else\r
29 $code=0;\r
30 parent::__construct(ts($message), $code, $previous);\r
31 $this->extraParams = $extraParams + array('error_code' => $error_code);\r
32 }\r
33\r
34 // custom string representation of object\r
35 public function __toString() {\r
36 return __CLASS__ . ": [{$this->code}]: {$this->message}\n";\r
37 }\r
38\r
39 public function getExtraParams() {\r
40 return $this->extraParams;\r
41 }\r
42\r
43 public function getErrorCodes(){ \r
44 return array(\r
45 2000 => '$params was not an array',\r
46 2001 => 'Invalid Value for Date field',\r
47 2100 => 'String value is longer than permitted length'\r
48 );\r
49 }\r
50}\r