diff --git a/lib/SimpleSAML/Error/Error.php b/lib/SimpleSAML/Error/Error.php index a7542c9c8e432995b135b395411683f10bd50146..c8d68591affa077c0b92f642cc7e5a26710a9c7f 100644 --- a/lib/SimpleSAML/Error/Error.php +++ b/lib/SimpleSAML/Error/Error.php @@ -12,10 +12,20 @@ class SimpleSAML_Error_Error extends SimpleSAML_Error_Exception { /** * The error code. + * + * @var string */ private $errorCode; + /** + * The parameters for the error. + * + * @var array + */ + private $parameters; + + /** * The exception which caused this error. */ @@ -33,12 +43,20 @@ class SimpleSAML_Error_Error extends SimpleSAML_Error_Exception { * @param Exception $cause The exception which caused this fatal error (if any). */ public function __construct($errorCode, Exception $cause = NULL) { - assert('is_string($errorCode) || is_array($errorCode)'); if (is_array($errorCode)) { - $msg = $errorCode[0] . '('; - foreach ($errorCode as $k => $v) { + $this->parameters = $errorCode; + unset($this->parameters[0]); + $this->errorCode = $errorCode[0]; + } else { + $parameters = array(); + $this->errorCode = $errorCode; + } + + if (!empty($this->parameters)) { + $msg = $this->errorCode . '('; + foreach ($this->parameters as $k => $v) { if ($k === 0) { continue; } @@ -47,11 +65,10 @@ class SimpleSAML_Error_Error extends SimpleSAML_Error_Exception { } $msg = substr($msg, 0, -2) . ')'; } else { - $msg = $errorCode; + $msg = $this->errorCode; } parent::__construct($msg, -1, $cause); - $this->errorCode = $errorCode; $this->cause = $cause; } @@ -59,7 +76,7 @@ class SimpleSAML_Error_Error extends SimpleSAML_Error_Exception { /** * Retrieve the error code given when throwing this error. * - * @return mixed The error code. + * @return string The error code. */ public function getErrorCode() { return $this->errorCode; @@ -77,77 +94,56 @@ class SimpleSAML_Error_Error extends SimpleSAML_Error_Exception { /** - * Show and log fatal error message. - * - * This function logs a error message to the error log and shows the - * message to the user. Script execution terminates afterwards. - * - * The error code comes from the errors-dictionary. It can optionally include parameters, which - * will be substituted into the output string. + * Save an error report. * - * @param string $trackId The trackid of the user, from $session->getTrackID(). - * @param mixed $errorCode Either a string with the error code, or an array with the error code and - * additional parameters. - * @param Exception $e The exception which caused the error. + * @return array The array with the error report data. */ - private function fatalError($trackId = 'na', $errorCode = null, Exception $e = null) { + protected function saveError() { - $config = SimpleSAML_Configuration::getInstance(); - $session = SimpleSAML_Session::getInstance(); - - if (is_array($errorCode)) { - $parameters = $errorCode; - unset($parameters[0]); - $errorCode = $errorCode[0]; - } else { - $parameters = array(); - } - - // Get the exception message if there is any exception provided. - $emsg = (empty($e) ? 'No exception available' : $e->getMessage()); - $etrace = (empty($e) ? 'No exception available' : SimpleSAML_Utilities::formatBacktrace($e)); - - if (!empty($errorCode) && count($parameters) > 0) { - $reptext = array(); - foreach($parameters as $k => $v) { - $reptext[] = '"' . $k . '"' . ' => "' . $v . '"'; - } - $reptext = '(' . implode(', ', $reptext) . ')'; - $error = $errorCode . $reptext; - } elseif(!empty($errorCode)) { - $error = $errorCode; - } else { - $error = 'na'; - } - - // Log a error message - SimpleSAML_Logger::error($_SERVER['PHP_SELF'].' - UserError: ErrCode:' . $error . ': ' . urlencode($emsg) ); - if (!empty($e)) { - SimpleSAML_Logger::error('Exception: ' . get_class($e)); - SimpleSAML_Logger::error('Backtrace:'); - foreach (explode("\n", $etrace) as $line) { - SimpleSAML_Logger::error($line); - } - } + $data = $this->format(); + $emsg = array_shift($data); + $etrace = implode("\n", $data); $reportId = SimpleSAML_Utilities::stringToHex(SimpleSAML_Utilities::generateRandomBytes(4)); SimpleSAML_Logger::error('Error report with id ' . $reportId . ' generated.'); + $config = SimpleSAML_Configuration::getInstance(); + $session = SimpleSAML_Session::getInstance(); + $errorData = array( 'exceptionMsg' => $emsg, 'exceptionTrace' => $etrace, 'reportId' => $reportId, - 'trackId' => $trackId, + 'trackId' => $session->getTrackID(), 'url' => SimpleSAML_Utilities::selfURLNoQuery(), 'version' => $config->getVersion(), ); $session->setData('core:errorreport', $reportId, $errorData); + return $errorData; + } + + + /** + * Display this error. + * + * This method displays a standard simpleSAMLphp error page and exits. + */ + public function show() { + + $this->setHTTPCode(); + + /* Log the error message. */ + $this->logError(); + + $errorData = $this->saveError(); + + $config = SimpleSAML_Configuration::getInstance(); $t = new SimpleSAML_XHTML_Template($config, 'error.php', 'errors'); $t->data['showerrors'] = $config->getBoolean('showerrors', true); $t->data['error'] = $errorData; - $t->data['errorCode'] = $errorCode; - $t->data['parameters'] = $parameters; + $t->data['errorCode'] = $this->errorCode; + $t->data['parameters'] = $this->parameters; /* Check if there is a valid technical contact email address. */ if($config->getString('technicalcontact_email', 'na@example.org') !== 'na@example.org') { @@ -156,6 +152,7 @@ class SimpleSAML_Error_Error extends SimpleSAML_Error_Exception { $t->data['errorReportAddress'] = $baseurl . 'errorreport.php'; } + $session = SimpleSAML_Session::getInstance(); $attributes = $session->getAttributes(); if (is_array($attributes) && array_key_exists('mail', $attributes) && count($attributes['mail']) > 0) { $email = $attributes['mail'][0]; @@ -168,26 +165,4 @@ class SimpleSAML_Error_Error extends SimpleSAML_Error_Exception { exit; } - - /** - * Display this error. - * - * This method displays a standard simpleSAMLphp error page and exits. - */ - public function show() { - - $this->setHTTPCode(); - - $session = SimpleSAML_Session::getInstance(); - - if($this->cause !== NULL) { - $e = $this->cause; - } else { - $e = $this; - } - - $this->fatalError($session->getTrackID(), $this->errorCode, $e); - - } - }