Skip to content

Commit abf9514

Browse files
committed
Handle exceptions caused by JSHint
So far, we only handled JavaScript errors, which are explicitly thrown by JavaScript code. We did not handle exceptions that happen in the JavaScript engine during the execution of JSHint. We now handle the latter too and distinguish both types in the error message to make the cause of the error more obvious.
1 parent 8eb7b5d commit abf9514

File tree

2 files changed

+24
-2
lines changed
  • com.eclipsesource.jshint.test/src/com/eclipsesource/jshint
  • com.eclipsesource.jshint/src/com/eclipsesource/jshint

2 files changed

+24
-2
lines changed

com.eclipsesource.jshint.test/src/com/eclipsesource/jshint/JSHint_Test.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import org.junit.Before;
2020
import org.junit.Test;
21+
import org.mozilla.javascript.EcmaError;
22+
import org.mozilla.javascript.JavaScriptException;
2123

2224
import static org.hamcrest.Matchers.containsString;
2325
import static org.hamcrest.Matchers.startsWith;
@@ -216,8 +218,25 @@ public void checkWithJavaScriptException() throws Exception {
216218
fail();
217219
} catch( RuntimeException exception ) {
218220

219-
String expected = "JavaScript exception occured in JSHint check: ERROR";
221+
String expected = "JavaScript exception thrown by JSHint: ERROR";
220222
assertThat( exception.getMessage(), startsWith( expected ) );
223+
assertSame( JavaScriptException.class, exception.getCause().getClass() );
224+
}
225+
}
226+
227+
@Test
228+
public void checkWithRhinoException() throws Exception {
229+
JSHint jsHint = new JSHint();
230+
jsHint.load( new ByteArrayInputStream( "JSHINT = function() { throw x[ 0 ]; };".getBytes() ) );
231+
232+
try {
233+
jsHint.check( "var a = 1;", handler );
234+
fail();
235+
} catch( RuntimeException exception ) {
236+
237+
String expected = "JavaScript exception caused by JSHint: ReferenceError";
238+
assertThat( exception.getMessage(), startsWith( expected ) );
239+
assertSame( EcmaError.class, exception.getCause().getClass() );
221240
}
222241
}
223242

com.eclipsesource.jshint/src/com/eclipsesource/jshint/JSHint.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,10 @@ private boolean checkCode( Context context, String code ) {
186186
Object[] args = new Object[] { code, opts };
187187
return ( (Boolean)jshint.call( context, scope, null, args ) ).booleanValue();
188188
} catch( JavaScriptException exception ) {
189-
String message = "JavaScript exception occured in JSHint check: " + exception.getMessage();
189+
String message = "JavaScript exception thrown by JSHint: " + exception.getMessage();
190+
throw new RuntimeException( message, exception );
191+
} catch( RhinoException exception ) {
192+
String message = "JavaScript exception caused by JSHint: " + exception.getMessage();
190193
throw new RuntimeException( message, exception );
191194
}
192195
}

0 commit comments

Comments
 (0)