diff --git a/src/Socialbox/Classes/StandardMethods/AddressBook/AddressBookAddContact.php b/src/Socialbox/Classes/StandardMethods/AddressBook/AddressBookAddContact.php index 2f4a88e..7af29fd 100644 --- a/src/Socialbox/Classes/StandardMethods/AddressBook/AddressBookAddContact.php +++ b/src/Socialbox/Classes/StandardMethods/AddressBook/AddressBookAddContact.php @@ -38,7 +38,7 @@ } catch(InvalidArgumentException $e) { - throw new InvalidRpcArgumentException('peer', $e->getMessage()); + throw new InvalidRpcArgumentException('peer', $e); } if($rpcRequest->containsParameter('relationship')) @@ -46,7 +46,7 @@ $relationship = ContactRelationshipType::tryFrom(strtoupper($rpcRequest->getParameter('relationship'))); if($relationship === null) { - throw new InvalidRpcArgumentException('peer', 'Invalid relationship type'); + throw new InvalidRpcArgumentException('relationship'); } } else diff --git a/src/Socialbox/Classes/StandardMethods/AddressBook/AddressBookContactExists.php b/src/Socialbox/Classes/StandardMethods/AddressBook/AddressBookContactExists.php index 087baa2..9758ce2 100644 --- a/src/Socialbox/Classes/StandardMethods/AddressBook/AddressBookContactExists.php +++ b/src/Socialbox/Classes/StandardMethods/AddressBook/AddressBookContactExists.php @@ -35,7 +35,7 @@ } catch(InvalidArgumentException $e) { - throw new InvalidRpcArgumentException('peer', $e->getMessage()); + throw new InvalidRpcArgumentException('peer', $e); } try diff --git a/src/Socialbox/Classes/StandardMethods/AddressBook/AddressBookDeleteContact.php b/src/Socialbox/Classes/StandardMethods/AddressBook/AddressBookDeleteContact.php index 1f27565..b891b92 100644 --- a/src/Socialbox/Classes/StandardMethods/AddressBook/AddressBookDeleteContact.php +++ b/src/Socialbox/Classes/StandardMethods/AddressBook/AddressBookDeleteContact.php @@ -36,7 +36,7 @@ } catch(InvalidArgumentException $e) { - throw new InvalidRpcArgumentException('peer', $e->getMessage()); + throw new InvalidRpcArgumentException('peer', $e); } try diff --git a/src/Socialbox/Classes/StandardMethods/AddressBook/AddressBookGetContact.php b/src/Socialbox/Classes/StandardMethods/AddressBook/AddressBookGetContact.php index 7054cc8..5443d29 100644 --- a/src/Socialbox/Classes/StandardMethods/AddressBook/AddressBookGetContact.php +++ b/src/Socialbox/Classes/StandardMethods/AddressBook/AddressBookGetContact.php @@ -35,7 +35,7 @@ } catch(InvalidArgumentException $e) { - throw new InvalidRpcArgumentException('peer', $e->getMessage()); + throw new InvalidRpcArgumentException('peer', $e); } try diff --git a/src/Socialbox/Classes/StandardMethods/AddressBook/AddressBookRevokeSignature.php b/src/Socialbox/Classes/StandardMethods/AddressBook/AddressBookRevokeSignature.php index 609cda7..52d00f1 100644 --- a/src/Socialbox/Classes/StandardMethods/AddressBook/AddressBookRevokeSignature.php +++ b/src/Socialbox/Classes/StandardMethods/AddressBook/AddressBookRevokeSignature.php @@ -34,7 +34,7 @@ } catch(InvalidArgumentException $e) { - throw new InvalidRpcArgumentException('peer', $e->getMessage()); + throw new InvalidRpcArgumentException('peer', $e); } if(!$rpcRequest->containsParameter('uuid')) @@ -48,7 +48,7 @@ } catch(InvalidArgumentException $e) { - throw new InvalidRpcArgumentException('uuid', $e->getMessage()); + throw new InvalidRpcArgumentException('uuid', $e); } try diff --git a/src/Socialbox/Classes/StandardMethods/AddressBook/AddressBookTrustSignature.php b/src/Socialbox/Classes/StandardMethods/AddressBook/AddressBookTrustSignature.php index c04a2a9..673d5b0 100644 --- a/src/Socialbox/Classes/StandardMethods/AddressBook/AddressBookTrustSignature.php +++ b/src/Socialbox/Classes/StandardMethods/AddressBook/AddressBookTrustSignature.php @@ -36,7 +36,7 @@ } catch(InvalidArgumentException $e) { - throw new InvalidRpcArgumentException('peer', $e->getMessage()); + throw new InvalidRpcArgumentException('peer', $e); } if(!$rpcRequest->containsParameter('uuid')) @@ -50,7 +50,7 @@ } catch(InvalidArgumentException $e) { - throw new InvalidRpcArgumentException('uuid', $e->getMessage()); + throw new InvalidRpcArgumentException('uuid', $e); } $signingKey = Socialbox::resolvePeerSignature($address, $uuid); diff --git a/src/Socialbox/Classes/StandardMethods/AddressBook/AddressBookUpdateRelationship.php b/src/Socialbox/Classes/StandardMethods/AddressBook/AddressBookUpdateRelationship.php index cf217c0..7e1898a 100644 --- a/src/Socialbox/Classes/StandardMethods/AddressBook/AddressBookUpdateRelationship.php +++ b/src/Socialbox/Classes/StandardMethods/AddressBook/AddressBookUpdateRelationship.php @@ -34,7 +34,7 @@ } catch(InvalidArgumentException $e) { - throw new InvalidRpcArgumentException('peer', 'Invalid peer address'); + throw new InvalidRpcArgumentException('peer'); } if(!$rpcRequest->containsParameter('relationship')) @@ -44,7 +44,7 @@ $relationship = ContactRelationshipType::tryFrom(strtoupper($rpcRequest->getParameter('relationship'))); if($relationship === null) { - throw new InvalidRpcArgumentException('relationship', 'Invalid relationship type'); + throw new InvalidRpcArgumentException('relationship'); } try diff --git a/src/Socialbox/Classes/StandardMethods/Settings/SettingsAddInformationField.php b/src/Socialbox/Classes/StandardMethods/Settings/SettingsAddInformationField.php index 88145ca..30b5978 100644 --- a/src/Socialbox/Classes/StandardMethods/Settings/SettingsAddInformationField.php +++ b/src/Socialbox/Classes/StandardMethods/Settings/SettingsAddInformationField.php @@ -9,6 +9,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; @@ -20,29 +22,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'); } // Privacy parameter is optional @@ -52,7 +55,7 @@ $privacy = PrivacyState::tryFrom(strtoupper($rpcRequest->getParameter('privacy'))); if($privacy === null) { - return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, 'The provided privacy parameter is invalid'); + throw new InvalidRpcArgumentException('privacy'); } } @@ -62,14 +65,15 @@ } catch (DatabaseOperationException $e) { - throw new StandardRpcException('Failed to retrieve current peer information', StandardError::INTERNAL_SERVER_ERROR, $e); + throw new StandardRpcException('Failed to retrieve peer information', StandardError::INTERNAL_SERVER_ERROR, $e); } try { if (PeerInformationManager::fieldExists($peer, $fieldName)) { - return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, 'The provided field parameter is already registered, use settingsUpdateInformationField or settingsUpdateInformationPrivacy instead'); + // Return False, because the field already exists + return $rpcRequest->produceResponse(false); } PeerInformationManager::addField($peer, $fieldName, $value, $privacy); diff --git a/src/Socialbox/Classes/StandardMethods/Settings/SettingsAddSignature.php b/src/Socialbox/Classes/StandardMethods/Settings/SettingsAddSignature.php index 5c7fb3c..5b3de2a 100644 --- a/src/Socialbox/Classes/StandardMethods/Settings/SettingsAddSignature.php +++ b/src/Socialbox/Classes/StandardMethods/Settings/SettingsAddSignature.php @@ -31,7 +31,6 @@ { $expires = (int)$rpcRequest->getParameter('expires'); } - if(!$rpcRequest->containsParameter('name')) { throw new MissingRpcArgumentException('name'); @@ -43,10 +42,9 @@ $name = $rpcRequest->getParameter('name'); } - $peerUuid = $request->getPeer()->getUuid(); - try { + $peerUuid = $request->getPeer()->getUuid(); if(SigningKeysManager::getSigningKeyCount($peerUuid) >= Configuration::getPoliciesConfiguration()->getMaxSigningKeys()) { return $rpcRequest->produceError(StandardError::FORBIDDEN, 'The maximum number of signing keys has been reached'); diff --git a/src/Socialbox/Classes/StandardMethods/Settings/SettingsDeleteInformationField.php b/src/Socialbox/Classes/StandardMethods/Settings/SettingsDeleteInformationField.php index 32e095e..a337efa 100644 --- a/src/Socialbox/Classes/StandardMethods/Settings/SettingsDeleteInformationField.php +++ b/src/Socialbox/Classes/StandardMethods/Settings/SettingsDeleteInformationField.php @@ -7,6 +7,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; @@ -23,12 +25,12 @@ // 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'); } try diff --git a/src/Socialbox/Exceptions/Standard/InvalidRpcArgumentException.php b/src/Socialbox/Exceptions/Standard/InvalidRpcArgumentException.php index 45b8673..33103fe 100644 --- a/src/Socialbox/Exceptions/Standard/InvalidRpcArgumentException.php +++ b/src/Socialbox/Exceptions/Standard/InvalidRpcArgumentException.php @@ -2,6 +2,8 @@ namespace Socialbox\Exceptions\Standard; + use Exception; + use InvalidArgumentException; use Socialbox\Enums\StandardError; class InvalidRpcArgumentException extends StandardRpcException @@ -10,10 +12,22 @@ * Thrown when a required parameter is missing * * @param string $parameterName The name of the parameter that is missing - * @param string $reason The reason why the parameter is invalid + * @param string|Exception|null $reason The reason why the parameter is invalid can be a string or an exception or null */ - public function __construct(string $parameterName, string $reason) + public function __construct(string $parameterName, null|string|Exception $reason=null) { + if(is_null($reason)) + { + parent::__construct(sprintf('Invalid parameter %s', $parameterName), StandardError::RPC_INVALID_ARGUMENTS); + return; + } + + if($reason instanceof InvalidArgumentException) + { + parent::__construct(sprintf('Invalid parameter %s: %s', $parameterName, $reason->getMessage()), StandardError::RPC_INVALID_ARGUMENTS, $reason); + return; + } + parent::__construct(sprintf('Invalid parameter %s: %s', $parameterName, $reason), StandardError::RPC_INVALID_ARGUMENTS); } } \ No newline at end of file