public static JSONObject toJsonError(Exception e) {
Throwable cause = e.getCause();
if (cause == null) cause = e;
JSONObject error = new JSONObject();
if (e instanceof ApiException) {
ApiException apiException = (ApiException)e;
error.put("name", apiException.exName);
error.put("message", apiException.getMessage());
if (apiException.stackTrace != null)
error.put("stacktrace", apiException.stackTrace);
} else {
error.put("name", e.getClass().toString());
error.put("message", e.toString());