diff --git a/src/main/java/com/loafle/commons/rpc/RPCException.java b/src/main/java/com/loafle/commons/rpc/RPCException.java index 5e83be5..9584b50 100644 --- a/src/main/java/com/loafle/commons/rpc/RPCException.java +++ b/src/main/java/com/loafle/commons/rpc/RPCException.java @@ -4,5 +4,19 @@ package com.loafle.commons.rpc; * RPCException */ public class RPCException extends Exception { + private Object data; + + public RPCException(String message) { + super(message); + } + public RPCException(String message, Object data) { + super(message); + + this.data = data; + } + + public Object getData() { + return this.data; + } } diff --git a/src/main/java/com/loafle/commons/rpc/protocol/RPCServerRequestCodec.java b/src/main/java/com/loafle/commons/rpc/protocol/RPCServerRequestCodec.java index 2d83db5..3887273 100644 --- a/src/main/java/com/loafle/commons/rpc/protocol/RPCServerRequestCodec.java +++ b/src/main/java/com/loafle/commons/rpc/protocol/RPCServerRequestCodec.java @@ -7,5 +7,5 @@ import com.loafle.commons.rpc.RPCException; */ public interface RPCServerRequestCodec extends RPCRegistryCodec { boolean hasResponse(); - byte[] response(Object reply, Error error) throws RPCException; + byte[] response(Object reply, RPCException error) throws RPCException; } \ No newline at end of file diff --git a/src/main/java/com/loafle/commons/rpc/protocol/json/JSONRPCServerRequestCodec.java b/src/main/java/com/loafle/commons/rpc/protocol/json/JSONRPCServerRequestCodec.java index 71cf544..891e0a2 100644 --- a/src/main/java/com/loafle/commons/rpc/protocol/json/JSONRPCServerRequestCodec.java +++ b/src/main/java/com/loafle/commons/rpc/protocol/json/JSONRPCServerRequestCodec.java @@ -17,10 +17,11 @@ class JSONRPCServerRequestCodec implements RPCServerRequestCodec { public JSONRPCServerRequestCodec(Gson gson, byte[] buff) throws RPCException { this.gson = gson; + String json = new String(buff); try { - this.request = this.gson.fromJson(new String(buff), JSONServerRequest.class); + this.request = this.gson.fromJson(json, JSONServerRequest.class); } catch (JsonSyntaxException e) { - e.printStackTrace(); + throw new RPCException("request is not valied", json); } } @@ -34,11 +35,11 @@ class JSONRPCServerRequestCodec implements RPCServerRequestCodec { } if ((null != paramTypes && null == this.request.params) || null == paramTypes && null != this.request.params) { - throw new RPCException(); + throw new RPCException("params is not valied"); } if (paramTypes.length != this.request.params.size()) { - throw new RPCException(); + throw new RPCException(String.format("The size of params[%d] and types[%d] is not same", this.request.params.size(), paramTypes.length)); } Object[] result = new Object[paramTypes.length]; @@ -57,10 +58,10 @@ class JSONRPCServerRequestCodec implements RPCServerRequestCodec { return null != this.request.id; } - public byte[] response(Object reply, Error error) throws RPCException { + public byte[] response(Object reply, RPCException ex) throws RPCException { RPCError rpcError = null; - if (null != error) { - rpcError = new RPCError(1, "", error); + if (null != ex) { + rpcError = new RPCError(1, ex.getMessage(), ex.getData()); } JSONServerResponse response = new JSONServerResponse(JSONRPC.version, reply, rpcError); response.id = this.request.id; diff --git a/src/main/java/com/loafle/commons/rpc/protocol/json/JSONServerNotification.java b/src/main/java/com/loafle/commons/rpc/protocol/json/JSONServerNotification.java index 7015702..b5a94e5 100644 --- a/src/main/java/com/loafle/commons/rpc/protocol/json/JSONServerNotification.java +++ b/src/main/java/com/loafle/commons/rpc/protocol/json/JSONServerNotification.java @@ -6,10 +6,7 @@ import com.google.gson.annotations.SerializedName; * JSONServerNotification */ class JSONServerNotification { - @SerializedName("method") public String method; - - @SerializedName("params") public Object[] params; JSONServerNotification(String method, Object[] params) { diff --git a/src/main/java/com/loafle/commons/rpc/protocol/json/JSONServerRequest.java b/src/main/java/com/loafle/commons/rpc/protocol/json/JSONServerRequest.java index bbc18de..963a1a7 100644 --- a/src/main/java/com/loafle/commons/rpc/protocol/json/JSONServerRequest.java +++ b/src/main/java/com/loafle/commons/rpc/protocol/json/JSONServerRequest.java @@ -10,14 +10,8 @@ import com.google.gson.annotations.SerializedName; class JSONServerRequest { @SerializedName("jsonrpc") public String version; - - @SerializedName("method") public String method; - - @SerializedName("params") public List params; - - @SerializedName("id") public Object id; } \ No newline at end of file diff --git a/src/main/java/com/loafle/commons/rpc/protocol/json/JSONServerResponse.java b/src/main/java/com/loafle/commons/rpc/protocol/json/JSONServerResponse.java index ffc76e0..1c13db9 100644 --- a/src/main/java/com/loafle/commons/rpc/protocol/json/JSONServerResponse.java +++ b/src/main/java/com/loafle/commons/rpc/protocol/json/JSONServerResponse.java @@ -10,13 +10,8 @@ class JSONServerResponse { @SerializedName("jsonrpc") public String version; - @SerializedName("result") public Object result; - - @SerializedName("error") public RPCError error; - - @SerializedName("id") public Object id; JSONServerResponse(String version, Object result, RPCError error) { diff --git a/src/main/java/com/loafle/commons/rpc/registry/RPCRegistry.java b/src/main/java/com/loafle/commons/rpc/registry/RPCRegistry.java index 3bc52ee..d555d37 100644 --- a/src/main/java/com/loafle/commons/rpc/registry/RPCRegistry.java +++ b/src/main/java/com/loafle/commons/rpc/registry/RPCRegistry.java @@ -12,4 +12,4 @@ public interface RPCRegistry extends RPCInvoker { void registerService(Object receiver, String name) throws RPCException; void registerServices(Object... receivers) throws RPCException; void registerServices(Map receivers) throws RPCException; -} \ No newline at end of file +}