# Exception and error handling in SimpleSAMLphp [TOC] This document describes the way errors and exceptions are handled in authentication sources and authentication processing filters. The basic goal is to be able to throw an exception during authentication, and then have that exception transported back to the SP in a way that the SP understands. This means that internal SimpleSAMLphp exceptions must be mapped to transport specific error codes for the various transports that are supported by SimpleSAMLphp. E.g.: When a `\SAML2\Exception\Protocol\NoPassiveException` error is thrown by an authentication processing filter in a SAML 2.0 IdP, we want to map that exception to the `urn:oasis:names:tc:SAML:2.0:status:NoPassive` status code. That status code should then be returned to the SP. ## Throwing exceptions How you throw an exception depends on where you want to throw it from. The simplest case is if you want to throw it during the `authenticate()`-method in an authentication module or during the `process()`-method in a processing filter. In those methods, you can just throw an exception: ```php public function process(array &$state): void { if ($state['something'] === false) { throw new \SimpleSAML\Error\Exception('Something is wrong...'); } } ``` Exceptions thrown at this stage will be caught and delivered to the appropriate error handler. If you want to throw an exception outside of those methods, i.e. after you have done a redirect, you need to use the `\SimpleSAML\Auth\State::throwException()` function: ```php \SimpleSAML\Utils\HTTP::getSelfURLNoQuery(), \SimpleSAML\Auth\State::EXCEPTION_HANDLER_URL => \SimpleSAML\Utils\HTTP::getSelfURLNoQuery(), [...], ] try { $procChain->processState($state); } catch (\SimpleSAML\Error\Exception $e) { /* Handle exception. */ [...]; } #### Note An exception which isn't a subclass of `\SimpleSAML\Error\Exception` will be converted to the `\SimpleSAML\Error\UnserializedException` class. This happens regardless of whether the exception is delivered directly or through the error handler. This is done to be consistent in what the application receives - now it will always receive the same exception, regardless of whether it is delivered directly or through a redirect. ## Custom error show function Optional custom error show function, called from \SimpleSAML\Error\Error::show, is defined with 'errors.show_function' in config.php. Example code for this function, which implements the same functionality as \SimpleSAML\Error\Error::show, looks something like: ```php public static function show(\SimpleSAML\Configuration $config, array $data) { $t = new \SimpleSAML\XHTML\Template($config, 'error.twig', 'errors'); $t->data = array_merge($t->data, $data); $t->send(); exit; } ```