Mercurial > lbo > hg > clusterrpc-java
changeset 9:10c9c3048bee
Implement RpcException
author | Lewin Bormann <lbo@spheniscida.de> |
---|---|
date | Sun, 25 Sep 2016 15:17:36 +0200 |
parents | 7da0ba664d74 |
children | b99a9821115c |
files | src/main/java/net/borgac/clusterrpc/client/RpcException.java |
diffstat | 1 files changed, 65 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/net/borgac/clusterrpc/client/RpcException.java Sun Sep 25 15:17:36 2016 +0200 @@ -0,0 +1,65 @@ +package net.borgac.clusterrpc.client; + +/** + * An exception that can occur while handling RPC communication. + * + * @author lbo + */ +public class RpcException extends Exception { + + private static final long serialVersionUID = 1L; + + private Exception inner; + private String message; + private final Reason reason; + + RpcException(Exception inner, Reason reason, String message) { + this.inner = inner; + this.message = message; + this.reason = reason; + } + + RpcException(Reason cause, String reason) { + this.message = reason; + this.reason = cause; + } + + RpcException(Reason cause) { + this.reason = cause; + } + + @Override + public Throwable getCause() { + return inner; + } + + public Reason getReason() { + return reason; + } + + @Override + public String toString() { + if (inner != null) { + return String.format("%s: %s (%s)", message, inner.toString(), reason.toString()); + } else if (message != null && reason != null) { + return String.format("%s (%s)", message, reason.toString()); + } else if (reason != null) { + return String.format("%s exception", reason.toString()); + } else { + return "Unknown exception"; + } + } + + /** + * A cause for an exception. + * + */ + public static enum Reason { + UNKNOWN, + IO_ERROR, + RPC_PROTOCOL_ERROR, + ENCODING_ERROR, + DECODING_ERROR, + UNEXPECTED_STATE, + } +}