Improved exception handling

This commit is contained in:
netkas 2025-01-31 18:26:53 -05:00
parent 3510204dae
commit a0acb28037

View file

@ -7,7 +7,10 @@
use Socialbox\Classes\Cryptography;
use Socialbox\Enums\Flags\SessionFlags;
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\OneTimePasswordManager;
@ -23,10 +26,10 @@
*/
public static function execute(ClientRequest $request, RpcRequest $rpcRequest): ?SerializableInterface
{
$peer = $request->getPeer();
try
{
$peer = $request->getPeer();
if (OneTimePasswordManager::usesOtp($peer->getUuid()))
{
return $rpcRequest->produceError(StandardError::METHOD_NOT_ALLOWED, "Cannot set One Time Password when one is already set, use 'settingsUpdateOtp' instead");
@ -59,12 +62,12 @@
{
if(!$rpcRequest->containsParameter('password'))
{
return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, 'When a password is set, the current password must be provided to set an OTP');
throw new MissingRpcArgumentException('password');
}
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
@ -74,7 +77,11 @@
return $rpcRequest->produceError(StandardError::FORBIDDEN, 'The provided password is incorrect');
}
}
catch(Exception $e)
catch(CryptographyException $e)
{
throw new StandardRpcException($e->getMessage(), StandardError::CRYPTOGRAPHIC_ERROR, $e);
}
catch(DatabaseOperationException $e)
{
throw new StandardRpcException('Failed to verify password due to an internal exception', StandardError::INTERNAL_SERVER_ERROR, $e);
}