annotate core/src/luan/LuanException.java @ 531:f99c79b0b426

change LuanException.getFullMessage() to not require LuanState
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 27 May 2015 22:01:40 -0600
parents 8dcf9e12446b
children f1601a4ce1aa
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;
1
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
5
200
9fb218211763 add Package.block();
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 171
diff changeset
6
481
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
7 public final class LuanException extends Exception implements DeepCloneable {
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
8 private LuanTable table = new LuanTable();
1
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
9
481
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
10 private LuanException(String msg,Throwable cause) {
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
11 super(msg,cause);
42
786699c78837 implement try-catch
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 40
diff changeset
12 }
786699c78837 implement try-catch
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 40
diff changeset
13
481
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
14 LuanException(LuanBit bit,Object msg) throws LuanException {
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
15 this( bit.luan.toString(msg), msg instanceof Throwable ? (Throwable)msg : null );
531
f99c79b0b426 change LuanException.getFullMessage() to not require LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 517
diff changeset
16 table.rawPut( "java", this );
f99c79b0b426 change LuanException.getFullMessage() to not require LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 517
diff changeset
17 table.rawPut( "message", msg );
f99c79b0b426 change LuanException.getFullMessage() to not require LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 517
diff changeset
18 table.rawPut( "message_string", bit.luan.toString(msg) );
481
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
19 for( StackTraceElement ste : bit.stackTrace() ) {
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
20 LuanTable tbl = new LuanTable();
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
21 tbl.rawPut( "source", ste.call.source.name );
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
22 tbl.rawPut( "line", ste.call.lineNumber() );
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
23 tbl.rawPut( "call_to", ste.fnName );
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
24 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
25 }
481
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
26 LuanTable metatable = new LuanTable();
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
27 table.setMetatable(metatable);
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
28 try {
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
29 table.rawPut( "throw", new LuanJavaFunction(
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
30 LuanException.class.getMethod( "throwThis" ), this
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
31 ) );
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
32 metatable.rawPut( "__to_string", new LuanJavaFunction(
531
f99c79b0b426 change LuanException.getFullMessage() to not require LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 517
diff changeset
33 LuanException.class.getMethod( "__to_string", LuanTable.class ), null
481
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
34 ) );
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
35 } catch(NoSuchMethodException e) {
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
36 throw new RuntimeException(e);
42
786699c78837 implement try-catch
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 40
diff changeset
37 }
1
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
38 }
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
39
481
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
40 @Override public LuanException shallowClone() {
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
41 return new LuanException(getMessage(),getCause());
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
42 }
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
43
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
44 @Override public void deepenClone(DeepCloneable dc,DeepCloner cloner) {
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
45 LuanException clone = (LuanException)dc;
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
46 clone.table = (LuanTable)cloner.get(table);
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
47 }
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
48
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
49 public LuanTable table() {
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
50 return table;
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
51 }
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
52
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
53 public void throwThis() throws LuanException {
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
54 throw this;
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
531
f99c79b0b426 change LuanException.getFullMessage() to not require LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 517
diff changeset
57 public String getFullMessage() {
f99c79b0b426 change LuanException.getFullMessage() to not require LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 517
diff changeset
58 return __to_string(table);
481
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
59 }
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
60
531
f99c79b0b426 change LuanException.getFullMessage() to not require LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 517
diff changeset
61 public static String __to_string(LuanTable table) {
40
e3624b7cd603 implement stack trace
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 37
diff changeset
62 StringBuilder buf = new StringBuilder();
481
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
63
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
64 Object msg = table.rawGet("message");
531
f99c79b0b426 change LuanException.getFullMessage() to not require LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 517
diff changeset
65 String msgStr = (String)table.rawGet("message_string");
f99c79b0b426 change LuanException.getFullMessage() to not require LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 517
diff changeset
66 buf.append( msgStr );
481
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
67
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
68 for( int i = table.rawLength(); i>=1; i-- ) {
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
69 LuanTable tbl = (LuanTable)table.rawGet(i);
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
70 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
71 Object callTo = tbl.rawGet("call_to");
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
72 if( callTo != null )
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
73 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
74 }
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 if( msg instanceof Throwable ) {
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
77 buf.append( "\nCaused by: " );
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
78 Throwable cause = (Throwable)msg;
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
79 StringWriter sw = new StringWriter();
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
80 cause.printStackTrace(new PrintWriter(sw));
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
81 buf.append( sw );
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
82 }
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
83
40
e3624b7cd603 implement stack trace
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 37
diff changeset
84 return buf.toString();
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 2
diff changeset
85 }
481
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 200
diff changeset
86
1
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
87 }