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

Error_Exception: Extend to support serialization.

Make the SimpleSAML_Error_Exception class suitable for serialization by
making it _not_ serialize the trace variable in the Exception class.

Serializing the trace-variable is problematic because it contains the
parameters to all function calls. This can make the exception very big.
This can also make the exception unserializable, if one of the
parameters is unserializable.

git-svn-id: https://simplesamlphp.googlecode.com/svn/trunk@1563 44740490-163a-0410-bde0-09ae8108e29a
parent 254bcd26
No related branches found
No related tags found
No related merge requests found
...@@ -2,13 +2,26 @@ ...@@ -2,13 +2,26 @@
/** /**
* Baseclass for simpleSAML Exceptions * Baseclass for simpleSAML Exceptions
* *
* This class tries to make sure that every exception is serializable.
*
* @author Thomas Graff <thomas.graff@uninett.no> * @author Thomas Graff <thomas.graff@uninett.no>
* @package simpleSAMLphp_base * @package simpleSAMLphp_base
* @version $Id$ * @version $Id$
*/ */
class SimpleSAML_Error_Exception extends Exception { class SimpleSAML_Error_Exception extends Exception {
/**
* The backtrace for this exception.
*
* We need to save the backtrace, since we cannot rely on
* serializing the Exception::trace-variable.
*
* @var string
*/
private $backtrace;
/** /**
* Constructor for this error. * Constructor for this error.
* *
...@@ -17,32 +30,61 @@ class SimpleSAML_Error_Exception extends Exception { ...@@ -17,32 +30,61 @@ class SimpleSAML_Error_Exception extends Exception {
*/ */
public function __construct($message, $code = 0) { public function __construct($message, $code = 0) {
assert('is_string($message) || is_int($code)'); assert('is_string($message) || is_int($code)');
parent::__construct($message, $code); parent::__construct($message, $code);
$this->backtrace = SimpleSAML_Utilities::buildBacktrace($this);
} }
/** /**
* Set the HTTP return code for this error. * Retrieve the backtrace.
* *
* This should be overridden by subclasses who want a different return code than 500 Internal Server Error. * @return array An array where each function call is a single item.
*/ */
protected function setHTTPCode() { public function getBacktrace() {
header('HTTP/1.0 500 Internal Server Error'); return $this->backtrace;
} }
/** /**
* Display this error. * Replace the backtrace.
*
* This function is meant for subclasses which needs to replace the backtrace
* of this exception, such as the SimpleSAML_Error_Unserializable class.
* *
* This method displays a standard simpleSAMLphp error page and exits. * @param array $backtrace The new backtrace.
*/ */
public function show() { protected function setBacktrace($backtrace) {
$this->setHTTPCode(); assert('is_array($backtrace)');
$session = SimpleSAML_Session::getInstance();
$e = $this; $this->backtrace = $backtrace;
SimpleSAML_Utilities::fatalError($session->getTrackID(), $this->errorCode, $e);
} }
/**
* Function for serialization.
*
* This function builds a list of all variables which should be serialized.
* It will serialize all variables except the Exception::trace variable.
*
* @return array Array with the variables which should be serialized.
*/
public function __sleep() {
$ret = array();
$ret = array_keys((array)$this);
foreach ($ret as $i => $e) {
if ($e === "\0Exception\0trace") {
unset($ret[$i]);
}
}
return $ret;
}
} }
?> ?>
\ No newline at end of file
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