package com.agedwards.core.util;

/**
 * Wraps another exception.
 * @author R. Mark Volkmann, Object Computing, Inc.
 */
public class WrappingException extends Exception {

    private Exception nestedException;

    /**
     * Creates a WrappingException.
     * @param e the Exception to be wrapped
     */
    public WrappingException(Exception e) {
        nestedException = e;
    }

    /**
     * Creates a WrappingException.
     * @param message the message to be associated with the exception
     *                in case it doesn't wrap another exception
     */
    public WrappingException(String message) {
        super(message);
    }

    /**
     * Gets the message associated with this exception.
     * If this exception wraps another one,
     * the message comes from the wrapped exception.
     * @return the message associated with this exception
     */
    public String getMessage() {
        return nestedException == null ?
            super.getMessage() : nestedException.getMessage();
    }

    /**
     * Gets the nested exception.
     * @return the message associated with this exception
     */
    public Exception getNestedException() {
        return nestedException;
    }

    /**
     * Gets a String representation of this exception.
     * @return the String representation
     */
    public String toString() {
        return getMessage();
    }
}