Merge pull request #127 from dlobo/CRM-12100
[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 /**
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 private $extraParams = array();
25 public function __construct($message, $error_code = 0, $extraParams = array(),Exception $previous = null) {
26 if (is_numeric ($error_code)) // using int for error code "old way")
27 $code = $error_code;
28 else
29 $code=0;
30 parent::__construct(ts($message), $code, $previous);
31 $this->extraParams = $extraParams + array('error_code' => $error_code);
32 }
33
34 // custom string representation of object
35 public function __toString() {
36 return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
37 }
38
39 public function getExtraParams() {
40 return $this->extraParams;
41 }
42
43 public function getErrorCodes(){
44 return array(
45 2000 => '$params was not an array',
46 2001 => 'Invalid Value for Date field',
47 2100 => 'String value is longer than permitted length'
48 );
49 }
50 }