/** * Determine if a given GoogleJsonError is caused by, and only by, * a rate limit being applied. * @param e The GoogleJsonError returned by the request * @return True if the error is caused by a rate limit being applied. */ public boolean rateLimited(GoogleJsonError e) { ErrorInfo errorInfo = getErrorInfo(e); if (errorInfo != null) { String domain = errorInfo.getDomain(); String reason = errorInfo.getReason(); boolean isRateLimitedOrGlobalDomain = USAGE_LIMITS_DOMAIN.equals(domain) || GLOBAL_DOMAIN.equals(domain); boolean isRateLimitedReason = RATE_LIMITED_REASON_CODE.equals(reason) || USER_RATE_LIMITED_REASON_CODE.equals(reason); return isRateLimitedOrGlobalDomain && isRateLimitedReason; } return false; }
/** * Determine if a given GoogleJsonError is caused by, and only by, * a rate limit being applied. * @param e The GoogleJsonError returned by the request * @return True if the error is caused by a rate limit being applied. */ public boolean rateLimited(GoogleJsonError e) { ErrorInfo errorInfo = getErrorInfo(e); if (errorInfo != null) { String domain = errorInfo.getDomain(); String reason = errorInfo.getReason(); boolean isRateLimitedOrGlobalDomain = USAGE_LIMITS_DOMAIN.equals(domain) || GLOBAL_DOMAIN.equals(domain); boolean isRateLimitedReason = RATE_LIMITED_REASON_CODE.equals(reason) || USER_RATE_LIMITED_REASON_CODE.equals(reason); return isRateLimitedOrGlobalDomain && isRateLimitedReason; } return false; }
@Override public BackgroundException map(final IOException failure) { final StringBuilder buffer = new StringBuilder(); if(failure instanceof GoogleJsonResponseException) { final GoogleJsonResponseException error = (GoogleJsonResponseException) failure; this.append(buffer, error.getDetails().getMessage()); switch(error.getDetails().getCode()) { case HttpStatus.SC_FORBIDDEN: final List<GoogleJsonError.ErrorInfo> errors = error.getDetails().getErrors(); for(GoogleJsonError.ErrorInfo info : errors) { if("usageLimits".equals(info.getDomain())) { return new RetriableAccessDeniedException(buffer.toString(), Duration.ofSeconds(5), failure); } } break; } } if(failure instanceof HttpResponseException) { final HttpResponseException response = (HttpResponseException) failure; this.append(buffer, response.getStatusMessage()); return new HttpResponseExceptionMappingService().map(new org.apache.http.client .HttpResponseException(response.getStatusCode(), buffer.toString())); } return super.map(failure); } }