Improved exception handling

This commit is contained in:
netkas 2025-01-31 12:56:45 -05:00
parent 0f67bb3337
commit 1f55538ad4

View file

@ -2,11 +2,11 @@
namespace Socialbox\Classes\StandardMethods\Core; namespace Socialbox\Classes\StandardMethods\Core;
use Exception;
use InvalidArgumentException; use InvalidArgumentException;
use Socialbox\Abstracts\Method; use Socialbox\Abstracts\Method;
use Socialbox\Enums\ReservedUsernames; use Socialbox\Enums\ReservedUsernames;
use Socialbox\Enums\StandardError; use Socialbox\Enums\StandardError;
use Socialbox\Exceptions\DatabaseOperationException;
use Socialbox\Exceptions\Standard\MissingRpcArgumentException; use Socialbox\Exceptions\Standard\MissingRpcArgumentException;
use Socialbox\Exceptions\Standard\StandardRpcException; use Socialbox\Exceptions\Standard\StandardRpcException;
use Socialbox\Interfaces\SerializableInterface; use Socialbox\Interfaces\SerializableInterface;
@ -40,20 +40,20 @@
} }
// Check if host is making the request & the identifier is not empty // Check if host is making the request & the identifier is not empty
$identifyAs = null; try
if($request->getPeer()->getUsername() == ReservedUsernames::HOST && $request->getIdentifyAs() != null)
{ {
$identifyAs = $request->getIdentifyAs(); $identifyAs = null;
if ($request->getPeer()->getUsername() == ReservedUsernames::HOST && $request->getIdentifyAs() != null)
{
$identifyAs = $request->getIdentifyAs();
}
}
catch (DatabaseOperationException $e)
{
throw new StandardRpcException('Failed to retrieve peer information', StandardError::INTERNAL_SERVER_ERROR, $e);
} }
// Resolve the peer using the server's peer resolver, this will resolve both internal peers and external peers // Resolve the peer using the server's peer resolver, this will resolve both internal peers and external peers
try return $rpcRequest->produceResponse(Socialbox::resolvePeer($peerAddress, $identifyAs));
{
return $rpcRequest->produceResponse(Socialbox::resolvePeer($peerAddress, $identifyAs));
}
catch(Exception $e)
{
throw new StandardRpcException(sprintf('There was an error while trying to resolve the peer %s: %s', $peerAddress, $e->getMessage()), StandardError::RESOLUTION_FAILED, $e);
}
} }
} }