Skip to content
Snippets Groups Projects
Commit 4eef4f2d authored by Olav Morken's avatar Olav Morken
Browse files

SimpleSAML_Error_Error::fatalError(): Simplify.

- Split out saveError(), for reuse by NoState error.
- Change to use internal logging functions and backtrace generation.
- Move fatalError() code into show().

git-svn-id: https://simplesamlphp.googlecode.com/svn/trunk@2481 44740490-163a-0410-bde0-09ae8108e29a
parent 9c9ebad2
No related branches found
No related tags found
No related merge requests found
......@@ -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);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment