Skip to content

Commit c40019d

Browse files
committed
Error Management catch known errors [GCSBucketDelete]
1 parent 60f3ba0 commit c40019d

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

src/main/java/io/cdap/plugin/gcp/gcs/actions/GCSBucketDelete.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
import io.cdap.cdap.api.annotation.Macro;
2626
import io.cdap.cdap.api.annotation.Name;
2727
import io.cdap.cdap.api.annotation.Plugin;
28+
import io.cdap.cdap.api.exception.ErrorCategory;
29+
import io.cdap.cdap.api.exception.ErrorType;
30+
import io.cdap.cdap.api.exception.ErrorUtils;
2831
import io.cdap.cdap.etl.api.FailureCollector;
2932
import io.cdap.cdap.etl.api.PipelineConfigurer;
3033
import io.cdap.cdap.etl.api.action.Action;
@@ -76,8 +79,16 @@ public void run(ActionContext context) throws Exception {
7679
return;
7780
}
7881
String serviceAccount = config.getServiceAccount();
79-
Credentials credentials = serviceAccount == null ?
80-
null : GCPUtils.loadServiceAccountCredentials(serviceAccount, isServiceAccountFilePath);
82+
Credentials credentials = null;
83+
try {
84+
credentials = serviceAccount == null ? null : GCPUtils.loadServiceAccountCredentials(serviceAccount,
85+
isServiceAccountFilePath);
86+
} catch (IOException e) {
87+
String errorReason = "Failed to load service account credentials. ";
88+
context.getFailureCollector().addFailure(String.format("%s %s", errorReason, e.getMessage()), null)
89+
.withStacktrace(e.getStackTrace());
90+
context.getFailureCollector().getOrThrowException();
91+
}
8192
Map<String, String> map = GCPUtils.generateGCSAuthProperties(serviceAccount, config.getServiceAccountType());
8293
map.forEach(configuration::set);
8394
configuration.set("fs.gs.impl", "com.google.cloud.hadoop.fs.gcs.GoogleHadoopFileSystem");
@@ -99,9 +110,11 @@ public void run(ActionContext context) throws Exception {
99110
storage.get(gcsPath.getBucket());
100111
} catch (StorageException e) {
101112
// Add more descriptive error message
102-
throw new RuntimeException(
103-
String.format("Unable to access or create bucket %s. ", gcsPath.getBucket())
104-
+ "Ensure you entered the correct bucket path and have permissions for it.", e);
113+
String errorReason = String.format("Unable to access or create bucket %s. ", gcsPath.getBucket()) +
114+
"Ensure you entered the correct bucket path and have permissions for it.";
115+
String errorMessage = String.format("%s %s", errorReason, e.getMessage());
116+
throw ErrorUtils.getProgramFailureException(new ErrorCategory(ErrorCategory.ErrorCategoryEnum.PLUGIN),
117+
errorReason, errorMessage, ErrorType.USER, true, e);
105118
}
106119
String exactGCSPath = "gs://" + gcsPath.getBucket() + "/" + gcsPath.getName();
107120
if (exactGCSPath.contains("*")) {

0 commit comments

Comments
 (0)