code = ((GoogleJsonResponseException) exception).getStatusCode(); retryable = BaseServiceException.isRetryable(code, null, idempotent, retryableErrors);
if (e.getStatusCode() == 404) { U.warn(log, "Bucket doesn't exist, will create it [bucketName=" + bucketName + "]");
/** * Returns HTTP status code from the given exception. * * <p> Note: GoogleJsonResponseException.getStatusCode() method is marked final * therefore it cannot be mocked using Mockito. We use this helper so that * we can override it in tests. */ protected int getHttpStatusCode(GoogleJsonResponseException e) { return e.getStatusCode(); }
static boolean isDeterministicException(GoogleJsonResponseException e) { int statusCode = e.getStatusCode(); switch (statusCode) { case HttpStatus.TOO_MANY_REQUESTS_429: case HttpStatus.REQUEST_TIMEOUT_408: return false; default: return statusCode >= 400 && statusCode < 500; } } }
private static boolean isRetryableException(Throwable t) { if (t instanceof IOException) { if (t instanceof GoogleJsonResponseException) { return ((GoogleJsonResponseException) t).getStatusCode() / 100 == 5; } return true; } return false; }
/** * Returns HTTP status code from the given exception. * * <p> Note: GoogleJsonResponseException.getStatusCode() method is marked final * therefore it cannot be mocked using Mockito. We use this helper so that * we can override it in tests. */ protected int getHttpStatusCode(GoogleJsonResponseException e) { return e.getStatusCode(); }
try { ... } catch (IOException) { if (e instanceof GoogleJsonResponseException){ GoogleJsonResponseException ex = e; switch (ex.getStatusCode()){ case 400: ... case 404: ... /*and the rest of codes available through endpoints*/ } } else { /*Manage other exceptions, maybe connection issues?*/ } }
public static boolean isResourceExhausted(GoogleJsonResponseException e) { return e.getStatusCode() == 429 && Optional.ofNullable(e.getDetails()) .map(GcpUtil::isResourceExhausted) .orElse(false); }
public static boolean isPermissionDenied(GoogleJsonResponseException e) { return e.getStatusCode() == 403 && Optional.ofNullable(e.getDetails()) .map(GcpUtil::isPermissionDenied) .orElse(false); }
private static <T> T executeNullIfNotFound(AbstractGoogleClientRequest<T> request) throws IOException { try { return request.execute(); } catch (GoogleJsonResponseException e) { if (e.getStatusCode() == SC_NOT_FOUND) { return null; } else { throw e; } } } }
Optional<StorageObject> stat(String bucket, String object) throws IOException { try { return Optional.of(client.objects() .get(bucket, object) .execute()); } catch (GoogleJsonResponseException e) { if (e.getStatusCode() == HttpStatusCodes.STATUS_CODE_NOT_FOUND) { return Optional.absent(); } throw e; } }
public boolean keyExists(String keyName) throws IOException { try { iam.projects().serviceAccounts().keys() .get(keyName) .execute(); return true; } catch (GoogleJsonResponseException e) { // TODO: handle 403 correctly once google fixes their API if (e.getStatusCode() == 403 || e.getStatusCode() == 404) { return false; } throw e; } }
boolean datasetExists(String projectId, String datasetId) throws IOException { try { client.datasets().get(projectId, datasetId).execute(); return true; } catch (GoogleJsonResponseException e) { if (e.getStatusCode() == HttpStatusCodes.STATUS_CODE_NOT_FOUND) { return false; } throw e; } }
private String getProjectIdOfServiceAccount(String email) throws IOException { try { final ServiceAccount serviceAccount = iam.projects().serviceAccounts().get("projects/-/serviceAccounts/" + email).execute(); return serviceAccount.getProjectId(); } catch (GoogleJsonResponseException e) { if (e.getStatusCode() == 404) { logger.debug("Service account {} doesn't exist", email, e); return null; } logger.info("Cannot get project id for service account {}", email, e); return null; } }
void deleteTable(String projectId, String datasetId, String tableId) throws IOException { try { client.tables().delete(projectId, datasetId, tableId).execute(); } catch (GoogleJsonResponseException e) { if (e.getStatusCode() == HttpStatusCodes.STATUS_CODE_NOT_FOUND) { // Already deleted return; } throw e; } }
public boolean serviceAccountExists(String serviceAccount) throws IOException { try { iam.projects().serviceAccounts().get("projects/-/serviceAccounts/" + serviceAccount) .execute(); return true; } catch (GoogleJsonResponseException e) { if (e.getStatusCode() == 404) { return false; } throw e; } }
private static String resolveJsonResponseToMessage(GoogleJsonResponseException reason) { switch (reason.getStatusCode()) { case 403: return StackdriverDebuggerBundle.message("clouddebug.debug.targets.accessdenied"); default: return StackdriverDebuggerBundle.getString( "clouddebug.debug.targets.error", reason.getDetails().getMessage()); } }
/** * Creates the storage bucket used by the load test. */ private void createStorageBucket() throws IOException { try { storage.buckets().insert(projectName, new Bucket() .setName(projectName + "-cloud-pubsub-loadtest")).execute(); } catch (GoogleJsonResponseException e) { if (e.getStatusCode() != ALREADY_EXISTS) { throw e; } } }
/** * Static builder to construct a {@code GoogleApiException} from a {@link * GoogleJsonResponseException} * * @param exception a GoogleJsonResponseException that was thrown by a Google API call */ public static GoogleApiException from(GoogleJsonResponseException exception) { String message = exception.getDetails() != null ? exception.getDetails().getMessage() : exception.getMessage(); return new GoogleApiException(message, exception, exception.getStatusCode()); }
/** Create a topic if it doesn't exist. */ public static void createTopic(Pubsub client, String fullTopicName) throws IOException { System.out.println("fullTopicName " + fullTopicName); try { client.projects().topics().get(fullTopicName).execute(); } catch (GoogleJsonResponseException e) { if (e.getStatusCode() == HttpStatusCodes.STATUS_CODE_NOT_FOUND) { Topic topic = client.projects().topics().create(fullTopicName, new Topic()).execute(); System.out.printf("Topic %s was created.%n", topic.getName()); } } } }