/*
 * Created on 2005. 2. 26.
 *
 */
package com.eactive.eai.adapter.socket2.common;

import com.eactive.eai.adapter.EAIException;

/**
 * @author janet
 *
 */
public class SocketAdapterException extends EAIException {

	  public static final int UNEXPECTED_TERMINATION = 1;

	  public static final int REMOTE_HOST_DISCONNECTED = 2;

	  public static final int INTERNAL_ERROR = 5;

	  public static final int CHANNEL_FAILURE = 6;

	  public static final int CANCELLED_CONNECTION = 8;

	  public static final int CONNECT_FAILED = 10;

	  public static final int CONNECTION_CLOSED = 12;

	  public static final int AGENT_ERROR = 13;

	  public static final int SESSION_STREAM_ERROR = 15;

	  int reason;

	  Throwable cause;

	  /**
	   * Create an exception with the given description and reason.
	   * @param msg
	   * @param reason
	   */
	  
	  public SocketAdapterException(String msg, int reason) {
	    this(msg, reason, null);
	  }

	  /**
	   * Create an exception with the given cause and reason.
	   * @param reason
	   * @param cause
	   */
	  public SocketAdapterException(int reason, Throwable cause) {
	    this(null, reason, cause);
	  }

	  /**
	   * Create an exception with the given description and cause. The reason given
	   * will be <code>INTERNAL_ERROR</code>.
	   *
	   * @param msg
	   * @param cause
	   */
	  public SocketAdapterException(String msg, Throwable cause) {
	    this(msg, INTERNAL_ERROR, cause);
	  }

	  /**
	   * 
	   * @param msg
	   */
	  public SocketAdapterException(String msg) {
	    this(msg, INTERNAL_ERROR, null);
	  }

	  /**
	   * Create an exception by providing the cause of the error. This constructor
	   * sets the reason to INTERNAL_ERROR.
	   * @param cause
	   */
	  public SocketAdapterException(Throwable cause) {
	    this("An unexpected exception was caught: " + cause.getMessage(), cause);
	  }

	  /**
	   * Create an exception with the given description cause, reason.
	   * @param msg
	   * @param reason
	   * @param cause
	   */
	  public SocketAdapterException(String msg, int reason, Throwable cause) {
	    super(msg);
	    this.cause = cause;
	    this.reason = reason;
	  }

	  /**
	   * Get the reason for the exception
	   * @return
	   */
	  public int getReason() {
	    return reason;
	  }

	  /**
	   * If an INTERNAL_ERROR reason is given this method MAY return the cause of
	   * the error.
	   * @return
	   */
	  @Override
	  public synchronized Throwable getCause() {
	    return cause;
	  }

}
