ufo-tech/rpc-exceptions

Exception package RPC server error codes

1.0.3 2024-04-30 19:44 UTC

This package is auto-updated.

Last update: 2024-05-02 20:09:55 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.

Code Type error Summary
-32700 Parse error Invalid JSON was received by the server
-32600 Invalid Request The JSON sent is not a valid Request object.
-32601 Method not found The method does not exist / is not available.
-32602 Invalid params Invalid method parameter(s).
-32603 Internal error Internal JSON-RPC error.
-32500 Application error Runtime error on procedure.
-32400 System error Logic error on application.
-32401 Security error Token not found
-32403 Security error Invalid token
-32300 Async error Error transfer async data
-32301 Batch error Error batch request
-32000 Server error Reserved for implementation-defined server-errors.
from -32001 to -32099 Custom user exception ---

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.

Code Exception class
-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
from -32001 to -32099" ---

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