Improved InvalidRpcArgumentException handling

This commit is contained in:
netkas 2025-01-31 13:28:28 -05:00
parent 55137a93f2
commit 0ed6a36d50
11 changed files with 43 additions and 25 deletions

View file

@ -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);
}
}