From 6513d8768a9674d5d4d8937027dca40925bda454 Mon Sep 17 00:00:00 2001 From: Brian Clark Date: Mon, 15 May 2023 12:11:25 -0500 Subject: [PATCH] Making the createFromCognitoException method a bit more robust so that it doesn't completely error in the event you pass it an Exception that is not specifically a CognitoIdentityProviderException. I believe this was the original intent. --- src/Exception/CognitoResponseException.php | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/Exception/CognitoResponseException.php b/src/Exception/CognitoResponseException.php index a3e9726..2b3515f 100644 --- a/src/Exception/CognitoResponseException.php +++ b/src/Exception/CognitoResponseException.php @@ -20,14 +20,19 @@ public function __construct(Throwable $previous = null) * @param CognitoIdentityProviderException $e * @return Exception */ - public static function createFromCognitoException(CognitoIdentityProviderException $e) + public static function createFromCognitoException(Exception $e) { - $errorClass = "pmill\\AwsCognito\\Exception\\" . $e->getAwsErrorCode(); + //If the class is CognitoIdentityProviderException, perform this custom logic + //to get the actual AWS error + if (method_exists($e, 'getAwsErrorCode')) { + $errorClass = "pmill\\AwsCognito\\Exception\\" . $e->getAwsErrorCode(); - if (class_exists($errorClass)) { - return new $errorClass($e); + if (class_exists($errorClass)) { + return new $errorClass($e); + } } + //Otherwise just return the Exception as is return $e; } }