From 148a65c2f785b63715d3c8258f0efc3308dab735 Mon Sep 17 00:00:00 2001 From: netkas Date: Fri, 31 Jan 2025 15:07:24 -0500 Subject: [PATCH] Improved exception handling --- .../Settings/SettingsDeletePassword.php | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/Socialbox/Classes/StandardMethods/Settings/SettingsDeletePassword.php b/src/Socialbox/Classes/StandardMethods/Settings/SettingsDeletePassword.php index caec050..06b78e2 100644 --- a/src/Socialbox/Classes/StandardMethods/Settings/SettingsDeletePassword.php +++ b/src/Socialbox/Classes/StandardMethods/Settings/SettingsDeletePassword.php @@ -2,12 +2,14 @@ namespace Socialbox\Classes\StandardMethods\Settings; - use Exception; use Socialbox\Abstracts\Method; use Socialbox\Classes\Configuration; use Socialbox\Classes\Cryptography; use Socialbox\Enums\StandardError; use Socialbox\Exceptions\CryptographyException; + use Socialbox\Exceptions\DatabaseOperationException; + use Socialbox\Exceptions\Standard\InvalidRpcArgumentException; + use Socialbox\Exceptions\Standard\MissingRpcArgumentException; use Socialbox\Exceptions\Standard\StandardRpcException; use Socialbox\Interfaces\SerializableInterface; use Socialbox\Managers\PasswordManager; @@ -32,20 +34,21 @@ // Check if the password parameter is present if(!$rpcRequest->containsParameter('password')) { - return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, "Missing 'password' parameter"); + throw new MissingRpcArgumentException('password'); } // Validate the password parameter if(!Cryptography::validateSha512($rpcRequest->getParameter('password'))) { - return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, "Invalid 'password' parameter, must be a valid SHA-512 hash"); + throw new InvalidRpcArgumentException('password', 'Must be a valid SHA-512 hash'); } - // Get the peer - $peer = $request->getPeer(); try { + // Get the peer + $peer = $request->getPeer(); + // Check if the password is set if (!PasswordManager::usesPassword($peer->getUuid())) { @@ -55,7 +58,7 @@ // Verify the existing password before deleting it if (!PasswordManager::verifyPassword($peer->getUuid(), $rpcRequest->getParameter('password'))) { - return $rpcRequest->produceError(StandardError::FORBIDDEN, "Failed to delete password due to incorrect existing password"); + return $rpcRequest->produceResponse(false); } // Delete the password @@ -65,9 +68,9 @@ { return $rpcRequest->produceError(StandardError::CRYPTOGRAPHIC_ERROR, 'Failed to delete password due to a cryptographic error'); } - catch (Exception $e) + catch (DatabaseOperationException $e) { - throw new StandardRpcException('Failed to check password due to an internal exception', StandardError::INTERNAL_SERVER_ERROR, $e); + throw new StandardRpcException('Failed to delete password due to an internal exception', StandardError::INTERNAL_SERVER_ERROR, $e); } // Success