comparison core/src/luan/LuanException.java @ 772:bffbef96ca6d

add public static methods to LuanException for building luan stack traces
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 23 Aug 2016 00:23:36 -0600
parents ca169567ce07
children
comparison
equal deleted inserted replaced
771:dcae4dd718b8 772:bffbef96ca6d
106 StringWriter sw = new StringWriter(); 106 StringWriter sw = new StringWriter();
107 th.printStackTrace(new PrintWriter(sw)); 107 th.printStackTrace(new PrintWriter(sw));
108 return sw.toString(); 108 return sw.toString();
109 } 109 }
110 110
111 private List<StackTraceElement> justLuan() { 111 public static List<StackTraceElement> justLuan(StackTraceElement[] orig) {
112 List<StackTraceElement> list = new ArrayList<StackTraceElement>(); 112 List<StackTraceElement> list = new ArrayList<StackTraceElement>();
113 StackTraceElement[] orig = getStackTrace();
114 for( int i=0; i<orig.length; i++ ) { 113 for( int i=0; i<orig.length; i++ ) {
115 StackTraceElement ste = orig[i]; 114 StackTraceElement ste = orig[i];
116 if( !ste.getClassName().startsWith("luan.impl.EXP") ) 115 if( !ste.getClassName().startsWith("luan.impl.EXP") )
117 continue; 116 continue;
118 list.add(ste); 117 list.add(ste);
120 i++; 119 i++;
121 } 120 }
122 return list; 121 return list;
123 } 122 }
124 123
124 public static String toString(StackTraceElement ste) {
125 StringBuilder sb = new StringBuilder();
126 sb.append( ste.getFileName() ).append( " line " ).append( ste.getLineNumber() );
127 String method = ste.getMethodName();
128 if( !method.equals("doCall") )
129 sb.append( " in function '" ).append( method.substring(1) ).append( "'" );
130 return sb.toString();
131 }
132
125 public String getLuanStackTraceString() { 133 public String getLuanStackTraceString() {
126 StringBuilder sb = new StringBuilder(); 134 StringBuilder sb = new StringBuilder();
127 sb.append( getMessage() ); 135 sb.append( getMessage() );
128 for( StackTraceElement ste : justLuan() ) { 136 for( StackTraceElement ste : justLuan(getStackTrace()) ) {
129 sb.append( "\n\t" ).append( ste.getFileName() ).append( " line " ).append( ste.getLineNumber() ); 137 sb.append( "\n\t" ).append( toString(ste) );
130 String method = ste.getMethodName();
131 if( !method.equals("doCall") )
132 sb.append( " in function '" ).append( method.substring(1) ).append( "'" );
133 } 138 }
134 Throwable cause = getCause(); 139 Throwable cause = getCause();
135 if( cause != null ) 140 if( cause != null )
136 sb.append( "\nCaused by: " ).append( getJavaStackTraceString(cause) ); 141 sb.append( "\nCaused by: " ).append( getJavaStackTraceString(cause) );
137 return sb.toString(); 142 return sb.toString();
138 } 143 }
139 144
140 public static String currentSource() { 145 public static String currentSource() {
141 LuanException ex = new LuanException("currentSource"); 146 LuanException ex = new LuanException("currentSource");
142 List<StackTraceElement> st = ex.justLuan(); 147 List<StackTraceElement> st = ex.justLuan(ex.getStackTrace());
143 return st.isEmpty() ? null : st.get(0).getFileName(); 148 return st.isEmpty() ? null : st.get(0).getFileName();
144 } 149 }
145 150
146 } 151 }