// Copyright 2016 The Nomulus Authors. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package google.registry.bigquery; import static com.google.common.base.Preconditions.checkArgument; import com.google.api.client.googleapis.json.GoogleJsonError; import com.google.api.client.googleapis.json.GoogleJsonResponseException; import com.google.api.services.bigquery.model.ErrorProto; import com.google.api.services.bigquery.model.JobStatus; import com.google.common.collect.Iterables; import java.io.IOException; import javax.annotation.Nullable; /** Generic exception to throw if a Bigquery job fails. */ public final class BigqueryJobFailureException extends RuntimeException { /** Delegate {@link IOException} errors, checking for {@link GoogleJsonResponseException} */ public static BigqueryJobFailureException create(IOException cause) { if (cause instanceof GoogleJsonResponseException) { return create(((GoogleJsonResponseException) cause).getDetails()); } else { return new BigqueryJobFailureException(cause.getMessage(), cause, null, null); } } /** Create an error for JSON server response errors. */ public static BigqueryJobFailureException create(GoogleJsonError error) { return new BigqueryJobFailureException(error.getMessage(), null, null, error); } /** Create an error from a failed job. */ public static BigqueryJobFailureException create(JobStatus jobStatus) { checkArgument(jobStatus.getErrorResult() != null, "this job didn't fail!"); return new BigqueryJobFailureException( describeError(jobStatus.getErrorResult()), null, jobStatus, null); } @Nullable private final JobStatus jobStatus; @Nullable private final GoogleJsonError jsonError; private BigqueryJobFailureException( String message, @Nullable Throwable cause, @Nullable JobStatus jobStatus, @Nullable GoogleJsonError jsonError) { super(message, cause); this.jobStatus = jobStatus; this.jsonError = jsonError; } /** * Returns a short error code describing why this job failed. * *