Skip to content

Commit

Permalink
Merge pull request #1498 from cloudsufi/cherry-pick/c153c7c2cfe8e52a0…
Browse files Browse the repository at this point in the history
…31ad93a761746d8afe7ce96

[🍒][PLUGIN-1838] Error Management catch known errors [GCSArgumentSetter]
  • Loading branch information
psainics authored Jan 22, 2025
2 parents 13276ee + 4ef9aba commit ad2503e
Showing 1 changed file with 27 additions and 11 deletions.
38 changes: 27 additions & 11 deletions src/main/java/io/cdap/plugin/gcp/gcs/actions/GCSArgumentSetter.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
import io.cdap.cdap.api.annotation.Name;
import io.cdap.cdap.api.annotation.Plugin;
import io.cdap.cdap.api.data.schema.Schema;
import io.cdap.cdap.api.exception.ErrorCategory;
import io.cdap.cdap.api.exception.ErrorType;
import io.cdap.cdap.api.exception.ErrorUtils;
import io.cdap.cdap.etl.api.FailureCollector;
import io.cdap.cdap.etl.api.PipelineConfigurer;
import io.cdap.cdap.etl.api.StageConfigurer;
Expand Down Expand Up @@ -81,7 +84,7 @@ public void configurePipeline(PipelineConfigurer configurer) {
}

@Override
public void run(ActionContext context) throws Exception {
public void run(ActionContext context) {
config.validateProperties(context.getFailureCollector());
String fileContent = GCSArgumentSetter.getContent(config);

Expand All @@ -94,27 +97,36 @@ public void run(ActionContext context) throws Exception {
if (value != null) {
context.getArguments().set(name, value);
} else {
throw new RuntimeException(
"Configuration '" + name + "' is null. Cannot set argument to null.");
String errorReason = String.format("Configuration '%s' is null. Cannot set argument to null.", name);
throw ErrorUtils.getProgramFailureException(new ErrorCategory(ErrorCategory.ErrorCategoryEnum.PLUGIN),
errorReason, errorReason, ErrorType.USER, false, null);
}
}
} catch (JsonSyntaxException e) {
throw new RuntimeException(
String.format(
"Could not parse response from '%s': %s", config.getPath(), e.getMessage()));
String errorReason = String.format("Could not parse response from '%s': %s", config.getPath(), e.getMessage());
throw ErrorUtils.getProgramFailureException(new ErrorCategory(ErrorCategory.ErrorCategoryEnum.PLUGIN),
errorReason, errorReason, ErrorType.USER, false, null);
}
}

private static Storage getStorage(GCSArgumentSetterConfig config) throws IOException {
private static Storage getStorage(GCSArgumentSetterConfig config) {
String serviceAccount = config.getServiceAccount();
StorageOptions.Builder builder = StorageOptions.newBuilder().setProjectId(config.getProject());
if (serviceAccount != null) {
builder.setCredentials(GCPUtils.loadServiceAccountCredentials(serviceAccount, config.isServiceAccountFilePath()));
try {
builder.setCredentials(
GCPUtils.loadServiceAccountCredentials(serviceAccount, config.isServiceAccountFilePath()));
} catch (IOException e) {
String errorReason = "Failed to load service account credentials.";
String errorMessage = String.format("%s: %s", e.getClass(), e.getMessage());
throw ErrorUtils.getProgramFailureException(new ErrorCategory(ErrorCategory.ErrorCategoryEnum.PLUGIN),
errorReason, errorMessage, ErrorType.USER, false, e);
}
}
return builder.build().getService();
}

public static String getContent(GCSArgumentSetterConfig config) throws IOException {
public static String getContent(GCSArgumentSetterConfig config) {
Storage storage = getStorage(config);
GCSPath path = config.getPath();
Blob blob = storage.get(path.getBucket(), path.getName());
Expand Down Expand Up @@ -150,7 +162,9 @@ public void setName(String name) {

public String getValue() {
if (value == null) {
throw new IllegalArgumentException("Null Argument value for name '" + name + "'");
String errorReason = String.format("Null Argument value for name '%s'", name);
throw ErrorUtils.getProgramFailureException(new ErrorCategory(ErrorCategory.ErrorCategoryEnum.PLUGIN),
errorReason, errorReason, ErrorType.USER, false, null);
}
if (type.equalsIgnoreCase("schema")) {
return createSchema(value).toString();
Expand Down Expand Up @@ -180,7 +194,9 @@ public String getValue() {
return Joiner.on(";").join(values);
}

throw new IllegalArgumentException("Invalid argument value '" + value + "'");
String errorReason = String.format("Invalid argument value '%s'", value);
throw ErrorUtils.getProgramFailureException(new ErrorCategory(ErrorCategory.ErrorCategoryEnum.PLUGIN),
errorReason, errorReason, ErrorType.USER, false, null);
}

private Schema createSchema(JsonElement array) {
Expand Down

0 comments on commit ad2503e

Please sign in to comment.