use of org.matrix.androidsdk.core.model.HttpError 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);
}
}
}
}
}
use of org.matrix.androidsdk.core.model.HttpError 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);
}
}
}
}
use of org.matrix.androidsdk.core.model.HttpError in project matrix-android-sdk by matrix-org.
the class DefaultRetrofit2ResponseHandler method handleResponse.
public static <T> void handleResponse(Response<T> response, Listener<T> listener) throws IOException {
if (response.isSuccessful()) {
listener.onSuccess(response);
} else {
String errorBody = response.errorBody().string();
listener.onHttpError(new HttpError(errorBody, response.code()));
}
}
Aggregations