diff src/luan/LuaException.java @ 42:786699c78837

implement try-catch git-svn-id: https://luan-java.googlecode.com/svn/trunk@43 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Sun, 23 Dec 2012 06:36:56 +0000
parents e3624b7cd603
children 80b67b1a653c
line wrap: on
line diff
--- a/src/luan/LuaException.java	Fri Dec 21 19:34:50 2012 +0000
+++ b/src/luan/LuaException.java	Sun Dec 23 06:36:56 2012 +0000
@@ -2,20 +2,38 @@
 
 
 public class LuaException extends Exception {
+	private final String stackTrace;
 
-	public LuaException(LuaState lua,LuaElement el,String msg) {
-		super(hideNull(msg)+stackTrace(lua,el));
+	public LuaException(LuaState lua,LuaElement el,Object msg) {
+		super(message(msg),cause(msg));
+		stackTrace = stackTrace(lua,el,msg);
+	}
+
+	@Override public String getMessage() {
+		return super.getMessage() + stackTrace;
+	}
+
+	private String message() {
+		return super.getMessage();
 	}
 
-	public LuaException(LuaState lua,LuaElement el,Exception cause) {
-		super(hideNull(cause.getMessage())+stackTrace(lua,el),cause);
+	private static Throwable cause(Object msg) {
+		return msg instanceof Throwable ? (Throwable)msg : null;
 	}
 
-	private static String hideNull(String s) {
-		return s==null ? "" : s;
+	private static String message(Object msg) {
+		if( msg instanceof LuaException ) {
+			LuaException le = (LuaException)msg;
+			return le.message();
+		} else if( msg instanceof Throwable ) {
+			Throwable t = (Throwable)msg;
+			return t.getMessage();
+		} else {
+			return msg.toString();
+		}
 	}
 
-	private static String stackTrace(LuaState lua,LuaElement el) {
+	private static String stackTrace(LuaState lua,LuaElement el,Object msg) {
 		StringBuilder buf = new StringBuilder();
 		int i = lua.stackTrace.size() - 1;
 		do {
@@ -23,6 +41,10 @@
 			buf.append( "\n\t" ).append( el.toString(stackTraceElement.fnName) );
 			el = stackTraceElement.call;
 		} while( --i >= 0 );
+		if( msg instanceof LuaException ) {
+			LuaException le = (LuaException)msg;
+			buf.append( "\ncaused by:" ).append( le.stackTrace );
+		}
 		return buf.toString();
 	}
 }