diff --git a/lib/SimpleSAML/Error/Exception.php b/lib/SimpleSAML/Error/Exception.php index ec631baf38b628e6c756e260c4ed94492b9498c5..0300b7e616e81389f23972d7d46f43e34b5a3de1 100644 --- a/lib/SimpleSAML/Error/Exception.php +++ b/lib/SimpleSAML/Error/Exception.php @@ -22,18 +22,38 @@ class SimpleSAML_Error_Exception extends Exception { private $backtrace; + /** + * The cause of this exception. + * + * @var SimpleSAML_Error_Exception + */ + private $cause; + + /** * Constructor for this error. * + * Note that the cause will be converted to a SimpleSAML_Error_UnserializableException + * unless it is a subclass of SimpleSAML_Error_Exception. + * * @param string $message Exception message * @param int $code Error code + * @param Exception|NULL $cause The cause of this exception. */ - public function __construct($message, $code = 0) { - assert('is_string($message) || is_int($code)'); + public function __construct($message, $code = 0, Exception $cause = NULL) { + assert('is_string($message)'); + assert('is_int($code)'); parent::__construct($message, $code); $this->backtrace = SimpleSAML_Utilities::buildBacktrace($this); + + if ($cause !== NULL) { + if (!($cause instanceof SimpleSAML_Error_Exception)) { + $cause = new SimpleSAML_Error_UnserializableException($cause); + } + $this->cause = $cause; + } } @@ -62,6 +82,16 @@ class SimpleSAML_Error_Exception extends Exception { } + /** + * Retrieve the cause of this exception. + * + * @return SimpleSAML_Error_Exception|NULL The cause of this exception. + */ + public function getCause() { + return $this->cause; + } + + /** * Function for serialization. *