Mercurial Hosting > luan
diff core/src/luan/LuanException.java @ 682:0c334975d526
finish stack trace
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 14 Apr 2016 21:41:40 -0600 |
parents | cdc70de628b5 |
children | 67dd1449e354 |
line wrap: on
line diff
--- a/core/src/luan/LuanException.java Thu Apr 14 20:10:38 2016 -0600 +++ b/core/src/luan/LuanException.java Thu Apr 14 21:41:40 2016 -0600 @@ -2,27 +2,26 @@ import java.io.StringWriter; import java.io.PrintWriter; +import java.util.List; +import java.util.ArrayList; -public final class LuanException extends Exception implements DeepCloneable { - private LuanTable table = new LuanTable(); +public final class LuanException extends Exception { public LuanException(String msg,Throwable cause) { super(msg,cause); - setTable(); } public LuanException(String msg) { super(msg); - setTable(); } public LuanException(Throwable cause) { super(cause); - setTable(); } - private void setTable() { + public LuanTable table() { + LuanTable table = new LuanTable(); table.rawPut( "java", this ); /* for( StackTraceElement ste : luan.stackTrace ) { @@ -42,24 +41,15 @@ table.rawPut( "throw", new LuanJavaFunction( LuanException.class.getMethod( "throwThis" ), this ) ); + table.rawPut( "get_java_stack_trace_string", new LuanJavaFunction( + LuanException.class.getMethod( "getJavaStackTraceString" ), this + ) ); metatable.rawPut( "__to_string", new LuanJavaFunction( - LuanException.class.getMethod( "__to_string", LuanTable.class ), null + LuanException.class.getMethod( "getFullMessage" ), this ) ); } catch(NoSuchMethodException e) { throw new RuntimeException(e); } - } - - @Override public LuanException shallowClone() { - return new LuanException(getMessage(),getCause()); - } - - @Override public void deepenClone(DeepCloneable dc,DeepCloner cloner) { - LuanException clone = (LuanException)dc; - clone.table = (LuanTable)cloner.get(table); - } - - public LuanTable table() { return table; } @@ -68,10 +58,7 @@ } public String getFullMessage() { - return __to_string(table); - } - - public static String __to_string(LuanTable table) { + return getLuanStackTraceString(); /* StringBuilder buf = new StringBuilder(); @@ -97,10 +84,45 @@ return buf.toString(); */ - LuanException ex = (LuanException)table.rawGet("java"); + } + + public String getJavaStackTraceString() { + return getJavaStackTraceString(this); + } + + private static String getJavaStackTraceString(Throwable th) { StringWriter sw = new StringWriter(); - ex.printStackTrace(new PrintWriter(sw)); + th.printStackTrace(new PrintWriter(sw)); return sw.toString(); } + private List<StackTraceElement> justLuan() { + List<StackTraceElement> list = new ArrayList<StackTraceElement>(); + StackTraceElement[] orig = getStackTrace(); + for( int i=0; i<orig.length; i++ ) { + StackTraceElement ste = orig[i]; + if( !ste.getClassName().startsWith("luan.impl.EXP") ) + continue; + list.add(ste); + if( !ste.getMethodName().equals("doCall") ) + i++; + } + return list; + } + + public String getLuanStackTraceString() { + StringBuilder sb = new StringBuilder(); + sb.append( getMessage() ); + for( StackTraceElement ste : justLuan() ) { + sb.append( "\n\t" ).append( ste.getFileName() ).append( " line " ).append( ste.getLineNumber() ); + String method = ste.getMethodName(); + if( !method.equals("doCall") ) + sb.append( " in function '" ).append( method.substring(1) ).append( "'" ); + } + Throwable cause = getCause(); + if( cause != null ) + sb.append( "\nCaused by: " ).append( getJavaStackTraceString(cause) ); + return sb.toString(); + } + }