Skip to content

Commit

Permalink
Update error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Amit-CloudSufi committed Jan 29, 2025
1 parent 2aecb46 commit 8ac8f51
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@
import io.cdap.cdap.api.annotation.Name;
import io.cdap.cdap.api.annotation.Plugin;
import io.cdap.cdap.api.exception.ErrorCategory;
import io.cdap.cdap.api.exception.ErrorCodeType;
import io.cdap.cdap.api.exception.ErrorType;
import io.cdap.cdap.api.exception.ErrorUtils;
import io.cdap.cdap.api.exception.ProgramFailureException;
import io.cdap.cdap.etl.api.action.Action;
import io.cdap.cdap.etl.api.action.ActionContext;
import io.cdap.plugin.gcp.bigquery.common.BigQueryErrorUtil;
Expand Down Expand Up @@ -115,12 +117,16 @@ public void run(ActionContext context) {

// Check for errors
if (queryJob.getStatus().getError() != null) {
String errorReason = String.format("The bigquery job failed with reason: %s",
queryJob.getStatus().getError().getReason());
String errorReason = String.format(
"The bigquery job failed with reason: %s. For more details, see %s",
queryJob.getStatus().getError().getReason(), GCPUtils.BQ_SUPPORTED_DOC_URL);
ErrorType type = BigQueryErrorUtil.getErrorType(queryJob.getStatus().getError().getReason());
String errorCode = BigQueryErrorUtil.getErrorCode(
queryJob.getStatus().getError().getReason());
throw ErrorUtils.getProgramFailureException(
new ErrorCategory(ErrorCategory.ErrorCategoryEnum.PLUGIN), errorReason, errorReason, type,
true, null);
new ErrorCategory(ErrorCategory.ErrorCategoryEnum.PLUGIN), errorReason,
queryJob.getStatus().getExecutionErrors().toString(), type, true, ErrorCodeType.HTTP,
errorCode, GCPUtils.BQ_SUPPORTED_DOC_URL, null);
}
TableResult queryResults;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import io.cdap.cdap.api.annotation.Name;
import io.cdap.cdap.api.annotation.Plugin;
import io.cdap.cdap.api.exception.ErrorCategory;
import io.cdap.cdap.api.exception.ErrorCodeType;
import io.cdap.cdap.api.exception.ErrorType;
import io.cdap.cdap.api.exception.ErrorUtils;
import io.cdap.cdap.etl.api.FailureCollector;
Expand Down Expand Up @@ -255,20 +256,27 @@ private void executeQuery(BigQuery bigQuery, QueryJobConfiguration queryConfig,
if (queryJob.getStatus().getError() != null) {
// You can also look at queryJob.getStatus().getExecutionErrors() for all
// errors, not just the latest one.
LOG.error(
String.format("The query job %s failed with error %s and reason %s.", jobId.getJob(),
queryJob.getStatus().getError().getReason(), queryJob.getStatus().getError()));
LOG.error("The query job {} failed with reason: {} and error: {}.", jobId.getJob(),
queryJob.getStatus().getError().getReason(),
queryJob.getStatus().getExecutionErrors().toString());
if (RETRY_ON_REASON.contains(queryJob.getStatus().getError().getReason())) {
throw new BigQueryJobExecutionException(queryJob.getStatus().getError().getMessage());
}
String error = String.format(
"The bigquery query execution failed with reason: %s and message: %s",
String errorReason = String.format(
"The bigquery query execution failed due to reason: %s and error: %s. "
+ "For more details, see %s", queryJob.getStatus().getError().getReason(),
queryJob.getStatus().getExecutionErrors().toString(), GCPUtils.BQ_SUPPORTED_DOC_URL);
String errorMessage = String.format(
"The bigquery query execution failed due to reason: %s , error: %s and message: %s",
queryJob.getStatus().getError().getReason(),
queryJob.getStatus().getExecutionErrors().toString(),
queryJob.getStatus().getError().getMessage());
ErrorType type = BigQueryErrorUtil.getErrorType(queryJob.getStatus().getError().getReason());
String errorCode = BigQueryErrorUtil.getErrorCode(
queryJob.getStatus().getError().getReason());
throw ErrorUtils.getProgramFailureException(
new ErrorCategory(ErrorCategory.ErrorCategoryEnum.PLUGIN), error, error, type, true,
null);
new ErrorCategory(ErrorCategory.ErrorCategoryEnum.PLUGIN), errorReason, errorMessage,
type, true, ErrorCodeType.HTTP, errorCode, GCPUtils.BQ_SUPPORTED_DOC_URL, null);
}

TableResult queryResults;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package io.cdap.plugin.gcp.bigquery.common;

import io.cdap.cdap.api.exception.ErrorCategory;
import io.cdap.cdap.api.exception.ErrorType;
import io.cdap.cdap.api.exception.ProgramFailureException;
import io.cdap.plugin.gcp.common.GCPErrorDetailsProviderUtil;
Expand All @@ -30,6 +31,7 @@ public class BigQueryErrorUtil {

// https://cloud.google.com/bigquery/docs/error-messages#errortable
private static final Map<String, ErrorType> ERROR_REASON_TO_ERROR_TYPE = new HashMap<>();
private static final Map<String, Integer> ERROR_REASON_TO_ERROR_CODE = new HashMap<>();

static {
// User Errors
Expand All @@ -39,29 +41,60 @@ public class BigQueryErrorUtil {
ERROR_REASON_TO_ERROR_TYPE.put("resourceInUse", ErrorType.USER);
ERROR_REASON_TO_ERROR_TYPE.put("resourcesExceeded", ErrorType.USER);
ERROR_REASON_TO_ERROR_TYPE.put("badRequest", ErrorType.USER);
ERROR_REASON_TO_ERROR_TYPE.put("failedPrecondition", ErrorType.USER);
ERROR_REASON_TO_ERROR_TYPE.put("outOfRange", ErrorType.USER);
ERROR_REASON_TO_ERROR_TYPE.put("invalidUser", ErrorType.USER);
ERROR_REASON_TO_ERROR_TYPE.put("notFound", ErrorType.USER);
ERROR_REASON_TO_ERROR_TYPE.put("duplicate", ErrorType.USER);
ERROR_REASON_TO_ERROR_TYPE.put("conditionNotMet", ErrorType.USER);
ERROR_REASON_TO_ERROR_TYPE.put("accessDenied", ErrorType.USER);
ERROR_REASON_TO_ERROR_TYPE.put("billingNotEnabled", ErrorType.USER);
ERROR_REASON_TO_ERROR_TYPE.put("quotaExceeded", ErrorType.USER);
ERROR_REASON_TO_ERROR_TYPE.put("rateLimitExceeded", ErrorType.USER);
ERROR_REASON_TO_ERROR_TYPE.put("responseTooLarge", ErrorType.USER);
ERROR_REASON_TO_ERROR_TYPE.put("blocked", ErrorType.USER);
ERROR_REASON_TO_ERROR_TYPE.put("proxyAuthenticationRequired", ErrorType.USER);
ERROR_REASON_TO_ERROR_TYPE.put("invalidClient", ErrorType.USER);
ERROR_REASON_TO_ERROR_TYPE.put("invalidGrant", ErrorType.USER);
ERROR_REASON_TO_ERROR_TYPE.put("jobRateLimitExceeded", ErrorType.USER);

// System Errors
ERROR_REASON_TO_ERROR_TYPE.put("tableUnavailable", ErrorType.SYSTEM);
ERROR_REASON_TO_ERROR_TYPE.put("backendError", ErrorType.SYSTEM);
ERROR_REASON_TO_ERROR_TYPE.put("internalError", ErrorType.SYSTEM);
ERROR_REASON_TO_ERROR_TYPE.put("notImplemented", ErrorType.SYSTEM);
ERROR_REASON_TO_ERROR_TYPE.put("jobBackendError", ErrorType.SYSTEM);
ERROR_REASON_TO_ERROR_TYPE.put("jobInternalError", ErrorType.SYSTEM);
ERROR_REASON_TO_ERROR_TYPE.put("timeout", ErrorType.SYSTEM);

// Unknown Errors
ERROR_REASON_TO_ERROR_TYPE.put("stopped", ErrorType.UNKNOWN);

//Error codes
ERROR_REASON_TO_ERROR_CODE.put("invalid", 400);
ERROR_REASON_TO_ERROR_CODE.put("invalidQuery", 400);
ERROR_REASON_TO_ERROR_CODE.put("billingTierLimitExceeded", 400);
ERROR_REASON_TO_ERROR_CODE.put("resourceInUse", 400);
ERROR_REASON_TO_ERROR_CODE.put("resourcesExceeded", 400);
ERROR_REASON_TO_ERROR_CODE.put("badRequest", 400);
ERROR_REASON_TO_ERROR_CODE.put("invalidUser", 400);
ERROR_REASON_TO_ERROR_CODE.put("notFound", 404);
ERROR_REASON_TO_ERROR_CODE.put("duplicate", 409);
ERROR_REASON_TO_ERROR_CODE.put("accessDenied", 403);
ERROR_REASON_TO_ERROR_CODE.put("billingNotEnabled", 403);
ERROR_REASON_TO_ERROR_CODE.put("quotaExceeded", 403);
ERROR_REASON_TO_ERROR_CODE.put("rateLimitExceeded", 403);
ERROR_REASON_TO_ERROR_CODE.put("responseTooLarge", 403);
ERROR_REASON_TO_ERROR_CODE.put("blocked", 403);
ERROR_REASON_TO_ERROR_CODE.put("proxyAuthenticationRequired", 407);

// System Errors
ERROR_REASON_TO_ERROR_CODE.put("tableUnavailable", 400);
ERROR_REASON_TO_ERROR_CODE.put("backendError", 503);
ERROR_REASON_TO_ERROR_CODE.put("internalError", 500);
ERROR_REASON_TO_ERROR_CODE.put("notImplemented", 501);
ERROR_REASON_TO_ERROR_CODE.put("jobBackendError", 400);
ERROR_REASON_TO_ERROR_CODE.put("jobInternalError", 400);
ERROR_REASON_TO_ERROR_CODE.put("jobRateLimitExceeded", 400);
ERROR_REASON_TO_ERROR_CODE.put("timeout", 400);

// Unknown Errors
ERROR_REASON_TO_ERROR_CODE.put("stopped", 200);
}

/**
Expand All @@ -77,6 +110,19 @@ public static ErrorType getErrorType(String errorReason) {
return ErrorType.UNKNOWN;
}

/**
* Method to get the error codes based on the error reason.
*
* @param errorReason the error reason to classify
* @return the corresponding ErrorType (USER, SYSTEM, UNKNOWN)
*/
public static String getErrorCode(String errorReason) {
if (errorReason != null && ERROR_REASON_TO_ERROR_CODE.containsKey(errorReason)) {
return String.valueOf(ERROR_REASON_TO_ERROR_CODE.get(errorReason));
}
return null;
}

/**
* Method to get the Program Failure exception based on error reason
*
Expand Down

0 comments on commit 8ac8f51

Please sign in to comment.