X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=CRM%2FCore%2FError.php;h=56ea895ceaeb1b9e6d8f3a702f9c2f36ff200c07;hb=91d49caed332075154788bea398d08452cb08ff2;hp=ed6be358d103ec9d4dd5ef672e700a73d6bf99bc;hpb=aa1396ed8d1f44605e012c1ec7f8b1e3e928d410;p=civicrm-core.git
diff --git a/CRM/Core/Error.php b/CRM/Core/Error.php
index ed6be358d1..56ea895cea 100644
--- a/CRM/Core/Error.php
+++ b/CRM/Core/Error.php
@@ -2,9 +2,9 @@
/*
+--------------------------------------------------------------------+
- | CiviCRM version 4.4 |
+ | CiviCRM version 4.5 |
+--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2013 |
+ | Copyright CiviCRM LLC (c) 2004-2014 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
@@ -31,7 +31,7 @@
* PEAR_ErrorStack and use that framework
*
* @package CRM
- * @copyright CiviCRM LLC (c) 2004-2013
+ * @copyright CiviCRM LLC (c) 2004-2014
* $Id$
*
*/
@@ -41,13 +41,34 @@ require_once 'PEAR/Exception.php';
require_once 'CRM/Core/Exception.php';
require_once 'Log.php';
+
+/**
+ * Class CRM_Exception
+ */
class CRM_Exception extends PEAR_Exception {
// Redefine the exception so message isn't optional
+ /**
+ * Supported signatures:
+ * - PEAR_Exception(string $message);
+ * - PEAR_Exception(string $message, int $code);
+ * - PEAR_Exception(string $message, Exception $cause);
+ * - PEAR_Exception(string $message, Exception $cause, int $code);
+ * - PEAR_Exception(string $message, PEAR_Error $cause);
+ * - PEAR_Exception(string $message, PEAR_Error $cause, int $code);
+ * - PEAR_Exception(string $message, array $causes);
+ * - PEAR_Exception(string $message, array $causes, int $code);
+ * @param string exception message
+ * @param int|Exception|PEAR_Error|array|null exception cause
+ * @param int|null exception code or null
+ */
public function __construct($message = NULL, $code = 0, Exception$previous = NULL) {
parent::__construct($message, $code, $previous);
}
}
+/**
+ * Class CRM_Core_Error
+ */
class CRM_Core_Error extends PEAR_ErrorStack {
/**
@@ -83,6 +104,12 @@ class CRM_Core_Error extends PEAR_ErrorStack {
/**
* singleton function used to manage this object.
*
+ * @param null $package
+ * @param bool $msgCallback
+ * @param bool $contextCallback
+ * @param bool $throwPEAR_Error
+ * @param string $stackClass
+ *
* @return object
* @static
*/
@@ -94,7 +121,7 @@ class CRM_Core_Error extends PEAR_ErrorStack {
}
/**
- * construcor
+ * constructor
*/
function __construct() {
parent::__construct('CiviCRM');
@@ -106,7 +133,13 @@ class CRM_Core_Error extends PEAR_ErrorStack {
$this->setDefaultCallback(array($this, 'handlePES'));
}
- function getMessages(&$error, $separator = '
') {
+ /**
+ * @param $error
+ * @param string $separator
+ *
+ * @return array|null|string
+ */
+ static public function getMessages(&$error, $separator = '
') {
if (is_a($error, 'CRM_Core_Error')) {
$errors = $error->getErrors();
$message = array();
@@ -119,7 +152,12 @@ class CRM_Core_Error extends PEAR_ErrorStack {
return NULL;
}
- function displaySessionError(&$error, $separator = '
') {
+ /**
+ * Status display function specific to payment processor errors
+ * @param $error
+ * @param string $separator
+ */
+ static function displaySessionError(&$error, $separator = '
') {
$message = self::getMessages($error, $separator);
if ($message) {
$status = ts("Payment Processor Error message") . "{$separator} $message";
@@ -215,6 +253,9 @@ class CRM_Core_Error extends PEAR_ErrorStack {
// this function is used to trap and print errors
// during system initialization time. Hence the error
// message is quite ugly
+ /**
+ * @param $pearError
+ */
public static function simpleHandler($pearError) {
// create the error array
@@ -258,9 +299,14 @@ class CRM_Core_Error extends PEAR_ErrorStack {
/**
* display an error page with an error message describing what happened
*
- * @param string message the error message
- * @param string code the error code if any
- * @param string email the email address to notify of this situation
+ * @param null $message
+ * @param null $code
+ * @param null $email
+ *
+ * @throws Exception
+ * @internal param \message $string the error message
+ * @internal param \code $string the error code if any
+ * @internal param \email $string the email address to notify of this situation
*
* @return void
* @static
@@ -311,6 +357,21 @@ class CRM_Core_Error extends PEAR_ErrorStack {
}
}
+ // If we are in an ajax callback, format output appropriately
+ if (CRM_Utils_Array::value('snippet', $_REQUEST) === CRM_Core_Smarty::PRINT_JSON) {
+ $out = array(
+ 'status' => 'fatal',
+ 'content' => '
See console for backtrace
'; + } + CRM_Core_Session::setStatus($message, ts('Sorry an Error Occured'), 'error'); + CRM_Core_Transaction::forceRollbackIfEnabled(); + CRM_Core_Page_AJAX::returnJsonResponse($out); + } + if ($config->backtrace) { self::backtrace(); } @@ -463,10 +524,15 @@ class CRM_Core_Error extends PEAR_ErrorStack { * Similar to the function debug. Only difference is * in the formatting of the output. * - * @param string variable name - * @param mixed reference to variables that we need a trace of - * @param bool should we use print_r ? (else we use var_dump) - * @param bool should we log or return the output + * @param $variable_name + * @param $variable + * @param bool $print + * @param bool $log + * @param string $comp variable name + * + * @internal param \reference $mixed to variables that we need a trace of + * @internal param \should $bool we use print_r ? (else we use var_dump) + * @internal param \should $bool we log or return the output * * @return string the generated output * @@ -511,9 +577,10 @@ class CRM_Core_Error extends PEAR_ErrorStack { /** * display the error message on terminal * - * @param string message to be output - * @param bool should we log or return the output + * @param $message + * @param bool $out should we log or return the output * + * @param string $comp message to be output * @return string format of the backtrace * * @access public @@ -556,6 +623,8 @@ class CRM_Core_Error extends PEAR_ErrorStack { /** * Obtain a reference to the error log * + * @param string $comp + * * @return Log */ static function createDebugLogger($comp = '') { @@ -586,6 +655,10 @@ class CRM_Core_Error extends PEAR_ErrorStack { return Log::singleton('file', $fileName); } + /** + * @param string $msg + * @param bool $log + */ static function backtrace($msg = 'backTrace', $log = FALSE) { $backTrace = debug_backtrace(); $message = self::formatBacktrace($backTrace); @@ -604,15 +677,32 @@ class CRM_Core_Error extends PEAR_ErrorStack { * @param boolean $showArgs TRUE if we should try to display content of function arguments (which could be sensitive); FALSE to display only the type of each function argument * @param int $maxArgLen maximum number of characters to show from each argument string * @return string printable plain-text - * @see debug_backtrace - * @see Exception::getTrace() */ static function formatBacktrace($backTrace, $showArgs = TRUE, $maxArgLen = 80) { $message = ''; - foreach ($backTrace as $idx => $trace) { + foreach (self::parseBacktrace($backTrace, $showArgs, $maxArgLen) as $idx => $trace) { + $message .= sprintf("#%s %s\n", $idx, $trace); + } + $message .= sprintf("#%s {main}\n", 1+$idx); + return $message; + } + + /** + * Render a backtrace array as an array + * + * @param array $backTrace array of stack frames + * @param boolean $showArgs TRUE if we should try to display content of function arguments (which could be sensitive); FALSE to display only the type of each function argument + * @param int $maxArgLen maximum number of characters to show from each argument string + * @return array + * @see debug_backtrace + * @see Exception::getTrace() + */ + static function parseBacktrace($backTrace, $showArgs = TRUE, $maxArgLen = 80) { + $ret = array(); + foreach ($backTrace as $trace) { $args = array(); $fnName = CRM_Utils_Array::value('function', $trace); - $className = array_key_exists('class', $trace) ? ($trace['class'] . $trace['type']) : ''; + $className = isset($trace['class']) ? ($trace['class'] . $trace['type']) : ''; // do now show args for a few password related functions $skipArgs = ($className == 'DB::' && $fnName == 'connect') ? TRUE : FALSE; @@ -651,9 +741,8 @@ class CRM_Core_Error extends PEAR_ErrorStack { } } - $message .= sprintf( - "#%s %s(%s): %s%s(%s)\n", - $idx, + $ret[] = sprintf( + "%s(%s): %s%s(%s)", CRM_Utils_Array::value('file', $trace, '[internal function]'), CRM_Utils_Array::value('line', $trace, ''), $className, @@ -661,8 +750,7 @@ class CRM_Core_Error extends PEAR_ErrorStack { implode(", ", $args) ); } - $message .= sprintf("#%s {main}\n", 1+$idx); - return $message; + return $ret; } /** @@ -721,6 +809,14 @@ class CRM_Core_Error extends PEAR_ErrorStack { return $msg; } + /** + * @param $message + * @param int $code + * @param string $level + * @param null $params + * + * @return object + */ static function createError($message, $code = 8000, $level = 'Fatal', $params = NULL) { $error = CRM_Core_Error::singleton(); $error->push($code, $level, array($params), $message); @@ -730,8 +826,10 @@ class CRM_Core_Error extends PEAR_ErrorStack { /** * Set a status message in the session, then bounce back to the referrer. * - * @param string $status The status message to set + * @param string $status The status message to set * + * @param null $redirect + * @param string $title * @return void * @access public * @static @@ -742,6 +840,9 @@ class CRM_Core_Error extends PEAR_ErrorStack { $redirect = $session->readUserContext(); } $session->setStatus($status, $title); + if (CRM_Utils_Array::value('snippet', $_REQUEST) === CRM_Core_Smarty::PRINT_JSON) { + CRM_Core_Page_AJAX::returnJsonResponse(array('status' => 'error')); + } CRM_Utils_System::redirect($redirect); } @@ -757,25 +858,21 @@ class CRM_Core_Error extends PEAR_ErrorStack { $error->_errorsByLevel = array(); } - public static function ignoreException($callback = NULL) { - if (!$callback) { - $callback = array('CRM_Core_Error', 'nullHandler'); - } - - $GLOBALS['_PEAR_default_error_mode'] = PEAR_ERROR_CALLBACK; - $GLOBALS['_PEAR_default_error_options'] = $callback; - } - + /** + * PEAR error-handler which converts errors to exceptions + * + * @param $pearError + * @throws PEAR_Exception + */ public static function exceptionHandler($pearError) { CRM_Core_Error::backtrace('backTrace', TRUE); throw new PEAR_Exception($pearError->getMessage(), $pearError); } /** - * Error handler to quietly catch otherwise fatal smtp transport errors. + * PEAR error-handler to quietly catch otherwise fatal errors. Intended for use with smtp transport. * * @param object $obj The PEAR_ERROR object - * * @return object $obj * @access public * @static @@ -786,26 +883,18 @@ class CRM_Core_Error extends PEAR_ErrorStack { return $obj; } - /** - * (Re)set the default callback method - * - * @return void - * @access public - * @static - */ - public static function setCallback($callback = NULL) { - if (!$callback) { - $callback = array('CRM_Core_Error', 'handle'); - } - $GLOBALS['_PEAR_default_error_mode'] = PEAR_ERROR_CALLBACK; - $GLOBALS['_PEAR_default_error_options'] = $callback; - } - /* * @deprecated * This function is no longer used by v3 api. * @fixme Some core files call it but it should be re-thought & renamed or removed */ + /** + * @param $msg + * @param null $data + * + * @return array + * @throws Exception + */ public static function &createAPIError($msg, $data = NULL) { if (self::$modeException) { throw new Exception($msg, $data); @@ -821,6 +910,9 @@ class CRM_Core_Error extends PEAR_ErrorStack { return $values; } + /** + * @param $file + */ public static function movedSiteError($file) { $url = CRM_Utils_System::url('civicrm/admin/setting/updateConfigBackend', 'reset=1', @@ -841,8 +933,14 @@ class CRM_Core_Error extends PEAR_ErrorStack { CRM_Utils_System::civiExit($code); } + /** + * @param $error + * @param const $type + * + * @return bool + */ public static function isAPIError($error, $type = CRM_Core_Error::FATAL_ERROR) { - if (is_array($error) && CRM_Utils_Array::value('is_error', $error)) { + if (is_array($error) && !empty($error['is_error'])) { $code = $error['error_message']['code']; if ($code == $type) { return TRUE;