use of com.dropbox.core.v2.callbacks.DbxGlobalCallbackFactory 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;
}
use of com.dropbox.core.v2.callbacks.DbxGlobalCallbackFactory in project dropbox-sdk-java by dropbox.
the class DbxWrappedException method fromResponse.
public static <T> DbxWrappedException fromResponse(StoneSerializer<T> errSerializer, HttpRequestor.Response response, String userId) throws IOException, JsonParseException {
String requestId = DbxRequestUtil.getRequestId(response);
ApiErrorResponse<T> apiResponse = new ApiErrorResponse.Serializer<T>(errSerializer).deserialize(response.getBody());
T routeError = apiResponse.getError();
DbxGlobalCallbackFactory factory = DbxRequestUtil.sharedCallbackFactory;
DbxWrappedException.executeBlockForObject(factory, userId, routeError);
DbxWrappedException.executeOtherBlocks(factory, userId, routeError);
return new DbxWrappedException(routeError, requestId, apiResponse.getUserMessage());
}
Aggregations