Commit a443bff6 authored by Kristian Feldsam's avatar Kristian Feldsam
Browse files

Response error codes handling as exceptions


Signed-off-by: Kristian Feldsam's avatarKristian Feldsam <feldsam@gmail.com>
parent fd42699e
<?php
namespace One\Exception;
class Authentication extends \RuntimeException {}
<?php
namespace One\Exception;
class Authorization extends \RuntimeException {}
<?php
namespace One\Exception;
class CannotAllocated extends \RuntimeException {}
<?php
namespace One\Exception;
class InternalError extends \RuntimeException {}
<?php
namespace One\Exception;
class NotFound extends \RuntimeException {}
<?php
namespace One\Exception;
class ResourceLocked extends \RuntimeException {}
<?php
namespace One\Exception;
class WrongParameters extends \RuntimeException {}
<?php
namespace One\Exception;
class WrongState extends \RuntimeException {}
......@@ -27,6 +27,44 @@ class OneBase {
*/
private $authString;
/**
* @var \string[][]
*/
private static $errorCodes = [
256 => [
'Authentication',
'User could not be authenticated.'
],
512 => [
'Authorization',
'User is not authorized to perform the requested action.'
],
1024 => [
'NotFound',
'The requested resource does not exist.'
],
2048 => [
'WrongState',
'Wrong state to perform action.'
],
4096 => [
'WrongParameters',
'Wrong parameters, e.g. param should be -1 or -2, but -3 was received.'
],
8192 => [
'InternalError',
'Internal error, e.g. the resource could not be loaded from the DB.'
],
16384 => [
'CannotAllocated',
'The resource cannot be allocated.'
],
32768 => [
'ResourceLocked',
'The resource is locked.'
]
];
private static $xmlrpcTypes = [
"boolean" => "boolean",
"integer" => "int",
......@@ -85,23 +123,15 @@ class OneBase {
}
if(!$response->value()) {
throw new \RuntimeException(sprintf(
'XML-RPC API Call ends with error "%s" and code "%s"',
$response->faultString(),
$response->faultCode()
));
static::raiseException($response->faultCode(), $response->faultString());
}
$success = (bool) $response->value()[0]->scalarval();
$body = (string) $response->value()[1]->scalarval();
$code = (string) $response->value()[2]->scalarval();
$code = (int) $response->value()[2]->scalarval();
if($success !== true) {
throw new \RuntimeException(sprintf(
'XML-RPC API Call ends with error "%s" and code "%s"',
$body,
$code
));
static::raiseException($code, $body);
}
// parse xml with proper error handling
......@@ -141,7 +171,7 @@ class OneBase {
* @return array
* https://hotexamples.com/examples/-/-/simplexml_to_array/php-simplexml_to_array-function-examples.html
*/
public static function simplexmlToArray($xml)
public static function simplexmlToArray($xml): Array
{
$ar = array();
foreach ($xml->children() as $k => $v) {
......@@ -168,4 +198,33 @@ class OneBase {
}
return $ar;
}
/**
* @param int $code
* @param string $error
* @throws \RuntimeException
* @throws Exception\Authentication
* @throws Exception\Authorization
* @throws Exception\NotFound
* @throws Exception\WrongState
* @throws Exception\WrongParameters
* @throws Exception\InternalError
* @throws Exception\CannotAllocated
* @throws Exception\ResourceLocked
*/
private static function raiseException(int $code, string $error): void {
if(!array_key_exists($code, static::$errorCodes)) {
throw new \RuntimeException(sprintf(
'XML-RPC API Call ends with error "%s" and code "%s"',
$error,
$code
));
}
$error = static::$errorCodes[$code];
$exception = "One\\Exception\\".$error[0];
throw new $exception($error[1]);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment