Search in sources :

Example 1 with WrappedException

use of org.mozilla.javascript.WrappedException in project scriptographer by scriptographer.

the class RhinoScriptException method getFullMessage.

public String getFullMessage() {
    Throwable cause = getCause();
    String separator = System.getProperty("file.separator");
    if (cause instanceof RhinoException) {
        RhinoException re = (RhinoException) cause;
        StringWriter buf = new StringWriter();
        PrintWriter writer = new PrintWriter(buf);
        if (re instanceof WrappedException) {
            // Make sure we're not printing the "Wrapped ...Exception:" part
            writer.println(((WrappedException) re).getWrappedException().getMessage());
        } else {
            writer.println(re.details());
        }
        String[] stackTrace = re.getScriptStackTrace().split("\\r\\n|\\n|\\r");
        String sourceName = re.sourceName();
        if (sourceName != null) {
            int lineNumber = re.lineNumber();
            // TODO Why is this needed? Rhino bug?
            if (stackTrace.length == 0 || stackTrace[0].indexOf(sourceName + ":" + lineNumber) == -1) {
                String[] path = engine.getScriptPath(new File(sourceName));
                if (path != null)
                    writer.println("\tat " + StringUtils.join(path, separator) + ":" + lineNumber);
            }
        }
        // Parse the lines for filename:linenumber
        Pattern pattern = Pattern.compile("\\s+at\\s+(.+):(\\d+)");
        for (int i = 0; i < stackTrace.length; i++) {
            String line = stackTrace[i];
            Matcher matcher = pattern.matcher(line);
            if (matcher.find()) {
                String file = matcher.group(1);
                // that are located in base:
                if (file.indexOf(separator + "__") == -1) {
                    String[] path = engine.getScriptPath(new File(file));
                    if (path != null) {
                        writer.println("\tat " + StringUtils.join(path, separator) + ":" + matcher.group(2));
                    }
                }
            }
        }
        return buf.toString().trim();
    } else {
        String message = cause.getMessage();
        String error = cause.getClass().getSimpleName();
        if (message != null && message.length() != 0)
            error += ": " + message;
        return error;
    }
}
Also used : Pattern(java.util.regex.Pattern) WrappedException(org.mozilla.javascript.WrappedException) StringWriter(java.io.StringWriter) Matcher(java.util.regex.Matcher) RhinoException(org.mozilla.javascript.RhinoException) File(java.io.File) PrintWriter(java.io.PrintWriter)

Example 2 with WrappedException

use of org.mozilla.javascript.WrappedException in project Dragonet-Legacy by DragonetMC.

the class ScriptAPI method addMethod.

@JSFunction
public static void addMethod(String method, String handler, String ownerUID) {
    //Check if ownerUID belongs to a valid script
    Script scr = null;
    try {
        for (Script s : DragonetServer.instance().getRhino().getScripts()) {
            if (s.getUID().equals(ownerUID)) {
                scr = s;
            }
        }
    } catch (WrappedException e) {
        DragonetServer.instance().getLogger().error("[DragonetAPI] Script tried to add a method before initialization finished! Please use postInit for this.");
    }
    if (scr == null) {
        DragonetServer.instance().getLogger().error("[DragonetAPI] Script doesn't have a valid UID but is trying to register method " + method + "! Received '" + ownerUID + "', this does not belong to any script!");
        DragonetServer.instance().getLogger().error("[DragonetAPI] Method " + method + " will not be defined. This will cause issues with other scripts.");
        return;
    }
    //Check if method name is already taken
    for (CustomMethod m : CustomMethod.methods) {
        if (m.method.equals(method)) {
            DragonetServer.instance().getLogger().error("[DragonetAPI] Script " + scr.getUID() + " (" + scr.getName() + ")" + " tried to reserve method " + method + ", but this has already been reserved by " + m.owner.getName() + "!");
            DragonetServer.instance().getLogger().error("[DragonetAPI] Method " + method + " will not be defined. This will cause issues with other scripts.");
        }
    }
    //Finally, add method
    CustomMethod.methods.add(new CustomMethod(method, handler, scr));
    DragonetServer.instance().getLogger().info("[DragonetAPI] Script " + scr.getUID() + " (" + scr.getName() + ") added API method " + method + " sucessfully.");
}
Also used : Script(org.dragonet.rhino.Script) WrappedException(org.mozilla.javascript.WrappedException) CustomMethod(org.dragonet.rhino.CustomMethod) JSFunction(org.mozilla.javascript.annotations.JSFunction)

Example 3 with WrappedException

use of org.mozilla.javascript.WrappedException in project hackpad by dropbox.

the class ContinuationsApiTest method testErrorOnEvalCall.

/**
   * Since a continuation can only capture JavaScript frames and not Java
   * frames, ensure that Rhino throws an exception when the JavaScript frames
   * don't reach all the way to the code called by
   * executeScriptWithContinuations or callFunctionWithContinuations.
   */
public void testErrorOnEvalCall() {
    Context cx = Context.enter();
    try {
        // must use interpreter mode
        cx.setOptimizationLevel(-1);
        Script script = cx.compileString("eval('myObject.f(3);');", "test source", 1, null);
        cx.executeScriptWithContinuations(script, globalScope);
        fail("Should throw IllegalStateException");
    } catch (WrappedException we) {
        Throwable t = we.getWrappedException();
        assertTrue(t instanceof IllegalStateException);
        assertTrue(t.getMessage().startsWith("Cannot capture continuation"));
    } finally {
        Context.exit();
    }
}
Also used : Context(org.mozilla.javascript.Context) Script(org.mozilla.javascript.Script) WrappedException(org.mozilla.javascript.WrappedException)

Example 4 with WrappedException

use of org.mozilla.javascript.WrappedException 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

WrappedException (org.mozilla.javascript.WrappedException)4 File (java.io.File)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 BSFException (org.apache.bsf.BSFException)1 CustomMethod (org.dragonet.rhino.CustomMethod)1 Script (org.dragonet.rhino.Script)1 Context (org.mozilla.javascript.Context)1 EvaluatorException (org.mozilla.javascript.EvaluatorException)1 JavaScriptException (org.mozilla.javascript.JavaScriptException)1 NativeJavaObject (org.mozilla.javascript.NativeJavaObject)1 RhinoException (org.mozilla.javascript.RhinoException)1 Script (org.mozilla.javascript.Script)1 JSFunction (org.mozilla.javascript.annotations.JSFunction)1