2828import io .cdap .cdap .api .annotation .Name ;
2929import io .cdap .cdap .api .annotation .Plugin ;
3030import 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 ;
3134import io .cdap .cdap .etl .api .FailureCollector ;
3235import io .cdap .cdap .etl .api .PipelineConfigurer ;
3336import 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