int getStatus(Exception e) { if (e instanceof JavaScriptException) { JavaScriptException je = (JavaScriptException) e; Object value = ((ScriptableObject) je.getValue()).get("message", null); if (value != null) { String status = Utility.find(value.toString(), "^\\d\\d\\d\\b"); if (status != null) return Integer.parseInt(status); } } return 500; }
/** process exception */ private void processJavaScriptException(JavaScriptException e) { Object eValue = e.getValue(); if (eValue instanceof NativeJavaObject) { eValue = ((NativeJavaObject) eValue).unwrap(); } boolean isBuildException = eValue instanceof BuildException; if (isBuildException) { BuildException be = (BuildException) eValue; be.setLocation(getLocation()); throw be; } }
@Override public void service(HttpServletRequest request, HttpServletResponse response) throws IOException { response.setContentType("text/html"); String path = request.getRequestURI(); // use routes if present if (this.routes != null) { String route = this.routes.getProperty(path); if (route != null) { path = route; // then we also need to replace the HttpServletRequest.getRequestURI method // request = getRequestWrapper(request, path); } } if (path.endsWith("/")) path += "index"; String source = null; // check for changes first if (this.debug && contextCache.checkForChanges()) { if (this.pageInfoCache != null) this.pageInfoCache.clear(); } ScriptContext context = contextCache.getContext(); try { serve(request, response, context, path, null); } catch (Exception e) { int status = getStatus(e); response.setStatus(status); response.setContentType("text/plain"); System.out.println(e.toString()); if (errorPath != null && status >= 500) { try { serve(request, response, context, errorPath, e); } catch (Exception f) { e.printStackTrace(response.getWriter()); response.getWriter().write("ERROR IN ERROR HANDLER PAGE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); f.printStackTrace(response.getWriter()); } } else if (debug && status == 500) { e.printStackTrace(response.getWriter()); } else if (e instanceof JavaScriptException) { JavaScriptException je = (JavaScriptException) e; response.getWriter().write(je.getValue().toString()); } else { response.getWriter().write(e.getMessage()); } } finally { contextCache.returnContext(context); } }
/** * Call a Javascript function, identified by name, on a set of arguments. Optionally, expect it to * throw an exception. * * @param expectingException * @param functionName * @param args * @return */ public Object rhinoCallExpectingException( final Object expectingException, final String functionName, final Object... args) { Object fObj = rhinoScope.get(functionName, rhinoScope); if (!(fObj instanceof Function)) { throw new RuntimeException("Missing test function " + functionName); } Function function = (Function) fObj; try { return function.call(rhinoContext, rhinoScope, rhinoScope, args); } catch (RhinoException angryRhino) { if (expectingException != null && angryRhino instanceof JavaScriptException) { JavaScriptException jse = (JavaScriptException) angryRhino; Assert.assertEquals(jse.getValue(), expectingException); return null; } String trace = angryRhino.getScriptStackTrace(); Assert.fail("JavaScript error: " + angryRhino.toString() + " " + trace); } catch (JavaScriptAssertionFailed assertion) { Assert.fail(assertion.getMessage()); } return null; }