view src/luan/LuanException.java @ 88:6ca02b188dba

add LuanBit to clean up code; add repr(); git-svn-id: https://luan-java.googlecode.com/svn/trunk@89 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Wed, 27 Feb 2013 23:50:32 +0000
parents 8ede219cd111
children 14281d5bd36f
line wrap: on
line source

package luan;


public class LuanException extends Exception {
	private final String stackTrace;

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

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

	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(LuanState luan,LuanElement el,Object msg) {
		StringBuilder buf = new StringBuilder();
		for( int i  = luan.stackTrace.size() - 1; i>=0; i-- ) {
			StackTraceElement stackTraceElement = luan.stackTrace.get(i);
			buf.append( "\n\t" ).append( el.toString(stackTraceElement.fnName) );
			el = stackTraceElement.call;
		}
		if( msg instanceof LuanException ) {
			LuanException le = (LuanException)msg;
			buf.append( "\ncaused by:" ).append( le.stackTrace );
		}
		return buf.toString();
	}
}