annotate core/src/luan/LuanException.java @ 689:51a134a8c89b

fix LuanException
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 18 Apr 2016 17:02:16 -0600
parents 67dd1449e354
children ca169567ce07
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
689
51a134a8c89b fix LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents: 683
diff changeset
9 public final class LuanException extends Exception implements DeepCloneable {
51a134a8c89b fix LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents: 683
diff changeset
10 private LuanTable table;
1
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
11
646
cdc70de628b5 simplify LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents: 645
diff changeset
12 public LuanException(String msg,Throwable cause) {
481
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
13 super(msg,cause);
689
51a134a8c89b fix LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents: 683
diff changeset
14 initTable();
42
786699c78837 implement try-catch
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 40
diff changeset
15 }
786699c78837 implement try-catch
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 40
diff changeset
16
646
cdc70de628b5 simplify LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents: 645
diff changeset
17 public LuanException(String msg) {
cdc70de628b5 simplify LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents: 645
diff changeset
18 super(msg);
689
51a134a8c89b fix LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents: 683
diff changeset
19 initTable();
646
cdc70de628b5 simplify LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents: 645
diff changeset
20 }
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 public LuanException(Throwable cause) {
cdc70de628b5 simplify LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents: 645
diff changeset
23 super(cause);
689
51a134a8c89b fix LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents: 683
diff changeset
24 initTable();
51a134a8c89b fix LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents: 683
diff changeset
25 }
51a134a8c89b fix LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents: 683
diff changeset
26
51a134a8c89b fix LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents: 683
diff changeset
27 private LuanException(String msg,Throwable cause,int nonsense) {
51a134a8c89b fix LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents: 683
diff changeset
28 super(msg,cause);
51a134a8c89b fix LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents: 683
diff changeset
29 }
51a134a8c89b fix LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents: 683
diff changeset
30
51a134a8c89b fix LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents: 683
diff changeset
31 @Override public LuanException shallowClone() {
51a134a8c89b fix LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents: 683
diff changeset
32 return new LuanException(getMessage(),getCause(),99);
51a134a8c89b fix LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents: 683
diff changeset
33 }
51a134a8c89b fix LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents: 683
diff changeset
34
51a134a8c89b fix LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents: 683
diff changeset
35 @Override public void deepenClone(DeepCloneable dc,DeepCloner cloner) {
51a134a8c89b fix LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents: 683
diff changeset
36 LuanException clone = (LuanException)dc;
51a134a8c89b fix LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents: 683
diff changeset
37 clone.table = (LuanTable)cloner.get(table);
646
cdc70de628b5 simplify LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents: 645
diff changeset
38 }
cdc70de628b5 simplify LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents: 645
diff changeset
39
682
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
40 public LuanTable table() {
689
51a134a8c89b fix LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents: 683
diff changeset
41 return table;
51a134a8c89b fix LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents: 683
diff changeset
42 }
51a134a8c89b fix LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents: 683
diff changeset
43
51a134a8c89b fix LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents: 683
diff changeset
44 private void initTable() {
51a134a8c89b fix LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents: 683
diff changeset
45 table = new LuanTable();
531
f99c79b0b426 change LuanException.getFullMessage() to not require LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 517
diff changeset
46 table.rawPut( "java", this );
481
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
47 LuanTable metatable = new LuanTable();
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
48 table.setMetatable(metatable);
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
49 try {
646
cdc70de628b5 simplify LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents: 645
diff changeset
50 table.rawPut( "get_message", new LuanJavaFunction(
cdc70de628b5 simplify LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents: 645
diff changeset
51 LuanException.class.getMethod( "getMessage" ), this
cdc70de628b5 simplify LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents: 645
diff changeset
52 ) );
481
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
53 table.rawPut( "throw", new LuanJavaFunction(
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
54 LuanException.class.getMethod( "throwThis" ), this
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
55 ) );
682
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
56 table.rawPut( "get_java_stack_trace_string", new LuanJavaFunction(
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
57 LuanException.class.getMethod( "getJavaStackTraceString" ), this
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
58 ) );
481
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
59 metatable.rawPut( "__to_string", new LuanJavaFunction(
682
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
60 LuanException.class.getMethod( "getFullMessage" ), this
481
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
61 ) );
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
62 } catch(NoSuchMethodException e) {
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
63 throw new RuntimeException(e);
42
786699c78837 implement try-catch
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 40
diff changeset
64 }
481
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
65 }
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
66
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
67 public void throwThis() throws LuanException {
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
68 throw this;
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
69 }
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
70
531
f99c79b0b426 change LuanException.getFullMessage() to not require LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 517
diff changeset
71 public String getFullMessage() {
682
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
72 return getLuanStackTraceString();
645
859c0dedc8b6 remove LuanSource
Franklin Schmidt <fschmidt@gmail.com>
parents: 578
diff changeset
73 /*
40
e3624b7cd603 implement stack trace
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 37
diff changeset
74 StringBuilder buf = new StringBuilder();
481
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
75
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
76 Object msg = table.rawGet("message");
531
f99c79b0b426 change LuanException.getFullMessage() to not require LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 517
diff changeset
77 String msgStr = (String)table.rawGet("message_string");
f99c79b0b426 change LuanException.getFullMessage() to not require LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 517
diff changeset
78 buf.append( msgStr );
481
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
79
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
80 for( int i = table.rawLength(); i>=1; i-- ) {
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
81 LuanTable tbl = (LuanTable)table.rawGet(i);
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
82 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
83 Object callTo = tbl.rawGet("call_to");
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
84 if( callTo != null )
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
85 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
86 }
481
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
87
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
88 if( msg instanceof Throwable ) {
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
89 buf.append( "\nCaused by: " );
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
90 Throwable cause = (Throwable)msg;
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
91 StringWriter sw = new StringWriter();
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
92 cause.printStackTrace(new PrintWriter(sw));
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
93 buf.append( sw );
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
94 }
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
95
40
e3624b7cd603 implement stack trace
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 37
diff changeset
96 return buf.toString();
645
859c0dedc8b6 remove LuanSource
Franklin Schmidt <fschmidt@gmail.com>
parents: 578
diff changeset
97 */
682
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
98 }
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
99
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
100 public String getJavaStackTraceString() {
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
101 return getJavaStackTraceString(this);
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
102 }
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
103
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
104 private static String getJavaStackTraceString(Throwable th) {
645
859c0dedc8b6 remove LuanSource
Franklin Schmidt <fschmidt@gmail.com>
parents: 578
diff changeset
105 StringWriter sw = new StringWriter();
682
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
106 th.printStackTrace(new PrintWriter(sw));
645
859c0dedc8b6 remove LuanSource
Franklin Schmidt <fschmidt@gmail.com>
parents: 578
diff changeset
107 return sw.toString();
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 2
diff changeset
108 }
481
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
109
682
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
110 private List<StackTraceElement> justLuan() {
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
111 List<StackTraceElement> list = new ArrayList<StackTraceElement>();
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
112 StackTraceElement[] orig = getStackTrace();
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
113 for( int i=0; i<orig.length; i++ ) {
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
114 StackTraceElement ste = orig[i];
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
115 if( !ste.getClassName().startsWith("luan.impl.EXP") )
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
116 continue;
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
117 list.add(ste);
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
118 if( !ste.getMethodName().equals("doCall") )
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
119 i++;
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
120 }
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
121 return list;
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
122 }
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
123
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
124 public String getLuanStackTraceString() {
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
125 StringBuilder sb = new StringBuilder();
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
126 sb.append( getMessage() );
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
127 for( StackTraceElement ste : justLuan() ) {
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
128 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
129 String method = ste.getMethodName();
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
130 if( !method.equals("doCall") )
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
131 sb.append( " in function '" ).append( method.substring(1) ).append( "'" );
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
132 }
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
133 Throwable cause = getCause();
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
134 if( cause != null )
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
135 sb.append( "\nCaused by: " ).append( getJavaStackTraceString(cause) );
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
136 return sb.toString();
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
137 }
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
138
683
67dd1449e354 fix java security
Franklin Schmidt <fschmidt@gmail.com>
parents: 682
diff changeset
139 public static String currentSource() {
67dd1449e354 fix java security
Franklin Schmidt <fschmidt@gmail.com>
parents: 682
diff changeset
140 LuanException ex = new LuanException("currentSource");
67dd1449e354 fix java security
Franklin Schmidt <fschmidt@gmail.com>
parents: 682
diff changeset
141 List<StackTraceElement> st = ex.justLuan();
67dd1449e354 fix java security
Franklin Schmidt <fschmidt@gmail.com>
parents: 682
diff changeset
142 return st.isEmpty() ? null : st.get(0).getFileName();
67dd1449e354 fix java security
Franklin Schmidt <fschmidt@gmail.com>
parents: 682
diff changeset
143 }
67dd1449e354 fix java security
Franklin Schmidt <fschmidt@gmail.com>
parents: 682
diff changeset
144
1
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
145 }