28
28
import io .cdap .cdap .api .annotation .Name ;
29
29
import io .cdap .cdap .api .annotation .Plugin ;
30
30
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 ;
31
34
import io .cdap .cdap .etl .api .FailureCollector ;
32
35
import io .cdap .cdap .etl .api .PipelineConfigurer ;
33
36
import io .cdap .cdap .etl .api .StageConfigurer ;
@@ -81,7 +84,7 @@ public void configurePipeline(PipelineConfigurer configurer) {
81
84
}
82
85
83
86
@ Override
84
- public void run (ActionContext context ) throws Exception {
87
+ public void run (ActionContext context ) {
85
88
config .validateProperties (context .getFailureCollector ());
86
89
String fileContent = GCSArgumentSetter .getContent (config );
87
90
@@ -94,27 +97,36 @@ public void run(ActionContext context) throws Exception {
94
97
if (value != null ) {
95
98
context .getArguments ().set (name , value );
96
99
} 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 );
99
103
}
100
104
}
101
105
} 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 );
105
109
}
106
110
}
107
111
108
- private static Storage getStorage (GCSArgumentSetterConfig config ) throws IOException {
112
+ private static Storage getStorage (GCSArgumentSetterConfig config ) {
109
113
String serviceAccount = config .getServiceAccount ();
110
114
StorageOptions .Builder builder = StorageOptions .newBuilder ().setProjectId (config .getProject ());
111
115
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
+ }
113
125
}
114
126
return builder .build ().getService ();
115
127
}
116
128
117
- public static String getContent (GCSArgumentSetterConfig config ) throws IOException {
129
+ public static String getContent (GCSArgumentSetterConfig config ) {
118
130
Storage storage = getStorage (config );
119
131
GCSPath path = config .getPath ();
120
132
Blob blob = storage .get (path .getBucket (), path .getName ());
@@ -150,7 +162,9 @@ public void setName(String name) {
150
162
151
163
public String getValue () {
152
164
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 );
154
168
}
155
169
if (type .equalsIgnoreCase ("schema" )) {
156
170
return createSchema (value ).toString ();
@@ -180,7 +194,9 @@ public String getValue() {
180
194
return Joiner .on (";" ).join (values );
181
195
}
182
196
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 );
184
200
}
185
201
186
202
private Schema createSchema (JsonElement array ) {
0 commit comments