Mercurial Hosting > luan
view 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 source
package luan; import java.io.StringWriter; import java.io.PrintWriter; import java.util.List; import java.util.ArrayList; public final class LuanException extends Exception { public LuanException(String msg,Throwable cause) { super(msg,cause); } public LuanException(String msg) { super(msg); } public LuanException(Throwable cause) { super(cause); } public LuanTable table() { LuanTable table = new LuanTable(); table.rawPut( "java", this ); /* for( StackTraceElement ste : luan.stackTrace ) { LuanTable tbl = new LuanTable(); tbl.rawPut( "source", ste.call.source.name ); tbl.rawPut( "line", ste.call.lineNumber() ); tbl.rawPut( "call_to", ste.fnName ); table.rawPut( table.rawLength() + 1, tbl ); } */ LuanTable metatable = new LuanTable(); table.setMetatable(metatable); try { table.rawPut( "get_message", new LuanJavaFunction( LuanException.class.getMethod( "getMessage" ), this ) ); 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( "getFullMessage" ), this ) ); } catch(NoSuchMethodException e) { throw new RuntimeException(e); } return table; } public void throwThis() throws LuanException { throw this; } public String getFullMessage() { return getLuanStackTraceString(); /* StringBuilder buf = new StringBuilder(); Object msg = table.rawGet("message"); String msgStr = (String)table.rawGet("message_string"); buf.append( msgStr ); for( int i = table.rawLength(); i>=1; i-- ) { LuanTable tbl = (LuanTable)table.rawGet(i); buf.append( "\n\t" ).append( tbl.rawGet("source") ).append( " line " ).append( tbl.rawGet("line") ); Object callTo = tbl.rawGet("call_to"); if( callTo != null ) buf.append( " in call to '" ).append( callTo ).append( "'" ); } if( msg instanceof Throwable ) { buf.append( "\nCaused by: " ); Throwable cause = (Throwable)msg; StringWriter sw = new StringWriter(); cause.printStackTrace(new PrintWriter(sw)); buf.append( sw ); } return buf.toString(); */ } public String getJavaStackTraceString() { return getJavaStackTraceString(this); } private static String getJavaStackTraceString(Throwable th) { StringWriter sw = new StringWriter(); 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(); } }