Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[🍒][PLUGIN-1838] Error Management catch known errors [GCSArgumentSetter] #1498

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading