Improved exception handling
This commit is contained in:
parent
056ee90515
commit
998a7ad2ec
1 changed files with 8 additions and 5 deletions
|
@ -6,6 +6,8 @@
|
|||
use Socialbox\Enums\StandardError;
|
||||
use Socialbox\Enums\Types\InformationFieldName;
|
||||
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\PeerInformationManager;
|
||||
|
@ -16,29 +18,30 @@
|
|||
{
|
||||
/**
|
||||
* @inheritDoc
|
||||
* @noinspection DuplicatedCode
|
||||
*/
|
||||
public static function execute(ClientRequest $request, RpcRequest $rpcRequest): ?SerializableInterface
|
||||
{
|
||||
// Field parameter is required
|
||||
if(!$rpcRequest->containsParameter('field'))
|
||||
{
|
||||
return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, 'The required field parameter is missing');
|
||||
throw new MissingRpcArgumentException('field');
|
||||
}
|
||||
$fieldName = InformationFieldName::tryFrom(strtoupper($rpcRequest->getParameter('field')));
|
||||
if($fieldName === null)
|
||||
{
|
||||
return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, 'The provided field parameter is invalid');
|
||||
throw new InvalidRpcArgumentException('field');
|
||||
}
|
||||
|
||||
// Value parameter is required
|
||||
if(!$rpcRequest->containsParameter('value'))
|
||||
{
|
||||
return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, 'The required value parameter is missing');
|
||||
throw new MissingRpcArgumentException('value');
|
||||
}
|
||||
$value = $rpcRequest->getParameter('value');
|
||||
if(!$fieldName->validate($value))
|
||||
{
|
||||
return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, 'The provided value parameter is invalid');
|
||||
throw new InvalidRpcArgumentException('value');
|
||||
}
|
||||
|
||||
try
|
||||
|
@ -46,7 +49,7 @@
|
|||
$peer = $request->getPeer();
|
||||
if(!PeerInformationManager::fieldExists($peer, $fieldName))
|
||||
{
|
||||
return $rpcRequest->produceError(StandardError::FORBIDDEN, 'The information field does not exist');
|
||||
return $rpcRequest->produceResponse(false);
|
||||
}
|
||||
|
||||
PeerInformationManager::updateField($peer, $fieldName, $value);
|
||||
|
|
Loading…
Add table
Reference in a new issue