From 8a86bd04a69ab76a7d38c755f0a69d2b5a30e774 Mon Sep 17 00:00:00 2001 From: netkas Date: Fri, 31 Jan 2025 15:03:52 -0500 Subject: [PATCH] Improved exception handling --- .../Settings/SettingsDeleteOtp.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Socialbox/Classes/StandardMethods/Settings/SettingsDeleteOtp.php b/src/Socialbox/Classes/StandardMethods/Settings/SettingsDeleteOtp.php index c6d504c..686ae8f 100644 --- a/src/Socialbox/Classes/StandardMethods/Settings/SettingsDeleteOtp.php +++ b/src/Socialbox/Classes/StandardMethods/Settings/SettingsDeleteOtp.php @@ -8,6 +8,7 @@ use Socialbox\Classes\Cryptography; use Socialbox\Enums\StandardError; use Socialbox\Exceptions\DatabaseOperationException; + use Socialbox\Exceptions\Standard\InvalidRpcArgumentException; use Socialbox\Exceptions\Standard\StandardRpcException; use Socialbox\Interfaces\SerializableInterface; use Socialbox\Managers\OneTimePasswordManager; @@ -24,16 +25,15 @@ { if(Configuration::getRegistrationConfiguration()->isOtpRequired()) { - return $rpcRequest->produceError(StandardError::METHOD_NOT_ALLOWED, 'One Time Password is required for this server'); + return $rpcRequest->produceError(StandardError::FORBIDDEN, 'One Time Password is required for this server'); } - $peer = $request->getPeer(); - try { + $peer = $request->getPeer(); if (!OneTimePasswordManager::usesOtp($peer->getUuid())) { - return $rpcRequest->produceError(StandardError::METHOD_NOT_ALLOWED, "Cannot delete One Time Password when none is set"); + return $rpcRequest->produceResponse(false); } } catch (DatabaseOperationException $e) @@ -55,12 +55,12 @@ { if(!$rpcRequest->containsParameter('password')) { - return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, 'When a password is set, the current password must be provided to delete an OTP'); + throw new InvalidRpcArgumentException('password', 'When a password is set, the current password must be provided to delete an OTP'); } if(!Cryptography::validateSha512($rpcRequest->getParameter('password'))) { - return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, 'The provided password is not a valid SHA-512 hash'); + throw new InvalidRpcArgumentException('password', 'The provided password is not a valid SHA-512 hash'); } try @@ -70,7 +70,7 @@ return $rpcRequest->produceError(StandardError::FORBIDDEN, 'The provided password is incorrect'); } } - catch(Exception $e) + catch(DatabaseOperationException $e) { throw new StandardRpcException('Failed to verify password due to an internal exception', StandardError::INTERNAL_SERVER_ERROR, $e); } @@ -81,7 +81,7 @@ // Delete the OTP OneTimePasswordManager::deleteOtp($peer); } - catch(Exception $e) + catch(DatabaseOperationException $e) { throw new StandardRpcException('Failed to set password due to an internal exception', StandardError::INTERNAL_SERVER_ERROR, $e); }