briandilley / jsonrpc4j

JSON-RPC for Java
MIT License
1.05k stars 439 forks source link

About @ jsonrpcerrors and @ jsonrpcclientexception #277

Open qq454549798 opened 3 years ago

qq454549798 commented 3 years ago

In the use of jsonrpc exceptions, it is troublesome to annotate @ jsonrpcerrors. If there are different exceptions in a method, multiple items need to be configured in @ jsonrpcerrors.

In the exception handling class defaulterrorresolver, it is recommended to adjust the method resolveerror to:

public JsonError resolveError(Throwable t, Method method, List arguments) {

//Add an exception judgment and bring in the error code

if (t instanceof JsonRpcClientException) {

JsonRpcClientException rpcException = ((JsonRpcClientException) t);

return new JsonError(rpcException.getCode(), rpcException.getMessage(), new ErrorData(t.getClass().getName(), rpcException.getMessage()));

}

return new JsonError(ERROR_NOT_HANDLED.code, t.getMessage(), new ErrorData(t.getClass().getName(), t.getMessage()));

}

In this way, when trying to catch, you can directly handle exceptions through new jsonrpcclientexception (code, message, jsonnode).

If you don't rewrite the code here, the code here can't be customized. It's always 36001