RPC exception serialization
This commit is contained in:
parent
385746dc4f
commit
e1548e88e8
1 changed files with 52 additions and 0 deletions
52
runtime/src/main/java/org/capnproto/RpcException.java
Normal file
52
runtime/src/main/java/org/capnproto/RpcException.java
Normal file
|
@ -0,0 +1,52 @@
|
||||||
|
package org.capnproto;
|
||||||
|
|
||||||
|
public final class RpcException extends java.lang.Exception {
|
||||||
|
|
||||||
|
public enum Type {
|
||||||
|
UNKNOWN,
|
||||||
|
UNIMPLEMENTED,
|
||||||
|
FAILED
|
||||||
|
}
|
||||||
|
|
||||||
|
private Type type;
|
||||||
|
|
||||||
|
public RpcException(Type type, String message) {
|
||||||
|
super(message);
|
||||||
|
this.type = type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public final Type getType() {
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static RpcException unimplemented(String message) {
|
||||||
|
return new RpcException(Type.UNIMPLEMENTED, message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static RpcException failed(String message) {
|
||||||
|
return new RpcException(Type.FAILED, message);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void fromException(Throwable exc, RpcProtocol.Exception.Builder builder) {
|
||||||
|
builder.setReason(exc.getMessage());
|
||||||
|
builder.setType(RpcProtocol.Exception.Type.FAILED);
|
||||||
|
}
|
||||||
|
|
||||||
|
static RpcException toException(RpcProtocol.Exception.Reader reader) {
|
||||||
|
var type = RpcException.Type.UNKNOWN;
|
||||||
|
|
||||||
|
switch (reader.getType()) {
|
||||||
|
case UNIMPLEMENTED:
|
||||||
|
type = RpcException.Type.UNIMPLEMENTED;
|
||||||
|
break;
|
||||||
|
case FAILED:
|
||||||
|
type = RpcException.Type.FAILED;
|
||||||
|
break;
|
||||||
|
case DISCONNECTED:
|
||||||
|
case OVERLOADED:
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return new RpcException(type, reader.getReason().toString());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue