Search in sources :

Example 1 with HttpException

use of org.matrix.androidsdk.core.model.HttpException in project matrix-android-sdk by matrix-org.

the class RestAdapterCallback method failure.

/**
 * Default failure implementation that calls the right error handler
 *
 * @param response  the retrofit response
 * @param exception the retrofit exception
 */
public void failure(Response<T> response, Exception exception) {
    if (null != mEventDescription) {
        String error = exception != null ? exception.getMessage() : (response != null ? response.message() : "unknown");
        Log.d(LOG_TAG, "## failure(): [" + mEventDescription + "]" + " with error " + error);
    }
    boolean retry = true;
    if (null != response) {
        retry = (response.code() < 400) || (response.code() > 500);
    }
    // do not retry if the response format is not the expected one.
    retry &= (null == exception.getCause()) || !(exception.getCause() instanceof MalformedJsonException || exception.getCause() instanceof JsonSyntaxException);
    if (retry && (null != mUnsentEventsManager)) {
        Log.d(LOG_TAG, "Add it to the UnsentEventsManager");
        mUnsentEventsManager.onEventSendingFailed(mEventDescription, mIgnoreEventTimeLifeInOffline, response, exception, mApiCallback, mRequestRetryCallBack);
    } else {
        if (exception != null && exception instanceof IOException) {
            try {
                if (null != mApiCallback) {
                    try {
                        mApiCallback.onNetworkError(exception);
                    } catch (Exception e) {
                        Log.e(LOG_TAG, "## failure(): onNetworkError " + exception.getLocalizedMessage(), exception);
                    }
                }
            } catch (Exception e) {
                // privacy
                // Log.e(LOG_TAG, "Exception NetworkError " + e.getMessage() + " while managing " + error.getUrl());
                Log.e(LOG_TAG, "## failure():  NetworkError " + e.getMessage(), e);
            }
        } else {
            // Try to convert this into a Matrix error
            MatrixError mxError;
            try {
                HttpError error = ((HttpException) exception).getHttpError();
                ResponseBody errorBody = response.errorBody();
                String bodyAsString = error.getErrorBody();
                mxError = GsonProvider.provideGson().fromJson(bodyAsString, MatrixError.class);
                mxError.mStatus = response.code();
                mxError.mReason = response.message();
                mxError.mErrorBodyAsString = bodyAsString;
            } catch (Exception e) {
                mxError = null;
            }
            if (mxError != null) {
                if (MatrixError.LIMIT_EXCEEDED.equals(mxError.errcode) && (null != mUnsentEventsManager)) {
                    mUnsentEventsManager.onEventSendingFailed(mEventDescription, mIgnoreEventTimeLifeInOffline, response, exception, mApiCallback, mRequestRetryCallBack);
                } else if (MatrixError.isConfigurationErrorCode(mxError.errcode) && (null != mUnsentEventsManager)) {
                    mUnsentEventsManager.onConfigurationErrorCode(mxError.errcode, mEventDescription);
                } else {
                    try {
                        if (null != mApiCallback) {
                            mApiCallback.onMatrixError(mxError);
                        }
                    } catch (Exception e) {
                        // privacy
                        // Log.e(LOG_TAG, "Exception MatrixError " + e.getMessage() + " while managing " + error.getUrl());
                        Log.e(LOG_TAG, "## failure():  MatrixError " + e.getMessage(), e);
                    }
                }
            } else {
                try {
                    if (null != mApiCallback) {
                        mApiCallback.onUnexpectedError(exception);
                    }
                } catch (Exception e) {
                    // privacy
                    // Log.e(LOG_TAG, "Exception UnexpectedError " + e.getMessage() + " while managing " + error.getUrl());
                    Log.e(LOG_TAG, "## failure():  UnexpectedError " + e.getMessage(), e);
                }
            }
        }
    }
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) HttpException(org.matrix.androidsdk.core.model.HttpException) IOException(java.io.IOException) HttpError(org.matrix.androidsdk.core.model.HttpError) MatrixError(org.matrix.androidsdk.core.model.MatrixError) JsonSyntaxException(com.google.gson.JsonSyntaxException) IOException(java.io.IOException) MalformedJsonException(com.google.gson.stream.MalformedJsonException) HttpException(org.matrix.androidsdk.core.model.HttpException) MalformedJsonException(com.google.gson.stream.MalformedJsonException) ResponseBody(okhttp3.ResponseBody)

Example 2 with HttpException

use of org.matrix.androidsdk.core.model.HttpException in project matrix-android-sdk by matrix-org.

the class CryptoRestAdapterCallback method failure.

/**
 * Default failure implementation that calls the right error handler
 *
 * @param response  the retrofit response
 * @param exception the retrofit exception
 */
public void failure(Response<T> response, Exception exception) {
    if (null != mEventDescription) {
        String error = exception != null ? exception.getMessage() : (response != null ? response.message() : "unknown");
        Log.d(LOG_TAG, "## failure(): [" + mEventDescription + "]" + " with error " + error);
    }
    if (exception != null && exception instanceof IOException) {
        try {
            if (null != mApiCallback) {
                try {
                    mApiCallback.onNetworkError(exception);
                } catch (Exception e) {
                    Log.e(LOG_TAG, "## failure(): onNetworkError " + exception.getLocalizedMessage(), exception);
                }
            }
        } catch (Exception e) {
            // privacy
            // Log.e(LOG_TAG, "Exception NetworkError " + e.getMessage() + " while managing " + error.getUrl());
            Log.e(LOG_TAG, "## failure():  NetworkError " + e.getMessage(), e);
        }
    } else {
        // Try to convert this into a Matrix error
        MatrixError mxError;
        try {
            HttpError error = ((HttpException) exception).getHttpError();
            ResponseBody errorBody = response.errorBody();
            String bodyAsString = error.getErrorBody();
            mxError = new Gson().fromJson(bodyAsString, MatrixError.class);
            mxError.mStatus = response.code();
            mxError.mReason = response.message();
            mxError.mErrorBodyAsString = bodyAsString;
        } catch (Exception e) {
            mxError = null;
        }
        if (mxError != null) {
            try {
                if (null != mApiCallback) {
                    mApiCallback.onMatrixError(mxError);
                }
            } catch (Exception e) {
                // privacy
                // Log.e(LOG_TAG, "Exception MatrixError " + e.getMessage() + " while managing " + error.getUrl());
                Log.e(LOG_TAG, "## failure():  MatrixError " + e.getMessage(), e);
            }
        } else {
            try {
                if (null != mApiCallback) {
                    mApiCallback.onUnexpectedError(exception);
                }
            } catch (Exception e) {
                // privacy
                // Log.e(LOG_TAG, "Exception UnexpectedError " + e.getMessage() + " while managing " + error.getUrl());
                Log.e(LOG_TAG, "## failure():  UnexpectedError " + e.getMessage(), e);
            }
        }
    }
}
Also used : Gson(com.google.gson.Gson) HttpException(org.matrix.androidsdk.core.model.HttpException) IOException(java.io.IOException) HttpError(org.matrix.androidsdk.core.model.HttpError) MatrixError(org.matrix.androidsdk.core.model.MatrixError) HttpException(org.matrix.androidsdk.core.model.HttpException) IOException(java.io.IOException) ResponseBody(okhttp3.ResponseBody)

Aggregations

IOException (java.io.IOException)2 ResponseBody (okhttp3.ResponseBody)2 HttpError (org.matrix.androidsdk.core.model.HttpError)2 HttpException (org.matrix.androidsdk.core.model.HttpException)2 MatrixError (org.matrix.androidsdk.core.model.MatrixError)2 Gson (com.google.gson.Gson)1 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 MalformedJsonException (com.google.gson.stream.MalformedJsonException)1