Add own exceptions to the library
While trying to add more meaningful messages to some of the typical errors, I've noticed that at some points in the library we are throwing simple exceptions with a message that's not really descriptive of the real problem, nor even translated. For instance, in \SAML2\SOAP::receive():
$postText = file_get_contents('php://input');
if (empty($postText)) {
throw new Exception('Invalid message received to AssertionConsumerService endpoint.');
}
or in \SAML2\Binding::getCurrentBinding():
throw new Exception('Unable to find the current binding.');
If these were custom exceptions, we could capture them easily and display a meaningful, translated error message to the user, depending on where we are. Therefore I think we should even move some of the exceptions in SimpleSAMLphp to this library, and extend them with specific ones.
Good point!
If only I could find time to do all the stuff I need to do... :cry:
+1
Much work has been done.. Some left;
grep -R "throw new Exception" * --exclude-dir=vendor
Only a few generic exceptions left;
src/SAML2/HTTPPost.php: throw new Exception('Cannot send message, no destination set.');
src/SAML2/HTTPPost.php: throw new Exception('Missing SAMLRequest or SAMLResponse parameter.');
src/SAML2/HTTPRedirect.php: throw new Exception('Cannot build a redirect URL, no destination set.');
src/SAML2/HTTPRedirect.php: throw new Exception('Missing SAMLRequest or SAMLResponse parameter.');
src/SAML2/HTTPRedirect.php: throw new Exception('Unknown SAMLEncoding: ' . var_export($query['SAMLEncoding'], true));
src/SAML2/HTTPRedirect.php: throw new Exception('Error while base64 decoding SAML message.');
src/SAML2/HTTPRedirect.php: throw new Exception('Error while inflating SAML message.');
src/SAML2/HTTPRedirect.php: throw new Exception('Missing signature algorithm.');
src/SAML2/HTTPRedirect.php: throw new Exception('Unable to validate signature on query string.');
src/SAML2/Response/Validation/ConstraintValidator/DestinationMatches.php:
throw new Exception('No destination set in the response.');
src/SAML2/HTTPArtifact.php: throw new Exception('Unable to send artifact without a datastore configured.');
src/SAML2/HTTPArtifact.php: throw new Exception('Cannot get redirect URL, no Issuer set in the message.');
src/SAML2/HTTPArtifact.php: throw new Exception('Cannot get redirect URL, no destination set in the message.');
src/SAML2/HTTPArtifact.php: throw new Exception('Missing SAMLart parameter.');
src/SAML2/HTTPArtifact.php: throw new Exception('No metadata found for remote provider with SHA1 ID: ' . var_export($sourceId, true));
src/SAML2/HTTPArtifact.php: throw new Exception('No ArtifactResolutionService with the correct index.');
src/SAML2/HTTPArtifact.php: throw new Exception('Received error from ArtifactResolutionService.');
src/SAML2/HTTPArtifact.php: throw new Exception('Empty ArtifactResponse received, maybe a replay?');
src/SAML2/SOAP.php: throw new Exception('No destination available for SOAP message.');
src/SAML2/SOAP.php: throw new Exception('Error while generating XML for SAML message.');
src/SAML2/SOAPClient.php: throw new Exception('Cannot send SOAP message, no destination set.');
src/SAML2/SOAPClient.php: throw new Exception('Empty SOAP response, check peer certificate.');
src/SAML2/SOAPClient.php: throw new Exception(
src/SAML2/SOAPClient.php: throw new Exception('Unable to get key details from XMLSecurityKey.');
src/SAML2/SOAPClient.php: throw new Exception('Missing key in public key details.');
These are the classes used for SAML2 bindings and still need work for the new v5.0 release
Update:
src/SAML2/Response/Validation/ConstraintValidator/DestinationMatches.php: throw new Exception('No destination set in the response.');
src/SAML2/HTTPArtifact.php: throw new Exception('Unable to send artifact without a datastore configured.');
src/SAML2/HTTPArtifact.php: throw new Exception('Cannot get redirect URL, no Issuer set in the message.');
src/SAML2/HTTPArtifact.php: throw new Exception('Cannot get redirect URL, no destination set in the message.');
src/SAML2/HTTPArtifact.php: throw new Exception('Missing SAMLart parameter.');
src/SAML2/HTTPArtifact.php: throw new Exception('No metadata found for remote provider with SHA1 ID: ' . var_export($sourceId, true));
src/SAML2/HTTPArtifact.php: throw new Exception('No ArtifactResolutionService with the correct index.');
src/SAML2/HTTPArtifact.php: throw new Exception('Received error from ArtifactResolutionService.');
src/SAML2/HTTPArtifact.php: throw new Exception('Empty ArtifactResponse received, maybe a replay?');
src/SAML2/HTTPPost.php: throw new Exception('Cannot send message, no destination set.');
src/SAML2/HTTPPost.php: throw new Exception('Missing SAMLRequest or SAMLResponse parameter.');
src/SAML2/HTTPRedirect.php: throw new Exception('Cannot build a redirect URL, no destination set.');
src/SAML2/HTTPRedirect.php: throw new Exception('Missing SAMLRequest or SAMLResponse parameter.');
src/SAML2/HTTPRedirect.php: throw new Exception(sprintf('Unknown SAMLEncoding: %s', $query['SAMLEncoding']));
src/SAML2/HTTPRedirect.php: throw new Exception('Error while base64 decoding SAML message.');
src/SAML2/HTTPRedirect.php: throw new Exception('Error while inflating SAML message.');
src/SAML2/HTTPRedirect.php: throw new Exception('Missing signature algorithm.');
src/SAML2/SOAP.php: throw new Exception('No destination available for SOAP message.');
src/SAML2/SOAPClient.php: throw new Exception('Cannot send SOAP message, no destination set.');
src/SAML2/SOAPClient.php: throw new Exception('Empty SOAP response, check peer certificate.');
src/SAML2/SOAPClient.php: throw new Exception(
src/SAML2/SOAPClient.php: throw new Exception('Unable to get key details from XMLSecurityKey.');
src/SAML2/SOAPClient.php: throw new Exception('Missing key in public key details.');