view src/main/java/net/borgac/clusterrpc/client/RpcException.java @ 9:10c9c3048bee

Implement RpcException
author Lewin Bormann <lbo@spheniscida.de>
date Sun, 25 Sep 2016 15:17:36 +0200
parents
children
line wrap: on
line source

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