Refactor RPC handling and improve error response clarity.

This commit is contained in:
netkas 2024-09-27 14:21:35 -04:00
parent 76c3629be2
commit 7524032f0f

View file

@ -2,9 +2,10 @@
namespace Socialbox; namespace Socialbox;
use ConfigLib\Configuration;
use Exception; use Exception;
use Socialbox\Classes\Configuration;
use Socialbox\Classes\RpcHandler; use Socialbox\Classes\RpcHandler;
use Socialbox\Classes\Utilities;
use Socialbox\Enums\StandardError; use Socialbox\Enums\StandardError;
use Socialbox\Enums\StandardMethods; use Socialbox\Enums\StandardMethods;
use Socialbox\Exceptions\RpcException; use Socialbox\Exceptions\RpcException;
@ -12,11 +13,6 @@
class Socialbox class Socialbox
{ {
public static function getConfiguration(): array
{
}
public static function handleRpc(): void public static function handleRpc(): void
{ {
try try
@ -38,37 +34,49 @@
if($method === false) if($method === false)
{ {
$response = $rpcRequest->produceError(StandardError::RPC_METHOD_NOT_FOUND, 'The requested method does not exist'); $response = $rpcRequest->produceError(StandardError::RPC_METHOD_NOT_FOUND, 'The requested method does not exist');
if($response !== null) }
else
{
try
{ {
$results[] = $response; $response = $method->execute($clientRequest, $rpcRequest);
}
catch(StandardException $e)
{
$response = $e->produceError($rpcRequest);
}
catch(Exception $e)
{
var_dump($e);
if(Configuration::getConfiguration()['security']['display_internal_exceptions'])
{
$response = $rpcRequest->produceError(StandardError::INTERNAL_SERVER_ERROR, Utilities::throwableToString($e));
}
else
{
$response = $rpcRequest->produceError(StandardError::INTERNAL_SERVER_ERROR);
}
} }
}
try
{
$response = $method->execute($clientRequest, $rpcRequest);
}
catch(StandardException $e)
{
$response = $e->produceError($rpcRequest);
}
catch(Exception $e)
{
$response = $rpcRequest->produceError(StandardError::INTERNAL_SERVER_ERROR, 'An error occurred while processing the request');
} }
if($response !== null) if($response !== null)
{ {
$results[] = $response; $results[] = $response->toArray();
} }
} }
if(count($results) > 0) if(count($results) == 0)
{ {
print(json_encode($results)); http_response_code(204);
return; return;
} }
http_response_code(204); if(count($results) == 1)
{
print(json_encode($results[0]));
return;
}
print(json_encode($results));
} }
} }