Skip to content

Commit

Permalink
Error Management catch known errors [GCSBucketDelete]
Browse files Browse the repository at this point in the history
  • Loading branch information
psainics committed Jan 21, 2025
1 parent d5a6e64 commit 5629eef
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/main/java/io/cdap/plugin/gcp/gcs/actions/GCSBucketDelete.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@
import io.cdap.cdap.api.annotation.Macro;
import io.cdap.cdap.api.annotation.Name;
import io.cdap.cdap.api.annotation.Plugin;
import io.cdap.cdap.api.exception.ErrorType;
import io.cdap.cdap.etl.api.FailureCollector;
import io.cdap.cdap.etl.api.PipelineConfigurer;
import io.cdap.cdap.etl.api.action.Action;
import io.cdap.cdap.etl.api.action.ActionContext;
import io.cdap.plugin.gcp.common.GCPConfig;
import io.cdap.plugin.gcp.common.GCPErrorDetailsProviderUtil;
import io.cdap.plugin.gcp.common.GCPUtils;
import io.cdap.plugin.gcp.gcs.GCSPath;
import org.apache.hadoop.conf.Configuration;
Expand Down Expand Up @@ -76,8 +78,17 @@ public void run(ActionContext context) throws Exception {
return;
}
String serviceAccount = config.getServiceAccount();
Credentials credentials = serviceAccount == null ?
null : GCPUtils.loadServiceAccountCredentials(serviceAccount, isServiceAccountFilePath);
Credentials credentials = null;
try {
credentials = serviceAccount == null ? null : GCPUtils.loadServiceAccountCredentials(serviceAccount,
isServiceAccountFilePath);
} catch (IOException e) {
String errorReason = "Failed to load service account credentials. ";
context.getFailureCollector()
.addFailure(String.format("%s %s: %s", errorReason, e.getClass().getName(), e.getMessage()), null)
.withStacktrace(e.getStackTrace());
context.getFailureCollector().getOrThrowException();
}
Map<String, String> map = GCPUtils.generateGCSAuthProperties(serviceAccount, config.getServiceAccountType());
map.forEach(configuration::set);
configuration.set("fs.gs.impl", "com.google.cloud.hadoop.fs.gcs.GoogleHadoopFileSystem");
Expand All @@ -99,9 +110,9 @@ public void run(ActionContext context) throws Exception {
storage.get(gcsPath.getBucket());
} catch (StorageException e) {
// Add more descriptive error message
throw new RuntimeException(
String.format("Unable to access or create bucket %s. ", gcsPath.getBucket())
+ "Ensure you entered the correct bucket path and have permissions for it.", e);
String errorReason = String.format("Unable to access GCS bucket '%s'", gcsPath.getBucket());
throw GCPErrorDetailsProviderUtil.getHttpResponseExceptionDetailsFromChain(e, errorReason, ErrorType.UNKNOWN,
true, GCPUtils.GCS_SUPPORTED_DOC_URL);
}
String exactGCSPath = "gs://" + gcsPath.getBucket() + "/" + gcsPath.getName();
if (exactGCSPath.contains("*")) {
Expand Down

0 comments on commit 5629eef

Please sign in to comment.