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,12 +34,9 @@
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
{ {
$results[] = $response;
}
}
try try
{ {
$response = $method->execute($clientRequest, $rpcRequest); $response = $method->execute($clientRequest, $rpcRequest);
@ -54,21 +47,36 @@
} }
catch(Exception $e) catch(Exception $e)
{ {
$response = $rpcRequest->produceError(StandardError::INTERNAL_SERVER_ERROR, 'An error occurred while processing the request'); 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);
}
}
} }
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));
} }
} }