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;
}
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");
}
Aggregations