Mercurial Hosting > luan
annotate src/luan/LuanException.java @ 1654:3f28ea044a32
docs UI
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Mon, 04 Apr 2022 22:19:32 -0600 |
parents | a37ffe2d1b14 |
children | 46cf5137cb6b |
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; |
1125
442abdfff437
include luan stack in LuanException.printStackTrace()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1122
diff
changeset
|
5 import java.io.PrintStream; |
682 | 6 import java.util.List; |
7 import java.util.ArrayList; | |
1335 | 8 import java.util.Map; |
9 import java.util.HashMap; | |
1
2df768b926aa
start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
10 |
200
9fb218211763
add Package.block();
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
171
diff
changeset
|
11 |
1578 | 12 public final class LuanException extends Exception implements LuanMutable { |
689 | 13 private LuanTable table; |
1335 | 14 private Map extra = new HashMap(); |
1578 | 15 private boolean immutable = false; |
1596
a9ff30fb5d89
add Hosting.push_file
Franklin Schmidt <fschmidt@gmail.com>
parents:
1578
diff
changeset
|
16 public boolean showCause = true; |
1
2df768b926aa
start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
17 |
646
cdc70de628b5
simplify LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents:
645
diff
changeset
|
18 public LuanException(String msg,Throwable cause) { |
481
5d4a78c93383
luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents:
200
diff
changeset
|
19 super(msg,cause); |
42
786699c78837
implement try-catch
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
40
diff
changeset
|
20 } |
786699c78837
implement try-catch
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
40
diff
changeset
|
21 |
646
cdc70de628b5
simplify LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents:
645
diff
changeset
|
22 public LuanException(String msg) { |
cdc70de628b5
simplify LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents:
645
diff
changeset
|
23 super(msg); |
cdc70de628b5
simplify LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents:
645
diff
changeset
|
24 } |
cdc70de628b5
simplify LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents:
645
diff
changeset
|
25 |
cdc70de628b5
simplify LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents:
645
diff
changeset
|
26 public LuanException(Throwable cause) { |
cdc70de628b5
simplify LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents:
645
diff
changeset
|
27 super(cause); |
689 | 28 } |
29 | |
1578 | 30 @Override public boolean isImmutable() { |
31 return immutable; | |
689 | 32 } |
33 | |
1578 | 34 @Override public void makeImmutable() { |
35 if(immutable) | |
36 return; | |
37 immutable = true; | |
38 LuanMutable.makeImmutable(table); | |
39 LuanMutable.makeImmutable(extra); | |
1561 | 40 } |
41 | |
1335 | 42 public void put(String key,Object value) throws LuanException { |
43 if( table == null ) { | |
44 extra.put(key,value); | |
45 } else { | |
1562 | 46 table.rawPut(key,value); |
1335 | 47 } |
646
cdc70de628b5
simplify LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents:
645
diff
changeset
|
48 } |
cdc70de628b5
simplify LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents:
645
diff
changeset
|
49 |
1333
25746915a241
merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents:
1330
diff
changeset
|
50 public LuanTable table(Luan luan) { |
783 | 51 if( table==null ) { |
52 try { | |
1283
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1282
diff
changeset
|
53 LuanTable Boot = (LuanTable)luan.require("luan:Boot.luan"); |
1618
a37ffe2d1b14
fix not_found_handler
Franklin Schmidt <fschmidt@gmail.com>
parents:
1596
diff
changeset
|
54 table = (LuanTable)Boot.fn(luan,"new_error_table").call(luan,this ); |
1335 | 55 for( Object stupid : extra.entrySet() ) { |
56 Map.Entry entry = (Map.Entry)stupid; | |
1562 | 57 table.put( luan, entry.getKey(), entry.getValue() ); |
1335 | 58 } |
1283
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1282
diff
changeset
|
59 } catch(LuanException e) { |
783 | 60 throw new RuntimeException(e); |
61 } | |
62 } | |
689 | 63 return table; |
64 } | |
65 | |
481
5d4a78c93383
luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents:
200
diff
changeset
|
66 public void throwThis() throws LuanException { |
5d4a78c93383
luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents:
200
diff
changeset
|
67 throw this; |
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 |
682 | 70 public String getJavaStackTraceString() { |
71 return getJavaStackTraceString(this); | |
72 } | |
73 | |
74 private static String getJavaStackTraceString(Throwable th) { | |
645 | 75 StringWriter sw = new StringWriter(); |
682 | 76 th.printStackTrace(new PrintWriter(sw)); |
645 | 77 return sw.toString(); |
37
8a57ebfdfd78
add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
2
diff
changeset
|
78 } |
481
5d4a78c93383
luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents:
200
diff
changeset
|
79 |
772
bffbef96ca6d
add public static methods to LuanException for building luan stack traces
Franklin Schmidt <fschmidt@gmail.com>
parents:
693
diff
changeset
|
80 public static List<StackTraceElement> justLuan(StackTraceElement[] orig) { |
682 | 81 List<StackTraceElement> list = new ArrayList<StackTraceElement>(); |
82 for( int i=0; i<orig.length; i++ ) { | |
83 StackTraceElement ste = orig[i]; | |
84 if( !ste.getClassName().startsWith("luan.impl.EXP") ) | |
85 continue; | |
86 list.add(ste); | |
87 if( !ste.getMethodName().equals("doCall") ) | |
88 i++; | |
89 } | |
90 return list; | |
91 } | |
92 | |
1568 | 93 public static LuanTable getLuanStackTrace() { |
94 return new LuanTable( justLuan( Thread.currentThread().getStackTrace() ) ); | |
95 } | |
96 | |
1400
221eedb0f54e
fix inner class gc bug
Franklin Schmidt <fschmidt@gmail.com>
parents:
1335
diff
changeset
|
97 public static String toLuanString(StackTraceElement ste) { |
221eedb0f54e
fix inner class gc bug
Franklin Schmidt <fschmidt@gmail.com>
parents:
1335
diff
changeset
|
98 int line = ste.getLineNumber(); |
221eedb0f54e
fix inner class gc bug
Franklin Schmidt <fschmidt@gmail.com>
parents:
1335
diff
changeset
|
99 String method = ste.getMethodName(); |
221eedb0f54e
fix inner class gc bug
Franklin Schmidt <fschmidt@gmail.com>
parents:
1335
diff
changeset
|
100 boolean hasMethod = !method.equals("doCall"); |
221eedb0f54e
fix inner class gc bug
Franklin Schmidt <fschmidt@gmail.com>
parents:
1335
diff
changeset
|
101 if( hasMethod ) { |
221eedb0f54e
fix inner class gc bug
Franklin Schmidt <fschmidt@gmail.com>
parents:
1335
diff
changeset
|
102 int i = method.indexOf('$'); |
221eedb0f54e
fix inner class gc bug
Franklin Schmidt <fschmidt@gmail.com>
parents:
1335
diff
changeset
|
103 if( i != -1 ) { |
221eedb0f54e
fix inner class gc bug
Franklin Schmidt <fschmidt@gmail.com>
parents:
1335
diff
changeset
|
104 int n = Integer.parseInt(method.substring(i+1)); |
221eedb0f54e
fix inner class gc bug
Franklin Schmidt <fschmidt@gmail.com>
parents:
1335
diff
changeset
|
105 line -= n; |
221eedb0f54e
fix inner class gc bug
Franklin Schmidt <fschmidt@gmail.com>
parents:
1335
diff
changeset
|
106 method = method.substring(0,i); |
221eedb0f54e
fix inner class gc bug
Franklin Schmidt <fschmidt@gmail.com>
parents:
1335
diff
changeset
|
107 if( method.equals("_") ) |
221eedb0f54e
fix inner class gc bug
Franklin Schmidt <fschmidt@gmail.com>
parents:
1335
diff
changeset
|
108 hasMethod = false; |
221eedb0f54e
fix inner class gc bug
Franklin Schmidt <fschmidt@gmail.com>
parents:
1335
diff
changeset
|
109 } |
221eedb0f54e
fix inner class gc bug
Franklin Schmidt <fschmidt@gmail.com>
parents:
1335
diff
changeset
|
110 } |
772
bffbef96ca6d
add public static methods to LuanException for building luan stack traces
Franklin Schmidt <fschmidt@gmail.com>
parents:
693
diff
changeset
|
111 StringBuilder sb = new StringBuilder(); |
1400
221eedb0f54e
fix inner class gc bug
Franklin Schmidt <fschmidt@gmail.com>
parents:
1335
diff
changeset
|
112 sb.append( ste.getFileName() ).append( " line " ).append( line ); |
221eedb0f54e
fix inner class gc bug
Franklin Schmidt <fschmidt@gmail.com>
parents:
1335
diff
changeset
|
113 if( hasMethod ) |
772
bffbef96ca6d
add public static methods to LuanException for building luan stack traces
Franklin Schmidt <fschmidt@gmail.com>
parents:
693
diff
changeset
|
114 sb.append( " in function '" ).append( method.substring(1) ).append( "'" ); |
bffbef96ca6d
add public static methods to LuanException for building luan stack traces
Franklin Schmidt <fschmidt@gmail.com>
parents:
693
diff
changeset
|
115 return sb.toString(); |
bffbef96ca6d
add public static methods to LuanException for building luan stack traces
Franklin Schmidt <fschmidt@gmail.com>
parents:
693
diff
changeset
|
116 } |
bffbef96ca6d
add public static methods to LuanException for building luan stack traces
Franklin Schmidt <fschmidt@gmail.com>
parents:
693
diff
changeset
|
117 |
1125
442abdfff437
include luan stack in LuanException.printStackTrace()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1122
diff
changeset
|
118 private StringBuilder luanStackTrace() { |
682 | 119 StringBuilder sb = new StringBuilder(); |
120 sb.append( getMessage() ); | |
772
bffbef96ca6d
add public static methods to LuanException for building luan stack traces
Franklin Schmidt <fschmidt@gmail.com>
parents:
693
diff
changeset
|
121 for( StackTraceElement ste : justLuan(getStackTrace()) ) { |
1400
221eedb0f54e
fix inner class gc bug
Franklin Schmidt <fschmidt@gmail.com>
parents:
1335
diff
changeset
|
122 sb.append( "\n\t" ).append( toLuanString(ste) ); |
682 | 123 } |
1125
442abdfff437
include luan stack in LuanException.printStackTrace()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1122
diff
changeset
|
124 return sb; |
442abdfff437
include luan stack in LuanException.printStackTrace()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1122
diff
changeset
|
125 } |
442abdfff437
include luan stack in LuanException.printStackTrace()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1122
diff
changeset
|
126 |
442abdfff437
include luan stack in LuanException.printStackTrace()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1122
diff
changeset
|
127 public String getLuanStackTraceString() { |
442abdfff437
include luan stack in LuanException.printStackTrace()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1122
diff
changeset
|
128 StringBuilder sb = luanStackTrace(); |
682 | 129 Throwable cause = getCause(); |
1596
a9ff30fb5d89
add Hosting.push_file
Franklin Schmidt <fschmidt@gmail.com>
parents:
1578
diff
changeset
|
130 if( showCause && cause != null ) |
682 | 131 sb.append( "\nCaused by: " ).append( getJavaStackTraceString(cause) ); |
132 return sb.toString(); | |
133 } | |
1400
221eedb0f54e
fix inner class gc bug
Franklin Schmidt <fschmidt@gmail.com>
parents:
1335
diff
changeset
|
134 |
1125
442abdfff437
include luan stack in LuanException.printStackTrace()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1122
diff
changeset
|
135 @Override public void printStackTrace(PrintStream s) { |
442abdfff437
include luan stack in LuanException.printStackTrace()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1122
diff
changeset
|
136 s.print("Luan: "); |
442abdfff437
include luan stack in LuanException.printStackTrace()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1122
diff
changeset
|
137 s.println(luanStackTrace()); |
442abdfff437
include luan stack in LuanException.printStackTrace()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1122
diff
changeset
|
138 s.print("Caused by: "); |
442abdfff437
include luan stack in LuanException.printStackTrace()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1122
diff
changeset
|
139 super.printStackTrace(s); |
442abdfff437
include luan stack in LuanException.printStackTrace()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1122
diff
changeset
|
140 } |
442abdfff437
include luan stack in LuanException.printStackTrace()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1122
diff
changeset
|
141 |
442abdfff437
include luan stack in LuanException.printStackTrace()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1122
diff
changeset
|
142 @Override public void printStackTrace(PrintWriter s) { |
442abdfff437
include luan stack in LuanException.printStackTrace()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1122
diff
changeset
|
143 s.print("Luan: "); |
442abdfff437
include luan stack in LuanException.printStackTrace()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1122
diff
changeset
|
144 s.println(luanStackTrace()); |
442abdfff437
include luan stack in LuanException.printStackTrace()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1122
diff
changeset
|
145 s.print("Caused by: "); |
442abdfff437
include luan stack in LuanException.printStackTrace()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1122
diff
changeset
|
146 super.printStackTrace(s); |
442abdfff437
include luan stack in LuanException.printStackTrace()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1122
diff
changeset
|
147 } |
442abdfff437
include luan stack in LuanException.printStackTrace()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1122
diff
changeset
|
148 |
1
2df768b926aa
start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
149 } |