PHP code example of ufo-tech / rpc-exceptions

1. Go to this page and download the library: Download ufo-tech/rpc-exceptions library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ufo-tech / rpc-exceptions example snippets


use Ufo\RpcError\AbstractRpcErrorException;

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

use Ufo\RpcError\AbstractRpcErrorException;

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

use Ufo\RpcError\AbstractRpcErrorException;

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

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,
]