Skip to content
Snippets Groups Projects
Unverified Commit d7f99a3f authored by Tim van Dijen's avatar Tim van Dijen Committed by GitHub
Browse files

Improve error handling (#1365)

Improve error handling
parent cc7ef745
No related branches found
No related tags found
No related merge requests found
Showing with 53 additions and 46 deletions
...@@ -5,6 +5,7 @@ declare(strict_types=1); ...@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace SimpleSAML\Error; namespace SimpleSAML\Error;
use SimpleSAML\Assert\Assert; use SimpleSAML\Assert\Assert;
use Throwable;
/** /**
* Baseclass for auth source exceptions. * Baseclass for auth source exceptions.
...@@ -33,9 +34,9 @@ class AuthSource extends Error ...@@ -33,9 +34,9 @@ class AuthSource extends Error
* *
* @param string $authsource Authsource module name from where this error was thrown. * @param string $authsource Authsource module name from where this error was thrown.
* @param string $reason Description of the error. * @param string $reason Description of the error.
* @param \Exception|null $cause * @param \Throwable|null $cause
*/ */
public function __construct(string $authsource, string $reason, \Exception $cause = null) public function __construct(string $authsource, string $reason, Throwable $cause = null)
{ {
$this->authsource = $authsource; $this->authsource = $authsource;
$this->reason = $reason; $this->reason = $reason;
......
...@@ -7,6 +7,7 @@ namespace SimpleSAML\Error; ...@@ -7,6 +7,7 @@ namespace SimpleSAML\Error;
use SimpleSAML\Configuration; use SimpleSAML\Configuration;
use SimpleSAML\Logger; use SimpleSAML\Logger;
use SimpleSAML\Utils; use SimpleSAML\Utils;
use Throwable;
/** /**
* This exception represents a configuration error that we cannot recover from. * This exception represents a configuration error that we cannot recover from.
...@@ -66,11 +67,11 @@ class CriticalConfigurationError extends ConfigurationError ...@@ -66,11 +67,11 @@ class CriticalConfigurationError extends ConfigurationError
/** /**
* @param \Exception $exception * @param \Throwable $exception
* *
* @return \SimpleSAML\Error\Exception * @return \SimpleSAML\Error\Exception
*/ */
public static function fromException(\Exception $exception): Exception public static function fromException(Throwable $exception): Exception
{ {
$reason = null; $reason = null;
$file = null; $file = null;
......
...@@ -10,6 +10,7 @@ use SimpleSAML\Logger; ...@@ -10,6 +10,7 @@ use SimpleSAML\Logger;
use SimpleSAML\Session; use SimpleSAML\Session;
use SimpleSAML\Utils; use SimpleSAML\Utils;
use SimpleSAML\XHTML\Template; use SimpleSAML\XHTML\Template;
use Throwable;
/** /**
* Class that wraps SimpleSAMLphp errors in exceptions. * Class that wraps SimpleSAMLphp errors in exceptions.
...@@ -76,11 +77,11 @@ class Error extends Exception ...@@ -76,11 +77,11 @@ class Error extends Exception
* The error can either be given as a string, or as an array. If it is an array, the first element in the array * The error can either be given as a string, or as an array. If it is an array, the first element in the array
* (with index 0), is the error code, while the other elements are replacements for the error text. * (with index 0), is the error code, while the other elements are replacements for the error text.
* *
* @param mixed $errorCode One of the error codes defined in the errors dictionary. * @param mixed $errorCode One of the error codes defined in the errors dictionary.
* @param \Exception $cause The exception which caused this fatal error (if any). Optional. * @param \Throwable $cause The exception which caused this fatal error (if any). Optional.
* @param int|null $httpCode The HTTP response code to use. Optional. * @param int|null $httpCode The HTTP response code to use. Optional.
*/ */
public function __construct($errorCode, \Exception $cause = null, ?int $httpCode = null) public function __construct($errorCode, Throwable $cause = null, ?int $httpCode = null)
{ {
Assert::true(is_string($errorCode) || is_array($errorCode)); Assert::true(is_string($errorCode) || is_array($errorCode));
......
...@@ -7,6 +7,7 @@ namespace SimpleSAML\Error; ...@@ -7,6 +7,7 @@ namespace SimpleSAML\Error;
use SimpleSAML\Assert\Assert; use SimpleSAML\Assert\Assert;
use SimpleSAML\Configuration; use SimpleSAML\Configuration;
use SimpleSAML\Logger; use SimpleSAML\Logger;
use Throwable;
/** /**
* Base class for SimpleSAMLphp Exceptions * Base class for SimpleSAMLphp Exceptions
...@@ -45,9 +46,9 @@ class Exception extends \Exception ...@@ -45,9 +46,9 @@ class Exception extends \Exception
* *
* @param string $message Exception message * @param string $message Exception message
* @param int $code Error code * @param int $code Error code
* @param \Exception|null $cause The cause of this exception. * @param \Throwable|null $cause The cause of this exception.
*/ */
public function __construct(string $message, int $code = 0, \Exception $cause = null) public function __construct(string $message, int $code = 0, Throwable $cause = null)
{ {
parent::__construct($message, $code); parent::__construct($message, $code);
...@@ -62,11 +63,11 @@ class Exception extends \Exception ...@@ -62,11 +63,11 @@ class Exception extends \Exception
/** /**
* Convert any exception into a \SimpleSAML\Error\Exception. * Convert any exception into a \SimpleSAML\Error\Exception.
* *
* @param \Exception $e The exception. * @param \Throwable $e The exception.
* *
* @return \SimpleSAML\Error\Exception The new exception. * @return \SimpleSAML\Error\Exception The new exception.
*/ */
public static function fromException(\Exception $e): Exception public static function fromException(Throwable $e): Exception
{ {
if ($e instanceof Exception) { if ($e instanceof Exception) {
return $e; return $e;
...@@ -78,10 +79,10 @@ class Exception extends \Exception ...@@ -78,10 +79,10 @@ class Exception extends \Exception
/** /**
* Load the backtrace from the given exception. * Load the backtrace from the given exception.
* *
* @param \Exception $exception The exception we should fetch the backtrace from. * @param \Throwable $exception The exception we should fetch the backtrace from.
* @return void * @return void
*/ */
protected function initBacktrace(\Exception $exception): void protected function initBacktrace(Throwable $exception): void
{ {
$this->backtrace = []; $this->backtrace = [];
...@@ -121,9 +122,9 @@ class Exception extends \Exception ...@@ -121,9 +122,9 @@ class Exception extends \Exception
/** /**
* Retrieve the cause of this exception. * Retrieve the cause of this exception.
* *
* @return \SimpleSAML\Error\Exception|null The cause of this exception. * @return \Throwable|null The cause of this exception.
*/ */
public function getCause(): ?Exception public function getCause(): ?Throwable
{ {
return $this->cause; return $this->cause;
} }
......
...@@ -5,6 +5,7 @@ declare(strict_types=1); ...@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace SimpleSAML\Error; namespace SimpleSAML\Error;
use PDOException; use PDOException;
use Throwable;
/** /**
* Class for saving normal exceptions for serialization. * Class for saving normal exceptions for serialization.
...@@ -32,9 +33,9 @@ class UnserializableException extends Exception ...@@ -32,9 +33,9 @@ class UnserializableException extends Exception
/** /**
* Create a serializable exception representing an unserializable exception. * Create a serializable exception representing an unserializable exception.
* *
* @param \Exception $original The original exception. * @param \Throwable $original The original exception.
*/ */
public function __construct(\Exception $original) public function __construct(Throwable $original)
{ {
$this->class = get_class($original); $this->class = get_class($original);
......
...@@ -4,6 +4,8 @@ declare(strict_types=1); ...@@ -4,6 +4,8 @@ declare(strict_types=1);
namespace SimpleSAML\Error; namespace SimpleSAML\Error;
use Throwable;
/** /**
* Exception indicating user aborting the authentication process. * Exception indicating user aborting the authentication process.
* *
...@@ -15,9 +17,9 @@ class UserAborted extends Error ...@@ -15,9 +17,9 @@ class UserAborted extends Error
/** /**
* Create the error * Create the error
* *
* @param \Exception|null $cause The exception that caused this error. * @param \Throwable|null $cause The exception that caused this error.
*/ */
public function __construct(\Exception $cause = null) public function __construct(Throwable $cause = null)
{ {
parent::__construct('USERABORTED', $cause); parent::__construct('USERABORTED', $cause);
} }
......
...@@ -59,12 +59,10 @@ class XML ...@@ -59,12 +59,10 @@ class XML
$enabled = Configuration::getInstance()->getBoolean('debug.validatexml', false); $enabled = Configuration::getInstance()->getBoolean('debug.validatexml', false);
if ( if (
!(in_array('validatexml', $debug, true) // implicitly enabled !(
|| (array_key_exists('validatexml', $debug) in_array('validatexml', $debug, true)
&& $debug['validatexml'] === true) || (array_key_exists('validatexml', $debug) && ($debug['validatexml'] === true))
// explicitly enabled )
// TODO: deprecate this option and remove it in 2.0
|| $enabled) // old 'debug.validatexml' configuration option
) { ) {
// XML validation is disabled // XML validation is disabled
return; return;
......
...@@ -6,6 +6,7 @@ namespace SimpleSAML\Module\saml; ...@@ -6,6 +6,7 @@ namespace SimpleSAML\Module\saml;
use SAML2\Constants; use SAML2\Constants;
use SimpleSAML\Assert\Assert; use SimpleSAML\Assert\Assert;
use Throwable;
/** /**
* Class for representing a SAML 2 error. * Class for representing a SAML 2 error.
...@@ -45,13 +46,13 @@ class Error extends \SimpleSAML\Error\Exception ...@@ -45,13 +46,13 @@ class Error extends \SimpleSAML\Error\Exception
* Can be NULL, in which case there is no second-level status code. * Can be NULL, in which case there is no second-level status code.
* @param string|null $statusMessage The status message. * @param string|null $statusMessage The status message.
* Can be NULL, in which case there is no status message. * Can be NULL, in which case there is no status message.
* @param \Exception|null $cause The cause of this exception. Can be NULL. * @param \Throwable|null $cause The cause of this exception. Can be NULL.
*/ */
public function __construct( public function __construct(
string $status, string $status,
string $subStatus = null, string $subStatus = null,
string $statusMessage = null, string $statusMessage = null,
\Exception $cause = null Throwable $cause = null
) { ) {
$st = self::shortStatus($status); $st = self::shortStatus($status);
if ($subStatus !== null) { if ($subStatus !== null) {
...@@ -107,10 +108,10 @@ class Error extends \SimpleSAML\Error\Exception ...@@ -107,10 +108,10 @@ class Error extends \SimpleSAML\Error\Exception
* This function attempts to create a SAML2 error with the appropriate * This function attempts to create a SAML2 error with the appropriate
* status codes from an arbitrary exception. * status codes from an arbitrary exception.
* *
* @param \Exception $exception The original exception. * @param \Throwable $exception The original exception.
* @return \SimpleSAML\Error\Exception The new exception. * @return \SimpleSAML\Error\Exception The new exception.
*/ */
public static function fromException(\Exception $exception): \SimpleSAML\Error\Exception public static function fromException(Throwable $exception): \SimpleSAML\Error\Exception
{ {
if ($exception instanceof \SimpleSAML\Module\saml\Error) { if ($exception instanceof \SimpleSAML\Module\saml\Error) {
// Return the original exception unchanged // Return the original exception unchanged
......
...@@ -5,6 +5,7 @@ declare(strict_types=1); ...@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace SimpleSAML\Module\saml\Error; namespace SimpleSAML\Module\saml\Error;
use SAML2\Constants; use SAML2\Constants;
use Throwable;
/** /**
* A SAML error indicating that none of the requested Authentication Contexts can be used. * A SAML error indicating that none of the requested Authentication Contexts can be used.
...@@ -21,9 +22,9 @@ class NoAuthnContext extends \SimpleSAML\Module\saml\Error ...@@ -21,9 +22,9 @@ class NoAuthnContext extends \SimpleSAML\Module\saml\Error
* - \SAML2\Constants::STATUS_RESPONDER: in case the error is caused by this SAML responder. * - \SAML2\Constants::STATUS_RESPONDER: in case the error is caused by this SAML responder.
* - \SAML2\Constants::STATUS_REQUESTER: in case the error is caused by the SAML requester. * - \SAML2\Constants::STATUS_REQUESTER: in case the error is caused by the SAML requester.
* @param string|null $message A short message explaining why this error happened. * @param string|null $message A short message explaining why this error happened.
* @param \Exception|null $cause An exception that caused this error. * @param \Throwable|null $cause An exception that caused this error.
*/ */
public function __construct(string $responsible, string $message = null, \Exception $cause = null) public function __construct(string $responsible, string $message = null, Throwable $cause = null)
{ {
parent::__construct($responsible, Constants::STATUS_NO_AUTHN_CONTEXT, $message, $cause); parent::__construct($responsible, Constants::STATUS_NO_AUTHN_CONTEXT, $message, $cause);
} }
......
...@@ -5,6 +5,7 @@ declare(strict_types=1); ...@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace SimpleSAML\Module\saml\Error; namespace SimpleSAML\Module\saml\Error;
use SAML2\Constants; use SAML2\Constants;
use Throwable;
/** /**
* A SAML error indicating that none of the requested IdPs can be used. * A SAML error indicating that none of the requested IdPs can be used.
...@@ -21,9 +22,9 @@ class NoAvailableIDP extends \SimpleSAML\Module\saml\Error ...@@ -21,9 +22,9 @@ class NoAvailableIDP extends \SimpleSAML\Module\saml\Error
* - \SAML2\Constants::STATUS_RESPONDER: in case the error is caused by this SAML responder. * - \SAML2\Constants::STATUS_RESPONDER: in case the error is caused by this SAML responder.
* - \SAML2\Constants::STATUS_REQUESTER: in case the error is caused by the SAML requester. * - \SAML2\Constants::STATUS_REQUESTER: in case the error is caused by the SAML requester.
* @param string|null $message A short message explaining why this error happened. * @param string|null $message A short message explaining why this error happened.
* @param \Exception|null $cause An exception that caused this error. * @param \Throwable|null $cause An exception that caused this error.
*/ */
public function __construct(string $responsible, string $message = null, \Exception $cause = null) public function __construct(string $responsible, string $message = null, Throwable $cause = null)
{ {
parent::__construct($responsible, Constants::STATUS_NO_AVAILABLE_IDP, $message, $cause); parent::__construct($responsible, Constants::STATUS_NO_AVAILABLE_IDP, $message, $cause);
} }
......
...@@ -5,6 +5,7 @@ declare(strict_types=1); ...@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace SimpleSAML\Module\saml\Error; namespace SimpleSAML\Module\saml\Error;
use SAML2\Constants; use SAML2\Constants;
use Throwable;
/** /**
* A SAML error indicating that passive authentication cannot be used. * A SAML error indicating that passive authentication cannot be used.
...@@ -21,9 +22,9 @@ class NoPassive extends \SimpleSAML\Module\saml\Error ...@@ -21,9 +22,9 @@ class NoPassive extends \SimpleSAML\Module\saml\Error
* - \SAML2\Constants::STATUS_RESPONDER: in case the error is caused by this SAML responder. * - \SAML2\Constants::STATUS_RESPONDER: in case the error is caused by this SAML responder.
* - \SAML2\Constants::STATUS_REQUESTER: in case the error is caused by the SAML requester. * - \SAML2\Constants::STATUS_REQUESTER: in case the error is caused by the SAML requester.
* @param string|null $message A short message explaining why this error happened. * @param string|null $message A short message explaining why this error happened.
* @param \Exception|null $cause An exception that caused this error. * @param \Throwable|null $cause An exception that caused this error.
*/ */
public function __construct(string $responsible, string $message = null, \Exception $cause = null) public function __construct(string $responsible, string $message = null, Throwable $cause = null)
{ {
parent::__construct($responsible, Constants::STATUS_NO_PASSIVE, $message, $cause); parent::__construct($responsible, Constants::STATUS_NO_PASSIVE, $message, $cause);
} }
......
...@@ -5,6 +5,7 @@ declare(strict_types=1); ...@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace SimpleSAML\Module\saml\Error; namespace SimpleSAML\Module\saml\Error;
use SAML2\Constants; use SAML2\Constants;
use Throwable;
/** /**
* A SAML error indicating that none of the IdPs requested are supported. * A SAML error indicating that none of the IdPs requested are supported.
...@@ -21,9 +22,9 @@ class NoSupportedIDP extends \SimpleSAML\Module\saml\Error ...@@ -21,9 +22,9 @@ class NoSupportedIDP extends \SimpleSAML\Module\saml\Error
* - \SAML2\Constants::STATUS_RESPONDER: in case the error is caused by this SAML responder. * - \SAML2\Constants::STATUS_RESPONDER: in case the error is caused by this SAML responder.
* - \SAML2\Constants::STATUS_REQUESTER: in case the error is caused by the SAML requester. * - \SAML2\Constants::STATUS_REQUESTER: in case the error is caused by the SAML requester.
* @param string|null $message A short message explaining why this error happened. * @param string|null $message A short message explaining why this error happened.
* @param \Exception|null $cause An exception that caused this error. * @param \Throwable|null $cause An exception that caused this error.
*/ */
public function __construct(string $responsible, string $message = null, \Exception $cause = null) public function __construct(string $responsible, string $message = null, Throwable $cause = null)
{ {
parent::__construct($responsible, Constants::STATUS_NO_SUPPORTED_IDP, $message, $cause); parent::__construct($responsible, Constants::STATUS_NO_SUPPORTED_IDP, $message, $cause);
} }
......
...@@ -5,6 +5,7 @@ declare(strict_types=1); ...@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace SimpleSAML\Module\saml\Error; namespace SimpleSAML\Module\saml\Error;
use SAML2\Constants; use SAML2\Constants;
use Throwable;
/** /**
* A SAML error indicating that the maximum amount of proxies traversed has been reached. * A SAML error indicating that the maximum amount of proxies traversed has been reached.
...@@ -21,9 +22,9 @@ class ProxyCountExceeded extends \SimpleSAML\Module\saml\Error ...@@ -21,9 +22,9 @@ class ProxyCountExceeded extends \SimpleSAML\Module\saml\Error
* - \SAML2\Constants::STATUS_RESPONDER: in case the error is caused by this SAML responder. * - \SAML2\Constants::STATUS_RESPONDER: in case the error is caused by this SAML responder.
* - \SAML2\Constants::STATUS_REQUESTER: in case the error is caused by the SAML requester. * - \SAML2\Constants::STATUS_REQUESTER: in case the error is caused by the SAML requester.
* @param string|null $message A short message explaining why this error happened. * @param string|null $message A short message explaining why this error happened.
* @param \Exception|null $cause An exception that caused this error. * @param \Throwable|null $cause An exception that caused this error.
*/ */
public function __construct(string $responsible, string $message = null, \Exception $cause = null) public function __construct(string $responsible, string $message = null, Throwable $cause = null)
{ {
parent::__construct($responsible, Constants::STATUS_PROXY_COUNT_EXCEEDED, $message, $cause); parent::__construct($responsible, Constants::STATUS_PROXY_COUNT_EXCEEDED, $message, $cause);
} }
......
...@@ -17,12 +17,8 @@ function SimpleSAML_exception_handler($exception) ...@@ -17,12 +17,8 @@ function SimpleSAML_exception_handler($exception)
$e = new \SimpleSAML\Error\Error('UNHANDLEDEXCEPTION', $exception); $e = new \SimpleSAML\Error\Error('UNHANDLEDEXCEPTION', $exception);
$e->show(); $e->show();
} elseif (class_exists('Error') && $exception instanceof \Error) { } elseif (class_exists('Error') && $exception instanceof \Error) {
$code = $exception->getCode(); $e = new \SimpleSAML\Error\Error('UNHANDLEDEXCEPTION', $exception);
$errno = ($code > 0) ? $code : E_ERROR; $e->show();
$errstr = $exception->getMessage();
$errfile = $exception->getFile();
$errline = $exception->getLine();
SimpleSAML_error_handler($errno, $errstr, $errfile, $errline);
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment