Search in sources :

Example 1 with AuthError

use of com.dropbox.core.v2.auth.AuthError 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);
            if (message.isEmpty()) {
                networkError = new InvalidAccessTokenException(requestId, message, AuthError.INVALID_ACCESS_TOKEN);
            } else {
                try {
                    ApiErrorResponse<AuthError> authErrorReponse = new ApiErrorResponse.Serializer<AuthError>(AuthError.Serializer.INSTANCE).deserialize(message);
                    AuthError authError = authErrorReponse.getError();
                    networkError = new InvalidAccessTokenException(requestId, message, authError);
                } catch (JsonParseException ex) {
                    throw new BadResponseException(requestId, "Bad JSON: " + ex.getMessage(), ex);
                }
            }
            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) JsonParseException(com.fasterxml.jackson.core.JsonParseException) 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) AuthError(com.dropbox.core.v2.auth.AuthError)

Example 2 with AuthError

use of com.dropbox.core.v2.auth.AuthError in project dropbox-sdk-java by dropbox.

the class DbxClientV2Test method testOnlineWontRefresh.

@Test
public void testOnlineWontRefresh() throws Exception {
    HttpRequestor mockRequestor = mock(HttpRequestor.class);
    DbxRequestConfig config = createRequestConfig().withHttpRequestor(mockRequestor).build();
    DbxCredential credential = new DbxCredential("accesstoken");
    DbxClientV2 client = new DbxClientV2(config, credential);
    FileMetadata expected = constructFileMetadate();
    HttpRequestor.Uploader mockUploader = mockUploader();
    when(mockUploader.finish()).thenReturn(createTokenExpiredResponse());
    when(mockRequestor.startPost(anyString(), anyHeaders())).thenReturn(mockUploader);
    try {
        client.files().getMetadata(expected.getId());
    } catch (InvalidAccessTokenException ex) {
        verify(mockRequestor, times(1)).startPost(anyString(), anyHeaders());
        assertThat(credential.getAccessToken()).isEqualTo("accesstoken");
        AuthError authError = DbxRequestUtil.readJsonFromErrorMessage(AuthError.Serializer.INSTANCE, ex.getMessage(), ex.getRequestId());
        assertThat(authError).isEqualTo(AuthError.EXPIRED_ACCESS_TOKEN);
        return;
    }
    fail("API v2 call should throw exception");
}
Also used : HttpRequestor(com.dropbox.core.http.HttpRequestor) AuthError(com.dropbox.core.v2.auth.AuthError) FileMetadata(com.dropbox.core.v2.files.FileMetadata) DbxCredential(com.dropbox.core.oauth.DbxCredential) Test(org.testng.annotations.Test)

Aggregations

AuthError (com.dropbox.core.v2.auth.AuthError)2 HttpRequestor (com.dropbox.core.http.HttpRequestor)1 DbxCredential (com.dropbox.core.oauth.DbxCredential)1 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 FileMetadata (com.dropbox.core.v2.files.FileMetadata)1 JsonParseException (com.fasterxml.jackson.core.JsonParseException)1 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 IOException (java.io.IOException)1 Test (org.testng.annotations.Test)1