comparison core/src/luan/LuanException.java @ 481:5d4a78c93383

luan errors are now tables
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 11 May 2015 20:26:36 -0600
parents 9fb218211763
children 2e17b54e69d9
comparison
equal deleted inserted replaced
480:44caaa6a3d92 481:5d4a78c93383
2 2
3 import java.io.StringWriter; 3 import java.io.StringWriter;
4 import java.io.PrintWriter; 4 import java.io.PrintWriter;
5 5
6 6
7 public final class LuanException extends Exception { 7 public final class LuanException extends Exception implements DeepCloneable {
8 private final String stackTrace; 8 private LuanTable table = new LuanTable();
9 9
10 LuanException(LuanBit bit,Object msg) { 10 private LuanException(String msg,Throwable cause) {
11 super(message(msg),cause(msg)); 11 super(msg,cause);
12 stackTrace = stackTrace(bit,msg);
13 } 12 }
14 13
15 @Override public String getMessage() { 14 LuanException(LuanBit bit,Object msg) throws LuanException {
16 return super.getMessage() + stackTrace; 15 this( bit.luan.toString(msg), msg instanceof Throwable ? (Throwable)msg : null );
17 } 16 if( msg instanceof LuanException )
18 17 throw new RuntimeException();
19 public String getFullMessage() { 18 table.rawPut("message",msg);
20 String msg = getMessage(); 19 for( StackTraceElement ste : bit.stackTrace() ) {
21 Throwable cause = getCause(); 20 LuanTable tbl = new LuanTable();
22 if( cause != null ) { 21 tbl.rawPut( "source", ste.call.source.name );
23 msg += "\nCaused by: "; 22 tbl.rawPut( "line", ste.call.lineNumber() );
24 StringWriter sw = new StringWriter(); 23 tbl.rawPut( "call_to", ste.fnName );
25 cause.printStackTrace(new PrintWriter(sw)); 24 table.rawPut( table.rawLength() + 1, tbl );
26 msg += sw;
27 } 25 }
28 return msg; 26 LuanTable metatable = new LuanTable();
29 } 27 table.setMetatable(metatable);
30 28 try {
31 private String message() { 29 table.rawPut( "throw", new LuanJavaFunction(
32 return super.getMessage(); 30 LuanException.class.getMethod( "throwThis" ), this
33 } 31 ) );
34 32 metatable.rawPut( "__to_string", new LuanJavaFunction(
35 private static Throwable cause(Object msg) { 33 LuanException.class.getMethod( "__to_string", LuanState.class, LuanTable.class ), null
36 return msg instanceof Throwable ? (Throwable)msg : null; 34 ) );
37 } 35 } catch(NoSuchMethodException e) {
38 36 throw new RuntimeException(e);
39 private static String message(Object msg) {
40 if( msg instanceof LuanException ) {
41 LuanException le = (LuanException)msg;
42 return le.message();
43 /*
44 } else if( msg instanceof Throwable ) {
45 Throwable t = (Throwable)msg;
46 return t.getMessage();
47 */
48 } else {
49 return msg.toString();
50 } 37 }
51 } 38 }
52 39
53 private static String stackTrace(LuanBit bit,Object msg) { 40 @Override public LuanException shallowClone() {
41 return new LuanException(getMessage(),getCause());
42 }
43
44 @Override public void deepenClone(DeepCloneable dc,DeepCloner cloner) {
45 LuanException clone = (LuanException)dc;
46 clone.table = (LuanTable)cloner.get(table);
47 }
48
49 public LuanTable table() {
50 return table;
51 }
52
53 public void throwThis() throws LuanException {
54 throw this;
55 }
56
57 public String getFullMessage(LuanState luan) {
58 try {
59 return __to_string(luan,table);
60 } catch(LuanException e) {
61 throw new RuntimeException(e);
62 }
63 }
64
65 public static String __to_string(LuanState luan,LuanTable table) throws LuanException {
54 StringBuilder buf = new StringBuilder(); 66 StringBuilder buf = new StringBuilder();
55 buf.append( bit.stackTrace() ); 67
56 if( msg instanceof LuanException ) { 68 Object msg = table.rawGet("message");
57 LuanException le = (LuanException)msg; 69 buf.append( luan.toString(msg) );
58 buf.append( "\ncaused by:" ).append( le.stackTrace ); 70
71 for( int i = table.rawLength(); i>=1; i-- ) {
72 LuanTable tbl = (LuanTable)table.rawGet(i);
73 buf.append( "\n\t" ).append( tbl.rawGet("source") ).append( " line " ).append( tbl.rawGet("line") );
74 Object callTo = tbl.rawGet("call_to");
75 if( callTo != null )
76 buf.append( " in call to '" ).append( callTo ).append( "'" );
59 } 77 }
78
79 if( msg instanceof Throwable ) {
80 buf.append( "\nCaused by: " );
81 Throwable cause = (Throwable)msg;
82 StringWriter sw = new StringWriter();
83 cause.printStackTrace(new PrintWriter(sw));
84 buf.append( sw );
85 }
86
60 return buf.toString(); 87 return buf.toString();
61 } 88 }
89
62 } 90 }