ufo-tech/rpc-exceptions

Exception package RPC server error codes

1.0.4 2024-05-10 18:41 UTC

This package is auto-updated.

Last update: 2024-11-10 19:56:06 UTC


README

Exception package RPC server error codes

Ukraine

License Size package_version fork php_version

Problem

When a rpc call encounters an error, the Response Object MUST contain the error member with a value that is a Object with the following members:

  • code: A Number that indicates the error type that occurred. This MUST be an integer.

  • message: A String providing a short description of the error. The message SHOULD be limited to a concise single sentence.

There are many error codes and identifying the error from the code can be a difficult task. This library will help you. You can easily get the exception object from the error code.

Installation

$ composer require ufo-tech/rpc-exceptions

Get exception object

From code

use Ufo\RpcError\AbstractRpcErrorException;

$code = -32700;
$message = 'Some custom error message from rpc server'; // optional
$rpcException = AbstractRpcErrorException::fromCode($code);
// return instance of RpcJsonParseException::class

From array

use Ufo\RpcError\AbstractRpcErrorException;

$data = [
    'code' = -32600,
    'message' = 'Some custom error message from rpc server',
];
$rpcException = RpcBadRequestException::fromArray($data);
// return instance of RpcBadRequestException::class

From json

use Ufo\RpcError\AbstractRpcErrorException;

$data = "{\"code\":-32500,\"message\":\"Some custom error message from rpc server\"}";
$rpcException = AbstractRpcErrorException::fromArray($data);
// return instance of RpcRuntimeException::class

Mapping list

use Ufo\RpcError\AbstractRpcErrorException;

$mapping = AbstractRpcErrorException::getRpcErrorsList();
// return array map
[
    -32700 => RpcJsonParseException::class,
    -32600 => RpcBadRequestException::class,
    -32601 => RpcMethodNotFoundExceptionRpc::class,
    -32602 => RpcBadParamException::class,
    -32603 => RpcInternalException::class,
    -32500 => RpcRuntimeException::class,
    -32400 => RpcLogicException::class,
    -32401 => RpcTokenNotFoundInHeaderException::class,
    -32403 => RpcInvalidTokenException::class,
    -32300 => RpcAsyncRequestException::class,
    -32301 => RpcInvalidBatchRequestExceptions::class,
    -32000 => RpcDataNotFoundException::class,
]

Profit