private JSONObject buildErrorValue(Throwable e, int status, String messagePrefix) throws JSONException {
    JSONObject errorValue = new JSONObject();
    errorValue.put("class", e.getClass().getCanonicalName());

    StringWriter stringWriter = new StringWriter();
    PrintWriter printWriter = new PrintWriter(stringWriter);

    if (messagePrefix != null) {
      printWriter.append(messagePrefix);
    }

    // Also include the Selendroid stack trace. Only do this in case of unknown errors for easier debugging.
    // In case of an expected error the stack trace is unnecessary and users often find it confusing.
    if (status == StatusCode.UNKNOWN_ERROR.getCode()) {
      e.printStackTrace(printWriter);
    } else {
      printWriter.append(e.getMessage());
    }

    errorValue.put("message", stringWriter.toString());

    /*
      The WebDriver protocol does not define a way to add exception stack traces to responses.
      The workaround above puts the stack trace in the response message.
      Apparently Selenium's BeanToJsonConverter would also work.

      JSONArray stackTrace = new JSONArray();
      for (StackTraceElement el : t.getStackTrace()) {
          JSONObject frame = new JSONObject();
          frame.put("lineNumber", el.getLineNumber());
          frame.put("className", el.getClassName());
          frame.put("methodName", el.getMethodName());
          frame.put("fileName", el.getFileName());
          stackTrace.put(frame);
      }
      errorValue.put("stackTrace", stackTrace);
    */
    return errorValue;
  }
It is currently hard to detect whether a test failed because of a legitimate
error by a developer or because something is going wrong in selendroid
internals. This response marks error responses from the server that indicate
something has gone wrong in the internals of selendroid.
selendroid-server-common/src/main/java/io/selendroid/server/common/SelendroidResponse.java