Search in sources :

Example 1 with NashornException

use of jdk.nashorn.api.scripting.NashornException in project serverless by bluenimble.

the class Lang method toError.

public static JsonObject toError(Throwable th) {
    if (th == null) {
        return null;
    }
    Throwable first = th;
    th = Lang.getRootCause(th);
    String message = th.getMessage();
    if (message == null) {
        StackTraceElement ste = th.getStackTrace()[0];
        message = th.getClass().getSimpleName() + Lang.GREATER + Lang.SPACE + ste.getClassName() + Lang.SPACE + ", Method: " + ste.getMethodName() + ", Line: " + ste.getLineNumber();
    }
    JsonObject error = new JsonObject();
    error.set(ApiResponse.Error.Message, message);
    if (!Lang.isDebugMode()) {
        return error;
    }
    JsonArray trace = new JsonArray();
    error.set(ApiResponse.Error.Trace, trace);
    if (th instanceof NashornException) {
        if (UndefinedMessage.equals(message)) {
            error.set(ApiResponse.Error.Message, UndefinedMessage + " variable");
        } else if (message.startsWith(TypeError)) {
            error.set(ApiResponse.Error.Message, Lang.replace(message, TypeError, Lang.BLANK));
        }
        readNashornError(th, first, trace);
        return error;
    }
    StackTraceElement[] stack = th.getStackTrace();
    for (StackTraceElement line : stack) {
        JsonObject oLine = new JsonObject();
        trace.add(oLine);
        oLine.set(ApiResponse.Error.Line, line.getLineNumber());
        oLine.set(ApiResponse.Error.Function, line.getMethodName());
        String className = line.getClassName();
        int indeOfNashornScript = className.indexOf(NashornScript);
        if (indeOfNashornScript > 0) {
            String file = line.getFileName();
            // if  the error is coming from the platform core scripts
            int indexOfPath = file.indexOf(PlatformPath);
            if (indexOfPath <= 0) {
                indexOfPath = file.indexOf(ApiResourcePath);
            }
            if (indexOfPath > 0) {
                file = file.substring(indexOfPath);
            }
            oLine.set(ApiResponse.Error.File, file);
            // break here
            break;
        } else {
            int indexOfDot = className.lastIndexOf(Lang.DOT);
            if (indexOfDot > 0) {
                className = className.substring(indexOfDot + 1);
            }
            oLine.set(ApiResponse.Error.Clazz, className);
        }
    }
    return error;
}
Also used : JsonArray(com.bluenimble.platform.json.JsonArray) NashornException(jdk.nashorn.api.scripting.NashornException) JsonObject(com.bluenimble.platform.json.JsonObject)

Example 2 with NashornException

use of jdk.nashorn.api.scripting.NashornException in project serverless by bluenimble.

the class Lang method readNashornError.

private static void readNashornError(Throwable err, Throwable first, JsonArray trace) {
    NashornException exc = (NashornException) err;
    StackTraceElement[] stack = exc.getStackTrace();
    for (StackTraceElement line : stack) {
        String className = line.getClassName();
        int indeOfNashornScript = className.indexOf(NashornScript);
        if (indeOfNashornScript < 0) {
            continue;
        }
        JsonObject oLine = new JsonObject();
        String originalScript = getScriptFile(first);
        String file = line.getFileName();
        if (EvalFile.equals(file)) {
            file = originalScript;
        } else {
            file = removeScriptPath(file);
        }
        oLine.set(ApiResponse.Error.File, file);
        oLine.set(ApiResponse.Error.Line, line.getLineNumber());
        trace.add(0, oLine);
    }
}
Also used : NashornException(jdk.nashorn.api.scripting.NashornException) JsonObject(com.bluenimble.platform.json.JsonObject)

Aggregations

JsonObject (com.bluenimble.platform.json.JsonObject)2 NashornException (jdk.nashorn.api.scripting.NashornException)2 JsonArray (com.bluenimble.platform.json.JsonArray)1