Merge pull request #2744 from pratik-joshi/CRM-13992_temp_fix
[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 public function __construct($message, $error_code = 0, $extraParams = array(),Exception $previous = null) {
29 if (is_numeric ($error_code)) // using int for error code "old way")
30 $code = $error_code;
31 else
32 $code=0;
33 parent::__construct(ts($message), $code, $previous);
34 $this->extraParams = $extraParams + array('error_code' => $error_code);
35 }
36
37 // custom string representation of object
38 public function __toString() {
39 return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
40 }
41
42 public function getExtraParams() {
43 return $this->extraParams;
44 }
45
46 public function getErrorCodes(){
47 return array(
48 2000 => '$params was not an array',
49 2001 => 'Invalid Value for Date field',
50 2100 => 'String value is longer than permitted length',
51 self::UNAUTHORIZED => 'Unauthorized',
52 self::NOT_IMPLEMENTED => 'Entity or method is not implemented',
53 );
54 }
55 }
56 /**
57 * This api exception returns more information than the default one. We are using it rather than
58 * API_Exception from the api wrapper as the namespace is more generic
59 * @param string $message the human friendly error message
60 * @param string $error_code a computer friendly error code. By convention, no space (but underscore allowed)
61 * ex: mandatory_missing, duplicate, invalid_format
62 * @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
63 */
64 class CiviCRM_API3_Exception extends Exception
65 {
66 private $extraParams = array();
67 public function __construct($message, $error_code, $extraParams = array(),Exception $previous = null) {
68 parent::__construct(ts($message));
69 $this->extraParams = $extraParams + array('error_code' => $error_code);
70 }
71
72 // custom string representation of object
73 public function __toString() {
74 return __CLASS__ . ": [{$this->extraParams['error_code']}: {$this->message}\n";
75 }
76
77 public function getErrorCode() {
78 return $this->extraParams['error_code'];
79 }
80 public function getExtraParams() {
81 return $this->extraParams;
82 }
83 }