1 38 package com.gargoylesoftware.htmlunit; 39 40 import java.io.BufferedReader ; 41 import java.io.IOException ; 42 import java.io.PrintStream ; 43 import java.io.PrintWriter ; 44 import java.io.StringReader ; 45 import java.io.StringWriter ; 46 import org.mozilla.javascript.EcmaError; 47 import org.mozilla.javascript.JavaScriptException; 48 import org.mozilla.javascript.WrappedException; 49 50 57 public class ScriptException extends RuntimeException { 58 private static final long serialVersionUID = 4788896649084231283L; 59 private final Throwable throwable_; 60 private final String scriptSourceCode_; 61 62 63 71 public ScriptException( final Throwable throwable, final String scriptSourceCode ) { 72 super(getMessageFrom(throwable)); 73 throwable_ = throwable; 74 scriptSourceCode_ = scriptSourceCode; 75 } 76 77 private static String getMessageFrom( final Throwable throwable ) { 78 if( throwable == null ) { 79 return "null"; 80 } 81 else { 82 return throwable.getMessage(); 83 } 84 } 85 86 91 public ScriptException( final Throwable throwable ) { 92 this( throwable, null ); 93 } 94 95 96 100 public Throwable getEnclosedException() { 101 return throwable_; 102 } 103 104 108 public void printStackTrace() { 109 printStackTrace(System.out); 110 } 111 112 118 public void printStackTrace( final PrintWriter writer ) { 119 writer.write( createPrintableStackTrace() ); 120 } 121 122 123 129 public void printStackTrace( final PrintStream stream ) { 130 stream.print(createPrintableStackTrace()); 131 } 132 133 134 private String createPrintableStackTrace() { 135 final StringWriter stringWriter = new StringWriter (); 136 final PrintWriter printWriter = new PrintWriter (stringWriter); 137 138 printWriter.println("======= EXCEPTION START ========"); 139 140 if( throwable_ != null ) { 141 if( throwable_ instanceof EcmaError ) { 142 final EcmaError ecmaError = (EcmaError)throwable_; 143 printWriter.print("EcmaError: "); 144 printWriter.print("lineNumber=["); 145 printWriter.print(ecmaError.lineNumber()); 146 printWriter.print("] column=["); 147 printWriter.print(ecmaError.columnNumber()); 148 printWriter.print("] lineSource=["); 149 printWriter.print(getFailingLine()); 150 printWriter.print("] name=["); 151 printWriter.print(ecmaError.getName()); 152 printWriter.print("] sourceName=["); 153 printWriter.print(ecmaError.sourceName()); 154 printWriter.print("] message=["); 155 printWriter.print(ecmaError.getMessage()); 156 printWriter.print("]"); 157 printWriter.println(); 158 } 159 else { 160 printWriter.println("Exception class=["+throwable_.getClass().getName()+"]"); 161 } 162 } 163 164 super.printStackTrace(printWriter); 165 if( throwable_ != null && throwable_ instanceof JavaScriptException ) { 166 final Object value = ((JavaScriptException)throwable_).getValue(); 167 168 printWriter.print("JavaScriptException value = "); 169 if( value instanceof Throwable ) { 170 ((Throwable )value).printStackTrace(printWriter); 171 } 172 else { 173 printWriter.println(value); 174 } 175 } 176 else if( throwable_ != null && throwable_ instanceof WrappedException ) { 177 final WrappedException wrappedException = (WrappedException)throwable_; 178 printWriter.print("WrappedException: "); 179 wrappedException.printStackTrace(printWriter); 180 181 final Throwable innerException = wrappedException.getWrappedException(); 182 if( innerException == null ) { 183 printWriter.println("Inside wrapped exception: null"); 184 } 185 else { 186 printWriter.println("Inside wrapped exception:"); 187 innerException.printStackTrace(printWriter); 188 } 189 } 190 else if( throwable_ != null ) { 191 printWriter.println( "Enclosed exception: " ); 192 throwable_.printStackTrace( printWriter ); 193 } 194 195 if( scriptSourceCode_ != null && scriptSourceCode_.length() > 0 ) { 196 printWriter.println("== CALLING JAVASCRIPT =="); 197 printWriter.println(scriptSourceCode_); 198 } 199 printWriter.println("======= EXCEPTION END ========"); 200 201 return stringWriter.toString(); 202 } 203 204 205 209 public String getScriptSourceCode() { 210 return scriptSourceCode_; 211 } 212 213 214 221 public String getFailingLine() { 222 final int lineNumber = getFailingLineNumber(); 223 if( lineNumber == -1 ) { 224 return "<no source>"; 225 } 226 227 try { 228 final BufferedReader reader = new BufferedReader ( new StringReader (scriptSourceCode_) ); 229 for( int i=0; i<lineNumber-1; i++ ) { 230 reader.readLine(); 231 } 232 final String result = reader.readLine(); 233 reader.close(); 234 return result; 235 } 236 catch( final IOException e ) { 237 e.printStackTrace(); 239 } 240 return ""; 241 } 242 243 244 251 public int getFailingLineNumber() { 252 if( scriptSourceCode_ == null ) { 253 return -1; 254 } 255 256 if( throwable_ instanceof EcmaError ) { 257 final EcmaError ecmaError = (EcmaError)throwable_; 258 return ecmaError.lineNumber(); 259 } 260 261 return -1; 262 } 263 } 264 | Popular Tags |