diff --git a/lib/SimpleSAML/Error/Exception.php b/lib/SimpleSAML/Error/Exception.php index 761665ae301c09f89afc4029d1d969756b940872..6e0da80085cf8cca339b6e15fb22c02705f832a8 100644 --- a/lib/SimpleSAML/Error/Exception.php +++ b/lib/SimpleSAML/Error/Exception.php @@ -46,7 +46,7 @@ class SimpleSAML_Error_Exception extends Exception { parent::__construct($message, $code); - $this->backtrace = SimpleSAML_Utilities::buildBacktrace($this); + $this->initBacktrace($this); if ($cause !== NULL) { $this->cause = SimpleSAML_Error_Exception::fromException($cause); @@ -70,27 +70,44 @@ class SimpleSAML_Error_Exception extends Exception { /** - * Retrieve the backtrace. + * Load the backtrace from the given exception. * - * @return array An array where each function call is a single item. + * @param Exception $exception The exception we should fetch the backtrace from. */ - public function getBacktrace() { - return $this->backtrace; + protected function initBacktrace(Exception $exception) { + + $this->backtrace = array(); + + /* Position in the top function on the stack. */ + $pos = $exception->getFile() . ':' . $exception->getLine(); + + foreach($exception->getTrace() as $t) { + + $function = $t['function']; + if(array_key_exists('class', $t)) { + $function = $t['class'] . '::' . $function; + } + + $this->backtrace[] = $pos . ' (' . $function . ')'; + + if(array_key_exists('file', $t)) { + $pos = $t['file'] . ':' . $t['line']; + } else { + $pos = '[builtin]'; + } + } + + $this->backtrace[] = $pos . ' (N/A)'; } /** - * 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. + * Retrieve the backtrace. * - * @param array $backtrace The new backtrace. + * @return array An array where each function call is a single item. */ - protected function setBacktrace($backtrace) { - assert('is_array($backtrace)'); - - $this->backtrace = $backtrace; + public function getBacktrace() { + return $this->backtrace; } diff --git a/lib/SimpleSAML/Error/UnserializableException.php b/lib/SimpleSAML/Error/UnserializableException.php index 00474404938c0f1d1b7f91e219ca2c61f6ae56e0..bbe0a27a3ceb6c0efc7dbe28e0bd6d7c3042cb05 100644 --- a/lib/SimpleSAML/Error/UnserializableException.php +++ b/lib/SimpleSAML/Error/UnserializableException.php @@ -40,8 +40,7 @@ class SimpleSAML_Error_UnserializableException extends SimpleSAML_Error_Exceptio } parent::__construct($msg, $code); - - $this->setBacktrace(SimpleSAML_Utilities::buildBacktrace($original)); + $this->initBacktrace($original); } diff --git a/lib/SimpleSAML/Utilities.php b/lib/SimpleSAML/Utilities.php index 6624207ebb79ebc4b6abb07234584e0a3aa8cad8..c834ef0d0119a0f263489f493e0720755ed4cc62 100644 --- a/lib/SimpleSAML/Utilities.php +++ b/lib/SimpleSAML/Utilities.php @@ -261,51 +261,6 @@ class SimpleSAML_Utilities { } - /** - * Build a backtrace. - * - * This function takes in an exception and optionally a start depth, and - * builds a backtrace from that depth. The backtrace is returned as an - * array of strings, where each string represents one level in the stack. - * - * @param Exception $exception The exception. - * @param int $startDepth The depth we should print the backtrace from. - * @return array The backtrace as an array of strings. - */ - public static function buildBacktrace(Exception $exception, $startDepth = 0) { - - assert('is_int($startDepth)'); - - $bt = array(); - - /* Position in the top function on the stack. */ - $pos = $exception->getFile() . ':' . $exception->getLine(); - - foreach($exception->getTrace() as $t) { - - $function = $t['function']; - if(array_key_exists('class', $t)) { - $function = $t['class'] . '::' . $function; - } - - $bt[] = $pos . ' (' . $function . ')'; - - if(array_key_exists('file', $t)) { - $pos = $t['file'] . ':' . $t['line']; - } else { - $pos = '[builtin]'; - } - } - - $bt[] = $pos . ' (N/A)'; - - /* Remove $startDepth elements from the top of the backtrace. */ - $bt = array_slice($bt, $startDepth); - - return $bt; - } - - /* This function converts a SAML2 timestamp on the form * yyyy-mm-ddThh:mm:ss(\.s+)?Z to a UNIX timestamp. The sub-second * part is ignored.