44
44
import io .cdap .cdap .api .annotation .Macro ;
45
45
import io .cdap .cdap .api .annotation .Name ;
46
46
import io .cdap .cdap .api .annotation .Plugin ;
47
+ import io .cdap .cdap .api .exception .ErrorCategory ;
48
+ import io .cdap .cdap .api .exception .ErrorType ;
49
+ import io .cdap .cdap .api .exception .ErrorUtils ;
47
50
import io .cdap .cdap .etl .api .FailureCollector ;
48
51
import io .cdap .cdap .etl .api .action .Action ;
49
52
import io .cdap .cdap .etl .api .action .ActionContext ;
52
55
import io .cdap .plugin .gcp .bigquery .sink .BigQuerySinkUtils ;
53
56
import io .cdap .plugin .gcp .bigquery .util .BigQueryUtil ;
54
57
import io .cdap .plugin .gcp .common .CmekUtils ;
58
+ import io .cdap .plugin .gcp .common .GCPErrorDetailsProviderUtil ;
55
59
import io .cdap .plugin .gcp .common .GCPUtils ;
56
60
import org .slf4j .Logger ;
57
61
import org .slf4j .LoggerFactory ;
@@ -93,7 +97,7 @@ public final class BigQueryExecute extends AbstractBigQueryAction {
93
97
}
94
98
95
99
@ Override
96
- public void run (ActionContext context ) throws Exception {
100
+ public void run (ActionContext context ) {
97
101
FailureCollector collector = context .getFailureCollector ();
98
102
config .validate (collector , context .getArguments ().asMap ());
99
103
QueryJobConfiguration .Builder builder = QueryJobConfiguration .newBuilder (config .getSql ());
@@ -125,9 +129,16 @@ public void run(ActionContext context) throws Exception {
125
129
builder .setUseLegacySql (config .isLegacySQL ());
126
130
127
131
// API request - starts the query.
128
- Credentials credentials = config .getServiceAccount () == null ?
129
- null : GCPUtils .loadServiceAccountCredentials (config .getServiceAccount (),
130
- config .isServiceAccountFilePath ());
132
+ Credentials credentials = null ;
133
+ try {
134
+ credentials = config .getServiceAccount () == null ?
135
+ null : GCPUtils .loadServiceAccountCredentials (config .getServiceAccount (),
136
+ config .isServiceAccountFilePath ());
137
+ } catch (IOException e ) {
138
+ collector .addFailure (String .format ("Failed to load service account credentials: %s" , e .getMessage ()), null )
139
+ .withStacktrace (e .getStackTrace ());
140
+ context .getFailureCollector ().getOrThrowException ();
141
+ }
131
142
BigQuery bigQuery = GCPUtils .getBigQuery (config .getProject (), credentials , config .getReadTimeout ());
132
143
//create dataset to store the results if not exists
133
144
if (config .getStoreResults () && !Strings .isNullOrEmpty (datasetName ) &&
@@ -152,23 +163,35 @@ public void run(ActionContext context) throws Exception {
152
163
try {
153
164
executeQueryWithExponentialBackoff (bigQuery , queryConfig , context );
154
165
} catch (Throwable e ) {
155
- throw new RuntimeException (e );
166
+ if (e instanceof Exception ) {
167
+ String error =
168
+ String .format ("Failed to execute query with exponential backoff with message: %s" , e .getMessage ());
169
+ throw GCPErrorDetailsProviderUtil .getHttpResponseExceptionDetailsFromChain ((Exception ) e , error ,
170
+ ErrorType .USER , true , GCPUtils .BQ_SUPPORTED_DOC_URL );
171
+ }
172
+ String errorMessage = String .format ("Failed to execute query with exponential backoff with message: %s" ,
173
+ e .getMessage ());
174
+ throw ErrorUtils .getProgramFailureException (new ErrorCategory (ErrorCategory .ErrorCategoryEnum .PLUGIN ),
175
+ errorMessage , errorMessage , ErrorType .UNKNOWN , true , null );
156
176
}
157
177
} else {
158
- executeQuery (bigQuery , queryConfig , context );
178
+ try {
179
+ executeQuery (bigQuery , queryConfig , context );
180
+ } catch (Exception e ) {
181
+ String errorMessage = String .format ("Failed to execute query with message: %s" , e .getMessage ());
182
+ throw GCPErrorDetailsProviderUtil .getHttpResponseExceptionDetailsFromChain (e , errorMessage , ErrorType .UNKNOWN ,
183
+ true , GCPUtils .BQ_SUPPORTED_DOC_URL );
184
+ }
159
185
}
160
186
}
161
187
162
- protected void executeQueryWithExponentialBackoff (BigQuery bigQuery ,
163
- QueryJobConfiguration queryConfig , ActionContext context )
164
- throws Throwable {
188
+ void executeQueryWithExponentialBackoff (BigQuery bigQuery ,
189
+ QueryJobConfiguration queryConfig , ActionContext context ) throws Throwable {
165
190
try {
166
191
Failsafe .with (getRetryPolicy ()).run (() -> executeQuery (bigQuery , queryConfig , context ));
167
192
} catch (FailsafeException e ) {
168
- if (e .getCause () != null ) {
169
- throw e .getCause ();
170
- }
171
- throw e ;
193
+ throw GCPErrorDetailsProviderUtil .getHttpResponseExceptionDetailsFromChain (e , e .getCause ().getMessage (),
194
+ ErrorType .SYSTEM , true , GCPUtils .BQ_SUPPORTED_DOC_URL );
172
195
}
173
196
}
174
197
@@ -203,7 +226,9 @@ private void executeQuery(BigQuery bigQuery, QueryJobConfiguration queryConfig,
203
226
if (RETRY_ON_REASON .contains (e .getError ().getReason ())) {
204
227
throw new BigQueryJobExecutionException (e .getError ().getMessage (), e );
205
228
}
206
- throw new RuntimeException (e );
229
+ String error = String .format ("Failed to execute query with message: %s" , e .getMessage ());
230
+ throw GCPErrorDetailsProviderUtil .getHttpResponseExceptionDetailsFromChain (e , error , ErrorType .UNKNOWN ,
231
+ true , GCPUtils .GCS_SUPPORTED_DOC_URL );
207
232
}
208
233
209
234
// Check for errors
@@ -214,7 +239,9 @@ private void executeQuery(BigQuery bigQuery, QueryJobConfiguration queryConfig,
214
239
if (RETRY_ON_REASON .contains (queryJob .getStatus ().getError ().getReason ())) {
215
240
throw new BigQueryJobExecutionException (queryJob .getStatus ().getError ().getMessage ());
216
241
}
217
- throw new RuntimeException (queryJob .getStatus ().getError ().getMessage ());
242
+ String error = queryJob .getStatus ().getError ().getMessage ();
243
+ throw ErrorUtils .getProgramFailureException (new ErrorCategory (ErrorCategory .ErrorCategoryEnum .PLUGIN ),
244
+ error , error , ErrorType .UNKNOWN , false , null );
218
245
}
219
246
220
247
TableResult queryResults = queryJob .getQueryResults ();
0 commit comments