view core/src/luan/LuanException.java @ 282:38bd29e59a6e

minor git-svn-id: https://luan-java.googlecode.com/svn/trunk@283 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Wed, 26 Nov 2014 22:25:09 +0000
parents 9fb218211763
children 5d4a78c93383
line wrap: on
line source

package luan;

import java.io.StringWriter;
import java.io.PrintWriter;


public final class LuanException extends Exception {
	private final String stackTrace;

	LuanException(LuanBit bit,Object msg) {
		super(message(msg),cause(msg));
		stackTrace = stackTrace(bit,msg);
	}

	@Override public String getMessage() {
		return super.getMessage() + stackTrace;
	}

	public String getFullMessage() {
		String msg = getMessage();
		Throwable cause = getCause();
		if( cause != null ) {
			msg += "\nCaused by: ";
			StringWriter sw = new StringWriter();
			cause.printStackTrace(new PrintWriter(sw));
			msg += sw;
		}
		return msg;
	}

	private String message() {
		return super.getMessage();
	}

	private static Throwable cause(Object msg) {
		return msg instanceof Throwable ? (Throwable)msg : null;
	}

	private static String message(Object msg) {
		if( msg instanceof LuanException ) {
			LuanException le = (LuanException)msg;
			return le.message();
/*
		} else if( msg instanceof Throwable ) {
			Throwable t = (Throwable)msg;
			return t.getMessage();
*/
		} else {
			return msg.toString();
		}
	}

	private static String stackTrace(LuanBit bit,Object msg) {
		StringBuilder buf = new StringBuilder();
		buf.append( bit.stackTrace() );
		if( msg instanceof LuanException ) {
			LuanException le = (LuanException)msg;
			buf.append( "\ncaused by:" ).append( le.stackTrace );
		}
		return buf.toString();
	}
}