Exception.php 3.62 KB
Newer Older
totten's avatar
totten committed
1 2 3
<?php
/*
 +--------------------------------------------------------------------+
totten's avatar
totten committed
4
 | CiviCRM version 5                                                  |
totten's avatar
totten committed
5
 +--------------------------------------------------------------------+
Seamus Lee's avatar
Seamus Lee committed
6
 | Copyright CiviCRM LLC (c) 2004-2019                                |
totten's avatar
totten committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 +--------------------------------------------------------------------+
 | This file is a part of CiviCRM.                                    |
 |                                                                    |
 | CiviCRM is free software; you can copy, modify, and distribute it  |
 | under the terms of the GNU Affero General Public License           |
 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
 |                                                                    |
 | CiviCRM is distributed in the hope that it will be useful, but     |
 | WITHOUT ANY WARRANTY; without even the implied warranty of         |
 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
 | See the GNU Affero General Public License for more details.        |
 |                                                                    |
 | You should have received a copy of the GNU Affero General Public   |
 | License and the CiviCRM Licensing Exception along                  |
 | with this program; if not, contact CiviCRM LLC                     |
 | at info[AT]civicrm[DOT]org. If you have questions about the        |
 | GNU Affero General Public License or the licensing of CiviCRM,     |
 | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
 +--------------------------------------------------------------------+
26
 */
totten's avatar
totten committed
27 28 29

/**
 * Base class for exceptions generated by CiviCRM.
30 31
 * This Exception returns more information than the default one. We are using it from the
 * form layer to allow redirects to occur without having redirects in the BAO
32 33 34 35
 * @param string $message
 *   The human friendly error message.
 * @param string $error_code
 *   A computer friendly error code. By convention, no space (but underscore allowed).
36
 *   ex: mandatory_missing, duplicate, invalid_format
37 38
 * @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.
totten's avatar
totten committed
39 40
 */
class CRM_Core_Exception extends PEAR_Exception {
41
  private $errorData = [];
42 43

  /**
eileenmcnaugton's avatar
eileenmcnaugton committed
44 45 46
   * Class constructor.
   *
   * @param string $message
47 48 49 50
   * @param int $error_code
   * @param array $errorData
   * @param null $previous
   */
51
  public function __construct($message, $error_code = 0, $errorData = [], $previous = NULL) {
52
    parent::__construct(ts($message));
53
    $this->errorData = $errorData + ['error_code' => $error_code];
54 55
  }

56
  /**
eileenmcnaugton's avatar
eileenmcnaugton committed
57 58
   * Custom string representation of object.
   *
59 60
   * @return string
   */
61 62 63 64
  public function __toString() {
    return __CLASS__ . ": [{$this->errorData['error_code']}: {$this->message}\n";
  }

eileenmcnaugton's avatar
eileenmcnaugton committed
65 66 67 68 69
  /**
   * Get error code.
   *
   * @return mixed
   */
70 71 72
  public function getErrorCode() {
    return $this->errorData['error_code'];
  }
73 74

  /**
eileenmcnaugton's avatar
eileenmcnaugton committed
75 76 77
   * Return specific error information.
   *
   * (Can be used for more detailed error messages or translation.)
78 79 80 81 82 83 84 85 86 87 88
   *
   * This method may be overridden in child exception classes in order
   * to add functionality not present in PEAR_Exception and is a placeholder
   * to define API
   *
   * The returned array must be an associative array of parameter => value like so:
   * <pre>
   * array('name' => $name, 'context' => array(...))
   * </pre>
   * @return array
   */
89 90 91
  public function getErrorData() {
    return $this->errorData;
  }
92

totten's avatar
totten committed
93
}