Search in sources :

Example 1 with PathRootError

use of com.dropbox.core.v2.common.PathRootError in project dropbox-sdk-java by dropbox.

the class DbxRequestUtil method unexpectedStatus.

public static DbxException unexpectedStatus(HttpRequestor.Response response, String userId) throws NetworkIOException, BadResponseException {
    String requestId = getRequestId(response);
    String message = null;
    DbxException networkError;
    switch(response.getStatusCode()) {
        case 400:
            message = DbxRequestUtil.messageFromResponse(response, requestId);
            networkError = new BadRequestException(requestId, message);
            break;
        case 401:
            message = DbxRequestUtil.messageFromResponse(response, requestId);
            networkError = new InvalidAccessTokenException(requestId, message);
            break;
        case 403:
            try {
                ApiErrorResponse<AccessError> accessErrorResponse = new ApiErrorResponse.Serializer<AccessError>(AccessError.Serializer.INSTANCE).deserialize(response.getBody());
                if (accessErrorResponse.getUserMessage() != null) {
                    message = accessErrorResponse.getUserMessage().toString();
                }
                AccessError accessError = accessErrorResponse.getError();
                networkError = new AccessErrorException(requestId, message, accessError);
            } catch (JsonProcessingException ex) {
                throw new BadResponseException(requestId, "Bad JSON: " + ex.getMessage(), ex);
            } catch (IOException ex) {
                throw new NetworkIOException(ex);
            }
            break;
        case 422:
            try {
                ApiErrorResponse<PathRootError> pathRootErrorResponse = new ApiErrorResponse.Serializer<PathRootError>(Serializer.INSTANCE).deserialize(response.getBody());
                if (pathRootErrorResponse.getUserMessage() != null) {
                    message = pathRootErrorResponse.getUserMessage().toString();
                }
                PathRootError pathRootError = pathRootErrorResponse.getError();
                networkError = new PathRootErrorException(requestId, message, pathRootError);
            } catch (JsonProcessingException ex) {
                throw new BadResponseException(requestId, "Bad JSON: " + ex.getMessage(), ex);
            } catch (IOException ex) {
                throw new NetworkIOException(ex);
            }
            break;
        case 429:
            try {
                int backoffSecs = Integer.parseInt(getFirstHeader(response, "Retry-After"));
                networkError = new RateLimitException(requestId, message, backoffSecs, TimeUnit.SECONDS);
                break;
            } catch (NumberFormatException ex) {
                networkError = new BadResponseException(requestId, "Invalid value for HTTP header: \"Retry-After\"");
                break;
            }
        case 500:
            networkError = new ServerException(requestId, message);
            break;
        case 503:
            // API v1 may include Retry-After in 503 responses, v2 does not
            String retryAfter = getFirstHeaderMaybe(response, "Retry-After");
            try {
                if (retryAfter != null && !retryAfter.trim().isEmpty()) {
                    int backoffSecs = Integer.parseInt(retryAfter);
                    networkError = new RetryException(requestId, message, backoffSecs, TimeUnit.SECONDS);
                    break;
                } else {
                    networkError = new RetryException(requestId, message);
                    break;
                }
            } catch (NumberFormatException ex) {
                networkError = new BadResponseException(requestId, "Invalid value for HTTP header: \"Retry-After\"");
                break;
            }
        default:
            networkError = new BadResponseCodeException(requestId, "unexpected HTTP status code: " + response.getStatusCode() + ": " + message, response.getStatusCode());
    }
    DbxGlobalCallbackFactory factory = DbxRequestUtil.sharedCallbackFactory;
    if (factory != null) {
        DbxNetworkErrorCallback callback = factory.createNetworkErrorCallback(userId);
        callback.onNetworkError(networkError);
    }
    return networkError;
}
Also used : DbxGlobalCallbackFactory(com.dropbox.core.v2.callbacks.DbxGlobalCallbackFactory) PathRootError(com.dropbox.core.v2.common.PathRootError) AccessError(com.dropbox.core.v2.auth.AccessError) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) DbxNetworkErrorCallback(com.dropbox.core.v2.callbacks.DbxNetworkErrorCallback) IOException(java.io.IOException)

Aggregations

AccessError (com.dropbox.core.v2.auth.AccessError)1 DbxGlobalCallbackFactory (com.dropbox.core.v2.callbacks.DbxGlobalCallbackFactory)1 DbxNetworkErrorCallback (com.dropbox.core.v2.callbacks.DbxNetworkErrorCallback)1 PathRootError (com.dropbox.core.v2.common.PathRootError)1 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 IOException (java.io.IOException)1