Search in sources :

Example 6 with EvaluatorException

use of org.mozilla.javascript.EvaluatorException in project jmeter by apache.

the class BSFJavaScriptEngine method call.

/**
     * Return an object from an extension.
     * @param object Object on which to make the call (ignored).
     * @param method The name of the method to call.
     * @param args an array of arguments to be
     * passed to the extension, which may be either
     * Vectors of Nodes, or Strings.
     */
@Override
public Object call(Object object, String method, Object[] args) throws BSFException {
    Object retval = null;
    Context cx;
    try {
        cx = Context.enter();
        // REMIND: convert arg list Vectors here?
        Object fun = global.get(method, global);
        //       Any way to make these arguments *sensible?
        if (fun == Scriptable.NOT_FOUND) {
            throw new EvaluatorException("function " + method + " not found.", "none", 0);
        }
        cx.setOptimizationLevel(-1);
        cx.setGeneratingDebug(false);
        cx.setGeneratingSource(false);
        cx.setOptimizationLevel(0);
        cx.setDebugger(null, null);
        retval = ((Function) fun).call(cx, global, global, args);
        if (retval instanceof Wrapper) {
            retval = ((Wrapper) retval).unwrap();
        }
    } catch (Throwable t) {
        //NOSONAR We handle correctly Error case in function
        handleError(t);
    } finally {
        Context.exit();
    }
    return retval;
}
Also used : Context(org.mozilla.javascript.Context) Wrapper(org.mozilla.javascript.Wrapper) EvaluatorException(org.mozilla.javascript.EvaluatorException) NativeJavaObject(org.mozilla.javascript.NativeJavaObject)

Example 7 with EvaluatorException

use of org.mozilla.javascript.EvaluatorException in project jmeter by apache.

the class BSFJavaScriptEngine method handleError.

/**
     * @param t {@link Throwable}
     * @throws BSFException
     */
private void handleError(Throwable t) throws BSFException {
    Throwable target = t;
    if (t instanceof WrappedException) {
        target = ((WrappedException) t).getWrappedException();
    }
    String message = null;
    if (target instanceof JavaScriptException) {
        message = target.getLocalizedMessage();
        // Is it an exception wrapped in a JavaScriptException?
        Object value = ((JavaScriptException) target).getValue();
        if (value instanceof Throwable) {
            // likely a wrapped exception from a LiveConnect call.
            // Display its stack trace as a diagnostic
            target = (Throwable) value;
        }
    } else if (target instanceof EvaluatorException || target instanceof SecurityException) {
        message = target.getLocalizedMessage();
    } else if (target instanceof RuntimeException) {
        message = "Internal Error: " + target.toString();
    } else if (target instanceof StackOverflowError) {
        message = "Stack Overflow";
    }
    if (message == null) {
        message = target.toString();
    }
    if (target instanceof Error && !(target instanceof StackOverflowError)) {
        // a long stacktrace would end up on the user's console
        throw (Error) target;
    } else {
        throw new BSFException(BSFException.REASON_OTHER_ERROR, "JavaScript Error: " + message, target);
    }
}
Also used : WrappedException(org.mozilla.javascript.WrappedException) BSFException(org.apache.bsf.BSFException) EvaluatorException(org.mozilla.javascript.EvaluatorException) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) JavaScriptException(org.mozilla.javascript.JavaScriptException)

Aggregations

EvaluatorException (org.mozilla.javascript.EvaluatorException)7 Context (org.mozilla.javascript.Context)2 ErrorReporter (org.mozilla.javascript.ErrorReporter)2 NativeJavaObject (org.mozilla.javascript.NativeJavaObject)2 CssCompressor (com.yahoo.platform.yui.compressor.CssCompressor)1 JavaScriptCompressor (com.yahoo.platform.yui.compressor.JavaScriptCompressor)1 CmdLineParser (jargs.gnu.CmdLineParser)1 File (java.io.File)1 PrintWriter (java.io.PrintWriter)1 StringReader (java.io.StringReader)1 StringWriter (java.io.StringWriter)1 ArrayList (java.util.ArrayList)1 Set (java.util.Set)1 RequestDispatcher (javax.servlet.RequestDispatcher)1 ServletConfig (javax.servlet.ServletConfig)1 ServletContext (javax.servlet.ServletContext)1 ServletException (javax.servlet.ServletException)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletRequestWrapper (javax.servlet.http.HttpServletRequestWrapper)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1