(NFC) (dev/core#878) Simplify copyright header (CRM/*)
[civicrm-core.git] / CRM / Core / Exception.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * Base class for exceptions generated by CiviCRM.
14 * This Exception returns more information than the default one. We are using it from the
15 * form layer to allow redirects to occur without having redirects in the BAO
16 * @param string $message
17 * The human friendly error message.
18 * @param string $error_code
19 * A computer friendly error code. By convention, no space (but underscore allowed).
20 * ex: mandatory_missing, duplicate, invalid_format
21 * @param array $data
22 * 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.
23 */
24 class CRM_Core_Exception extends PEAR_Exception {
25 private $errorData = [];
26
27 /**
28 * Class constructor.
29 *
30 * @param string $message
31 * @param int $error_code
32 * @param array $errorData
33 * @param null $previous
34 */
35 public function __construct($message, $error_code = 0, $errorData = [], $previous = NULL) {
36 parent::__construct($message);
37 $this->errorData = $errorData + ['error_code' => $error_code];
38 }
39
40 /**
41 * Custom string representation of object.
42 *
43 * @return string
44 */
45 public function __toString() {
46 return __CLASS__ . ": [{$this->errorData['error_code']}: {$this->message}\n";
47 }
48
49 /**
50 * Get error code.
51 *
52 * @return mixed
53 */
54 public function getErrorCode() {
55 return $this->errorData['error_code'];
56 }
57
58 /**
59 * Return specific error information.
60 *
61 * (Can be used for more detailed error messages or translation.)
62 *
63 * This method may be overridden in child exception classes in order
64 * to add functionality not present in PEAR_Exception and is a placeholder
65 * to define API
66 *
67 * The returned array must be an associative array of parameter => value like so:
68 * <pre>
69 * array('name' => $name, 'context' => array(...))
70 * </pre>
71 * @return array
72 */
73 public function getErrorData() {
74 return $this->errorData;
75 }
76
77 }