Mercurial Hosting > luan
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 |
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 |
682 | 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 | 23 public LuanTable table() { |
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 | 26 /* |
576 | 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 | 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 | 44 table.rawPut( "get_java_stack_trace_string", new LuanJavaFunction( |
45 LuanException.class.getMethod( "getJavaStackTraceString" ), this | |
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 | 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 | 61 return getLuanStackTraceString(); |
645 | 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 | 86 */ |
682 | 87 } |
88 | |
89 public String getJavaStackTraceString() { | |
90 return getJavaStackTraceString(this); | |
91 } | |
92 | |
93 private static String getJavaStackTraceString(Throwable th) { | |
645 | 94 StringWriter sw = new StringWriter(); |
682 | 95 th.printStackTrace(new PrintWriter(sw)); |
645 | 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 | 99 private List<StackTraceElement> justLuan() { |
100 List<StackTraceElement> list = new ArrayList<StackTraceElement>(); | |
101 StackTraceElement[] orig = getStackTrace(); | |
102 for( int i=0; i<orig.length; i++ ) { | |
103 StackTraceElement ste = orig[i]; | |
104 if( !ste.getClassName().startsWith("luan.impl.EXP") ) | |
105 continue; | |
106 list.add(ste); | |
107 if( !ste.getMethodName().equals("doCall") ) | |
108 i++; | |
109 } | |
110 return list; | |
111 } | |
112 | |
113 public String getLuanStackTraceString() { | |
114 StringBuilder sb = new StringBuilder(); | |
115 sb.append( getMessage() ); | |
116 for( StackTraceElement ste : justLuan() ) { | |
117 sb.append( "\n\t" ).append( ste.getFileName() ).append( " line " ).append( ste.getLineNumber() ); | |
118 String method = ste.getMethodName(); | |
119 if( !method.equals("doCall") ) | |
120 sb.append( " in function '" ).append( method.substring(1) ).append( "'" ); | |
121 } | |
122 Throwable cause = getCause(); | |
123 if( cause != null ) | |
124 sb.append( "\nCaused by: " ).append( getJavaStackTraceString(cause) ); | |
125 return sb.toString(); | |
126 } | |
127 | |
1
2df768b926aa
start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
128 } |