Search in sources :

Example 1 with ConversionException

use of retrofit.converter.ConversionException in project matrix-android-sdk by matrix-org.

the class RestAdapterCallback method failure.

/**
 * Default failure implementation that calls the right error handler
 *
 * @param error the retrofit error
 */
@Override
public void failure(RetrofitError error) {
    if (null != mEventDescription) {
        Log.d(LOG_TAG, "## failure(): [" + mEventDescription + "]" + " with error " + error.getMessage());
    }
    boolean retry = true;
    if (null != error.getResponse()) {
        retry = (error.getResponse().getStatus() < 400) || (error.getResponse().getStatus() > 500);
    }
    // do not retry if the response format is not the expected one.
    retry &= (null == error.getCause()) || !(error.getCause() instanceof ConversionException);
    if (retry && (null != mUnsentEventsManager)) {
        Log.d(LOG_TAG, "Add it to the UnsentEventsManager");
        mUnsentEventsManager.onEventSendingFailed(mEventDescription, mIgnoreEventTimeLifeInOffline, error, mApiCallback, mRequestRetryCallBack);
    } else {
        if (error.isNetworkError()) {
            try {
                if (null != mApiCallback) {
                    try {
                        mApiCallback.onNetworkError(error);
                    } catch (Exception e) {
                        Log.e(LOG_TAG, "## failure(): onNetworkError " + error.getMessage());
                    }
                }
            } catch (Exception e) {
                // privacy
                // Log.e(LOG_TAG, "Exception NetworkError " + e.getMessage() + " while managing " + error.getUrl());
                Log.e(LOG_TAG, "## failure():  NetworkError " + e.getMessage());
            }
        } else {
            // Try to convert this into a Matrix error
            MatrixError mxError;
            try {
                mxError = (MatrixError) error.getBodyAs(MatrixError.class);
                mxError.mStatus = error.getResponse().getStatus();
                mxError.mReason = error.getResponse().getReason();
                TypedInput body = error.getResponse().getBody();
                if (null != body) {
                    mxError.mErrorBodyMimeType = body.mimeType();
                    mxError.mErrorBody = body;
                    try {
                        if (body instanceof TypedByteArray) {
                            mxError.mErrorBodyAsString = new String(((TypedByteArray) body).getBytes());
                        } else {
                            mxError.mErrorBodyAsString = (String) error.getBodyAs(String.class);
                        }
                    } catch (Exception castException) {
                        Log.e(LOG_TAG, "## failure(): MatrixError cannot cast the response body" + castException.getMessage());
                    }
                }
            } catch (Exception e) {
                mxError = null;
            }
            if (mxError != null) {
                if (MatrixError.LIMIT_EXCEEDED.equals(mxError.errcode) && (null != mUnsentEventsManager)) {
                    mUnsentEventsManager.onEventSendingFailed(mEventDescription, mIgnoreEventTimeLifeInOffline, error, 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());
                    }
                }
            } else {
                try {
                    if (null != mApiCallback) {
                        mApiCallback.onUnexpectedError(error);
                    }
                } catch (Exception e) {
                    // privacy
                    // Log.e(LOG_TAG, "Exception UnexpectedError " + e.getMessage() + " while managing " + error.getUrl());
                    Log.e(LOG_TAG, "## failure():  UnexpectedError " + e.getMessage());
                }
            }
        }
    }
}
Also used : ConversionException(retrofit.converter.ConversionException) TypedInput(retrofit.mime.TypedInput) TypedByteArray(retrofit.mime.TypedByteArray) MatrixError(org.matrix.androidsdk.rest.model.MatrixError) ConversionException(retrofit.converter.ConversionException)

Aggregations

MatrixError (org.matrix.androidsdk.rest.model.MatrixError)1 ConversionException (retrofit.converter.ConversionException)1 TypedByteArray (retrofit.mime.TypedByteArray)1 TypedInput (retrofit.mime.TypedInput)1