Mercurial Hosting > luan
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 |
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 | 5 import java.util.List; |
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 | 9 public final class LuanException extends Exception implements DeepCloneable { |
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 | 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 | 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 | 24 initTable(); |
25 } | |
26 | |
27 private LuanException(String msg,Throwable cause,int nonsense) { | |
28 super(msg,cause); | |
29 } | |
30 | |
31 @Override public LuanException shallowClone() { | |
32 return new LuanException(getMessage(),getCause(),99); | |
33 } | |
34 | |
35 @Override public void deepenClone(DeepCloneable dc,DeepCloner cloner) { | |
36 LuanException clone = (LuanException)dc; | |
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 | 40 public LuanTable table() { |
689 | 41 return table; |
42 } | |
43 | |
44 private void initTable() { | |
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 | 56 table.rawPut( "get_java_stack_trace_string", new LuanJavaFunction( |
57 LuanException.class.getMethod( "getJavaStackTraceString" ), this | |
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 | 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 | 72 return getLuanStackTraceString(); |
645 | 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 | 97 */ |
682 | 98 } |
99 | |
100 public String getJavaStackTraceString() { | |
101 return getJavaStackTraceString(this); | |
102 } | |
103 | |
104 private static String getJavaStackTraceString(Throwable th) { | |
645 | 105 StringWriter sw = new StringWriter(); |
682 | 106 th.printStackTrace(new PrintWriter(sw)); |
645 | 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 | 110 private List<StackTraceElement> justLuan() { |
111 List<StackTraceElement> list = new ArrayList<StackTraceElement>(); | |
112 StackTraceElement[] orig = getStackTrace(); | |
113 for( int i=0; i<orig.length; i++ ) { | |
114 StackTraceElement ste = orig[i]; | |
115 if( !ste.getClassName().startsWith("luan.impl.EXP") ) | |
116 continue; | |
117 list.add(ste); | |
118 if( !ste.getMethodName().equals("doCall") ) | |
119 i++; | |
120 } | |
121 return list; | |
122 } | |
123 | |
124 public String getLuanStackTraceString() { | |
125 StringBuilder sb = new StringBuilder(); | |
126 sb.append( getMessage() ); | |
127 for( StackTraceElement ste : justLuan() ) { | |
128 sb.append( "\n\t" ).append( ste.getFileName() ).append( " line " ).append( ste.getLineNumber() ); | |
129 String method = ste.getMethodName(); | |
130 if( !method.equals("doCall") ) | |
131 sb.append( " in function '" ).append( method.substring(1) ).append( "'" ); | |
132 } | |
133 Throwable cause = getCause(); | |
134 if( cause != null ) | |
135 sb.append( "\nCaused by: " ).append( getJavaStackTraceString(cause) ); | |
136 return sb.toString(); | |
137 } | |
138 | |
683 | 139 public static String currentSource() { |
140 LuanException ex = new LuanException("currentSource"); | |
141 List<StackTraceElement> st = ex.justLuan(); | |
142 return st.isEmpty() ? null : st.get(0).getFileName(); | |
143 } | |
144 | |
1
2df768b926aa
start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
145 } |