annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
1 package luan;
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
2
200
9fb218211763 add Package.block();
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 171
diff changeset
3 import java.io.StringWriter;
9fb218211763 add Package.block();
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 171
diff changeset
4 import java.io.PrintWriter;
682
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
5 import java.util.List;
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
6 import java.util.ArrayList;
1
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
7
200
9fb218211763 add Package.block();
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 171
diff changeset
8
682
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
9 public final class LuanException extends Exception {
1
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
10
646
cdc70de628b5 simplify LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents: 645
diff changeset
11 public LuanException(String msg,Throwable cause) {
481
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
12 super(msg,cause);
42
786699c78837 implement try-catch
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 40
diff changeset
13 }
786699c78837 implement try-catch
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 40
diff changeset
14
646
cdc70de628b5 simplify LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents: 645
diff changeset
15 public LuanException(String msg) {
cdc70de628b5 simplify LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents: 645
diff changeset
16 super(msg);
cdc70de628b5 simplify LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents: 645
diff changeset
17 }
cdc70de628b5 simplify LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents: 645
diff changeset
18
cdc70de628b5 simplify LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents: 645
diff changeset
19 public LuanException(Throwable cause) {
cdc70de628b5 simplify LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents: 645
diff changeset
20 super(cause);
cdc70de628b5 simplify LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents: 645
diff changeset
21 }
cdc70de628b5 simplify LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents: 645
diff changeset
22
682
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
23 public LuanTable table() {
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
24 LuanTable table = new LuanTable();
531
f99c79b0b426 change LuanException.getFullMessage() to not require LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 517
diff changeset
25 table.rawPut( "java", this );
645
859c0dedc8b6 remove LuanSource
Franklin Schmidt <fschmidt@gmail.com>
parents: 578
diff changeset
26 /*
576
4723d22062ce remove LuanBit
Franklin Schmidt <fschmidt@gmail.com>
parents: 575
diff changeset
27 for( StackTraceElement ste : luan.stackTrace ) {
481
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
28 LuanTable tbl = new LuanTable();
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
29 tbl.rawPut( "source", ste.call.source.name );
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
30 tbl.rawPut( "line", ste.call.lineNumber() );
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
31 tbl.rawPut( "call_to", ste.fnName );
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
32 table.rawPut( table.rawLength() + 1, tbl );
200
9fb218211763 add Package.block();
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 171
diff changeset
33 }
645
859c0dedc8b6 remove LuanSource
Franklin Schmidt <fschmidt@gmail.com>
parents: 578
diff changeset
34 */
481
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
35 LuanTable metatable = new LuanTable();
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
36 table.setMetatable(metatable);
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
37 try {
646
cdc70de628b5 simplify LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents: 645
diff changeset
38 table.rawPut( "get_message", new LuanJavaFunction(
cdc70de628b5 simplify LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents: 645
diff changeset
39 LuanException.class.getMethod( "getMessage" ), this
cdc70de628b5 simplify LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents: 645
diff changeset
40 ) );
481
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
41 table.rawPut( "throw", new LuanJavaFunction(
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
42 LuanException.class.getMethod( "throwThis" ), this
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
43 ) );
682
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
44 table.rawPut( "get_java_stack_trace_string", new LuanJavaFunction(
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
45 LuanException.class.getMethod( "getJavaStackTraceString" ), this
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
46 ) );
481
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
47 metatable.rawPut( "__to_string", new LuanJavaFunction(
682
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
48 LuanException.class.getMethod( "getFullMessage" ), this
481
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
49 ) );
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
50 } catch(NoSuchMethodException e) {
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
51 throw new RuntimeException(e);
42
786699c78837 implement try-catch
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 40
diff changeset
52 }
481
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
53 return table;
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
54 }
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
55
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
56 public void throwThis() throws LuanException {
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
57 throw this;
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
58 }
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
59
531
f99c79b0b426 change LuanException.getFullMessage() to not require LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 517
diff changeset
60 public String getFullMessage() {
682
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
61 return getLuanStackTraceString();
645
859c0dedc8b6 remove LuanSource
Franklin Schmidt <fschmidt@gmail.com>
parents: 578
diff changeset
62 /*
40
e3624b7cd603 implement stack trace
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 37
diff changeset
63 StringBuilder buf = new StringBuilder();
481
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
64
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
65 Object msg = table.rawGet("message");
531
f99c79b0b426 change LuanException.getFullMessage() to not require LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 517
diff changeset
66 String msgStr = (String)table.rawGet("message_string");
f99c79b0b426 change LuanException.getFullMessage() to not require LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 517
diff changeset
67 buf.append( msgStr );
481
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
68
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
69 for( int i = table.rawLength(); i>=1; i-- ) {
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
70 LuanTable tbl = (LuanTable)table.rawGet(i);
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
71 buf.append( "\n\t" ).append( tbl.rawGet("source") ).append( " line " ).append( tbl.rawGet("line") );
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
72 Object callTo = tbl.rawGet("call_to");
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
73 if( callTo != null )
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
74 buf.append( " in call to '" ).append( callTo ).append( "'" );
42
786699c78837 implement try-catch
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 40
diff changeset
75 }
481
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
76
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
77 if( msg instanceof Throwable ) {
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
78 buf.append( "\nCaused by: " );
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
79 Throwable cause = (Throwable)msg;
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
80 StringWriter sw = new StringWriter();
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
81 cause.printStackTrace(new PrintWriter(sw));
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
82 buf.append( sw );
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
83 }
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
84
40
e3624b7cd603 implement stack trace
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 37
diff changeset
85 return buf.toString();
645
859c0dedc8b6 remove LuanSource
Franklin Schmidt <fschmidt@gmail.com>
parents: 578
diff changeset
86 */
682
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
87 }
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
88
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
89 public String getJavaStackTraceString() {
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
90 return getJavaStackTraceString(this);
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
91 }
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
92
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
93 private static String getJavaStackTraceString(Throwable th) {
645
859c0dedc8b6 remove LuanSource
Franklin Schmidt <fschmidt@gmail.com>
parents: 578
diff changeset
94 StringWriter sw = new StringWriter();
682
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
95 th.printStackTrace(new PrintWriter(sw));
645
859c0dedc8b6 remove LuanSource
Franklin Schmidt <fschmidt@gmail.com>
parents: 578
diff changeset
96 return sw.toString();
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 2
diff changeset
97 }
481
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
98
682
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
99 private List<StackTraceElement> justLuan() {
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
100 List<StackTraceElement> list = new ArrayList<StackTraceElement>();
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
101 StackTraceElement[] orig = getStackTrace();
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
102 for( int i=0; i<orig.length; i++ ) {
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
103 StackTraceElement ste = orig[i];
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
104 if( !ste.getClassName().startsWith("luan.impl.EXP") )
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
105 continue;
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
106 list.add(ste);
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
107 if( !ste.getMethodName().equals("doCall") )
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
108 i++;
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
109 }
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
110 return list;
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
111 }
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
112
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
113 public String getLuanStackTraceString() {
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
114 StringBuilder sb = new StringBuilder();
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
115 sb.append( getMessage() );
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
116 for( StackTraceElement ste : justLuan() ) {
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
117 sb.append( "\n\t" ).append( ste.getFileName() ).append( " line " ).append( ste.getLineNumber() );
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
118 String method = ste.getMethodName();
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
119 if( !method.equals("doCall") )
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
120 sb.append( " in function '" ).append( method.substring(1) ).append( "'" );
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
121 }
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
122 Throwable cause = getCause();
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
123 if( cause != null )
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
124 sb.append( "\nCaused by: " ).append( getJavaStackTraceString(cause) );
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
125 return sb.toString();
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
126 }
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
127
1
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
128 }