From e8bc5cb4877bc878286774a144493bfdc5201af8 Mon Sep 17 00:00:00 2001 From: Olav Morken <olav.morken@uninett.no> Date: Mon, 13 Jul 2009 06:16:15 +0000 Subject: [PATCH] Error_Exception: Include the cause of the exception as an parameter. Including the cause of the exception will make it simpler to trace exceptions through the program. git-svn-id: https://simplesamlphp.googlecode.com/svn/trunk@1565 44740490-163a-0410-bde0-09ae8108e29a --- lib/SimpleSAML/Error/Exception.php | 34 ++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/lib/SimpleSAML/Error/Exception.php b/lib/SimpleSAML/Error/Exception.php index ec631baf3..0300b7e61 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. * -- GitLab