Search in sources :

Example 1 with CanvasError

use of com.instructure.canvasapi.model.CanvasError in project instructure-android by instructure.

the class CanvasCallback method handleHTTPError.

private void handleHTTPError(RetrofitError retrofitError) {
    Response response = retrofitError.getResponse();
    if (response == null) {
        return;
    }
    Log.e(APIHelpers.LOG_TAG, "Response code: " + response.getStatus());
    Log.e(APIHelpers.LOG_TAG, "Response body: " + response.getBody());
    CanvasError canvasError = null;
    try {
        canvasError = (CanvasError) retrofitError.getBodyAs(CanvasError.class);
    } catch (Exception exception) {
    }
    if (response.getStatus() == 200) {
        errorDelegate.generalError(retrofitError, canvasError, getContext());
    } else if (response.getStatus() == 401) {
        errorDelegate.notAuthorizedError(retrofitError, canvasError, getContext());
    } else if (response.getStatus() >= 400 && response.getStatus() < 500) {
        errorDelegate.invalidUrlError(retrofitError, getContext());
    } else if (response.getStatus() >= 500 && response.getStatus() < 600) {
        // anything there
        if (response.getStatus() == 504 && APIHelpers.isCachedResponse(response)) {
            if (!CanvasRestAdapter.isNetworkAvaliable(getContext())) {
                // Purposely not part of the above if statement. First if statement is prevent the error delegate from a 504 cache response
                // Only call when no items were cached and there isn't a network
                statusDelegate.onNoNetwork();
            }
        // do nothing
        } else {
            errorDelegate.serverError(retrofitError, getContext());
        }
    }
}
Also used : Response(retrofit.client.Response) CanvasError(com.instructure.canvasapi.model.CanvasError)

Example 2 with CanvasError

use of com.instructure.canvasapi.model.CanvasError in project instructure-android by instructure.

the class CanvasCallback method failure.

/**
 * failure calls the correct method on the ErrorDelegate that's been set.
 * @param retrofitError
 */
@Override
public void failure(RetrofitError retrofitError) {
    RetrofitCounter.decrement();
    // check if it's cancelled or detached
    if (isCancelled || getContext() == null) {
        return;
    }
    finishLoading();
    Log.e(APIHelpers.LOG_TAG, "ERROR: " + retrofitError.getUrl());
    Log.e(APIHelpers.LOG_TAG, "ERROR: " + retrofitError.getMessage());
    // Return if the failure was already handled
    if (onFailure(retrofitError)) {
        return;
    }
    if (errorDelegate == null) {
        Log.d(APIHelpers.LOG_TAG, "WARNING: No ErrorDelegate Provided ");
        return;
    }
    CanvasError canvasError;
    switch(retrofitError.getKind()) {
        case CONVERSION:
            canvasError = CanvasError.createError("Conversion Error", "An exception was thrown while (de)serializing a body");
            errorDelegate.generalError(retrofitError, canvasError, getContext());
            break;
        case HTTP:
            // A non-200 HTTP status code was received from the server.
            handleHTTPError(retrofitError);
            break;
        case NETWORK:
            // An IOException occurred while communicating to the server.
            statusDelegate.onNoNetwork();
            errorDelegate.noNetworkError(retrofitError, getContext());
            break;
        case UNEXPECTED:
            canvasError = CanvasError.createError("Unexpected Error", "An internal error occurred while attempting to execute a request.");
            errorDelegate.generalError(retrofitError, canvasError, getContext());
            break;
        default:
            canvasError = CanvasError.createError("Unexpected Error", "An unexpected error occurred.");
            errorDelegate.generalError(retrofitError, canvasError, getContext());
            break;
    }
}
Also used : CanvasError(com.instructure.canvasapi.model.CanvasError)

Aggregations

CanvasError (com.instructure.canvasapi.model.CanvasError)2 Response (retrofit.client.Response)1