summaryrefslogtreecommitdiff
path: root/StoneIsland/plugins/cordova-plugin-firebasex/src/android/JavaScriptException.java
blob: 3d423fa3524a66b64896e7e597c7d3091c9487c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package org.apache.cordova.firebase;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Exception class to log Javascript based exceptions with stacktrace.
 *
 * Picked from https://github.com/wizpanda/cordova-plugin-firebase-lib/pull/8/files
 *
 * @author https://github.com/sagrawal31/
 */
public class JavaScriptException extends Exception {

    public JavaScriptException(String message) {
        super(message);
    }

    public JavaScriptException(String message, JSONArray stackTrace) throws JSONException {
        super(message);
        this.handleStacktrace(stackTrace);
    }

    private void handleStacktrace(JSONArray stackTrace) throws JSONException {
        if (stackTrace == null) {
            return;
        }

        StackTraceElement[] trace = new StackTraceElement[stackTrace.length()];

        for (int i = 0; i < stackTrace.length(); i++) {
            JSONObject elem = stackTrace.getJSONObject(i);

            trace[i] = new StackTraceElement(
                    "undefined",
                    elem.optString("functionName", "undefined"),
                    elem.optString("fileName", "undefined"),
                    elem.optInt("lineNumber", -1)
            );
        }

        this.setStackTrace(trace);
    }
}