Search in sources :

Example 1 with ErrorCode

use of io.realm.ErrorCode in project realm-java by realm.

the class AuthServerResponse method createError.

/**
     * Parse an HTTP error from a Realm Authentication Server. The server returns errors following
     * https://tools.ietf.org/html/rfc7807 with an extra "code" field for Realm specific error codes.
     *
     * @param response the server response.
     * @param httpErrorCode the HTTP error code.
     * @return an server error.
     */
public static ObjectServerError createError(String response, int httpErrorCode) {
    try {
        JSONObject obj = new JSONObject(response);
        String title = obj.optString("title", null);
        String hint = obj.optString("hint", null);
        ErrorCode errorCode = ErrorCode.fromInt(obj.optInt("code", -1));
        return new ObjectServerError(errorCode, title, hint);
    } catch (JSONException e) {
        return new ObjectServerError(ErrorCode.JSON_EXCEPTION, "Server failed with " + httpErrorCode + ", but could not parse error.", e);
    }
}
Also used : JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) ErrorCode(io.realm.ErrorCode) ObjectServerError(io.realm.ObjectServerError)

Aggregations

ErrorCode (io.realm.ErrorCode)1 ObjectServerError (io.realm.ObjectServerError)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1