some comment block fixes
[civicrm-core.git] / api / Exception.php
CommitLineData
e7c0be9a 1<?php
2/**
3 * File for the CiviCRM APIv3 API wrapper
4 *
5 * @package CiviCRM_APIv3
6 * @subpackage API
7 *
731a0992 8 * @copyright CiviCRM LLC (c) 2004-2014
e7c0be9a 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 */
22class API_Exception extends Exception
23{
5fda6437
TO
24 const UNAUTHORIZED = 'unauthorized';
25 const NOT_IMPLEMENTED = 'not-found';
26
e7c0be9a 27 private $extraParams = array();
aa1b1481
EM
28
29 /**
30 * @param string $message
31 * @param int $error_code
32 * @param array $extraParams
33 * @param Exception $previous
34 */
e7c0be9a 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
aa1b1481
EM
45 /**
46 * @return string
47 */
e7c0be9a 48 public function __toString() {
49 return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
50 }
51
aa1b1481
EM
52 /**
53 * @return array
54 */
e7c0be9a 55 public function getExtraParams() {
56 return $this->extraParams;
57 }
58
aa1b1481
EM
59 /**
60 * @return array
61 */
e7c0be9a 62 public function getErrorCodes(){
63 return array(
64 2000 => '$params was not an array',
65 2001 => 'Invalid Value for Date field',
5fda6437
TO
66 2100 => 'String value is longer than permitted length',
67 self::UNAUTHORIZED => 'Unauthorized',
68 self::NOT_IMPLEMENTED => 'Entity or method is not implemented',
e7c0be9a 69 );
70 }
71}
6b359437 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 */
80class CiviCRM_API3_Exception extends Exception
81{
82 private $extraParams = array();
aa1b1481
EM
83
84 /**
85 * @param string $message
86 * @param int $error_code
87 * @param array $extraParams
88 * @param Exception $previous
89 */
6b359437 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
aa1b1481
EM
96 /**
97 * @return string
98 */
6b359437 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 }
aa1b1481
EM
106
107 /**
108 * @return array
109 */
6b359437 110 public function getExtraParams() {
111 return $this->extraParams;
112 }
113}