Skip to content

Commit 77e0ac8

Browse files
authored
Merge pull request #1497 from cloudsufi/fem/action/GCSArgumentSetter
[PLUGIN-1838] Error Management catch known errors [GCSArgumentSetter]
2 parents 8eaddbc + c153c7c commit 77e0ac8

File tree

1 file changed

+27
-11
lines changed

1 file changed

+27
-11
lines changed

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

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
import io.cdap.cdap.api.annotation.Name;
2929
import io.cdap.cdap.api.annotation.Plugin;
3030
import io.cdap.cdap.api.data.schema.Schema;
31+
import io.cdap.cdap.api.exception.ErrorCategory;
32+
import io.cdap.cdap.api.exception.ErrorType;
33+
import io.cdap.cdap.api.exception.ErrorUtils;
3134
import io.cdap.cdap.etl.api.FailureCollector;
3235
import io.cdap.cdap.etl.api.PipelineConfigurer;
3336
import io.cdap.cdap.etl.api.StageConfigurer;
@@ -81,7 +84,7 @@ public void configurePipeline(PipelineConfigurer configurer) {
8184
}
8285

8386
@Override
84-
public void run(ActionContext context) throws Exception {
87+
public void run(ActionContext context) {
8588
config.validateProperties(context.getFailureCollector());
8689
String fileContent = GCSArgumentSetter.getContent(config);
8790

@@ -94,27 +97,36 @@ public void run(ActionContext context) throws Exception {
9497
if (value != null) {
9598
context.getArguments().set(name, value);
9699
} else {
97-
throw new RuntimeException(
98-
"Configuration '" + name + "' is null. Cannot set argument to null.");
100+
String errorReason = String.format("Configuration '%s' is null. Cannot set argument to null.", name);
101+
throw ErrorUtils.getProgramFailureException(new ErrorCategory(ErrorCategory.ErrorCategoryEnum.PLUGIN),
102+
errorReason, errorReason, ErrorType.USER, false, null);
99103
}
100104
}
101105
} catch (JsonSyntaxException e) {
102-
throw new RuntimeException(
103-
String.format(
104-
"Could not parse response from '%s': %s", config.getPath(), e.getMessage()));
106+
String errorReason = String.format("Could not parse response from '%s': %s", config.getPath(), e.getMessage());
107+
throw ErrorUtils.getProgramFailureException(new ErrorCategory(ErrorCategory.ErrorCategoryEnum.PLUGIN),
108+
errorReason, errorReason, ErrorType.USER, false, null);
105109
}
106110
}
107111

108-
private static Storage getStorage(GCSArgumentSetterConfig config) throws IOException {
112+
private static Storage getStorage(GCSArgumentSetterConfig config) {
109113
String serviceAccount = config.getServiceAccount();
110114
StorageOptions.Builder builder = StorageOptions.newBuilder().setProjectId(config.getProject());
111115
if (serviceAccount != null) {
112-
builder.setCredentials(GCPUtils.loadServiceAccountCredentials(serviceAccount, config.isServiceAccountFilePath()));
116+
try {
117+
builder.setCredentials(
118+
GCPUtils.loadServiceAccountCredentials(serviceAccount, config.isServiceAccountFilePath()));
119+
} catch (IOException e) {
120+
String errorReason = "Failed to load service account credentials.";
121+
String errorMessage = String.format("%s: %s", e.getClass(), e.getMessage());
122+
throw ErrorUtils.getProgramFailureException(new ErrorCategory(ErrorCategory.ErrorCategoryEnum.PLUGIN),
123+
errorReason, errorMessage, ErrorType.USER, false, e);
124+
}
113125
}
114126
return builder.build().getService();
115127
}
116128

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

151163
public String getValue() {
152164
if (value == null) {
153-
throw new IllegalArgumentException("Null Argument value for name '" + name + "'");
165+
String errorReason = String.format("Null Argument value for name '%s'", name);
166+
throw ErrorUtils.getProgramFailureException(new ErrorCategory(ErrorCategory.ErrorCategoryEnum.PLUGIN),
167+
errorReason, errorReason, ErrorType.USER, false, null);
154168
}
155169
if (type.equalsIgnoreCase("schema")) {
156170
return createSchema(value).toString();
@@ -180,7 +194,9 @@ public String getValue() {
180194
return Joiner.on(";").join(values);
181195
}
182196

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

186202
private Schema createSchema(JsonElement array) {

0 commit comments

Comments
 (0)