Skip to content

Commit

Permalink
Update GCPErrorDetailsProviderUtil
Browse files Browse the repository at this point in the history
  • Loading branch information
psainics committed Jan 14, 2025
1 parent 042a683 commit 56dfc62
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,42 +35,46 @@
*/
public final class GCPErrorDetailsProviderUtil {

public static ProgramFailureException getHttpResponseExceptionDetailsFromChain(Exception e, String errorReason) {
return getHttpResponseExceptionDetailsFromChain(e, errorReason, ErrorType.USER, true);
}

public static ProgramFailureException getHttpResponseExceptionDetailsFromChain(Exception e, String errorReason,
String externalDocumentationLink) {
ErrorType errorType,
boolean dependency) {
List<Throwable> causalChain = Throwables.getCausalChain(e);
for (Throwable t : causalChain) {
if (t instanceof ProgramFailureException) {
// Avoid double wrap
return (ProgramFailureException) t;
}
if (t instanceof HttpResponseException) {
return getProgramFailureException((HttpResponseException) t, externalDocumentationLink);
return getProgramFailureException((HttpResponseException) t);
}
}
// If no HttpResponseException found in the causal chain, return generic program failure exception
return ErrorUtils.getProgramFailureException(new ErrorCategory(ErrorCategory.ErrorCategoryEnum.PLUGIN), errorReason,
String.format("%s %s", errorReason, e.getMessage()), ErrorType.USER, true, e);
String.format("%s %s: %s", errorReason, e.getClass().getName(), e.getMessage()), errorType, dependency, e);
}

private static ProgramFailureException getProgramFailureException(HttpResponseException e,
String externalDocumentationLink) {
private static ProgramFailureException getProgramFailureException(HttpResponseException e) {
Integer statusCode = e.getStatusCode();
ErrorUtils.ActionErrorPair pair = ErrorUtils.getActionErrorByStatusCode(statusCode);
String errorReason =
String.format("%s %s. %s", e.getStatusCode(), e.getStatusMessage(), pair.getCorrectiveAction());
String errorMessage = e.getMessage();
if (e instanceof GoogleJsonResponseException) {
errorMessage = getErrorMessage((GoogleJsonResponseException) e);
if (!Strings.isNullOrEmpty(externalDocumentationLink)) {
if (!Strings.isNullOrEmpty(GCPUtils.GCS_SUPPORTED_DOC_URL)) {
if (!errorReason.endsWith(".")) {
errorReason = errorReason + ".";
}
errorReason = String.format("%s For more details, see %s", errorReason, externalDocumentationLink);
errorReason = String.format("%s For more details, see %s", errorReason, GCPUtils.GCS_SUPPORTED_DOC_URL);
}
}
return ErrorUtils.getProgramFailureException(new ErrorCategory(ErrorCategory.ErrorCategoryEnum.PLUGIN), errorReason,
String.format("%s %s: %s", errorReason, e.getClass().getName(), errorMessage), pair.getErrorType(), true,
ErrorCodeType.HTTP, statusCode.toString(), externalDocumentationLink, e);
ErrorCodeType.HTTP, statusCode.toString(), GCPUtils.GCS_SUPPORTED_DOC_URL, e);
}

static String getErrorMessage(GoogleJsonResponseException exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,7 @@ public void run(ActionContext context) throws Exception {
// Add more descriptive error message
String errorReason = String.format("Unable to access or create bucket %s. ", gcsPath.getBucket()) +
"Ensure you entered the correct bucket path and have permissions for it.";
throw GCPErrorDetailsProviderUtil.getHttpResponseExceptionDetailsFromChain(e, errorReason,
GCPUtils.GCS_SUPPORTED_DOC_URL);
throw GCPErrorDetailsProviderUtil.getHttpResponseExceptionDetailsFromChain(e, errorReason);
}
if (bucket == null) {
GCPUtils.createBucket(storage, gcsPath.getBucket(), config.location, cmekKeyName);
Expand All @@ -155,9 +154,8 @@ public void run(ActionContext context) throws Exception {
} catch (IOException e) {
rollback = true;
String errorReason = "Unable to get GCS filesystem handler.";
String errorMessage = String.format("%s %s", errorReason, e.getMessage());
throw ErrorUtils.getProgramFailureException(new ErrorCategory(ErrorCategory.ErrorCategoryEnum.PLUGIN),
errorReason, errorMessage, ErrorType.UNKNOWN, false, e);
throw GCPErrorDetailsProviderUtil.getHttpResponseExceptionDetailsFromChain(e, errorReason, ErrorType.SYSTEM,
true);
}
if (!fs.exists(gcsPath)) {
try {
Expand All @@ -168,9 +166,8 @@ public void run(ActionContext context) throws Exception {
LOG.warn(String.format("Failed to create path '%s'", gcsPath));
rollback = true;
String errorReason = String.format("Failed to create path %s.", gcsPath);
String errorMessage = String.format("%s with message: %s", errorReason, e.getMessage());
throw ErrorUtils.getProgramFailureException(new ErrorCategory(ErrorCategory.ErrorCategoryEnum.PLUGIN),
errorReason, errorMessage, ErrorType.SYSTEM, true, e);
throw GCPErrorDetailsProviderUtil.getHttpResponseExceptionDetailsFromChain(e, errorReason, ErrorType.SYSTEM,
true);
}
} else {
if (config.failIfExists()) {
Expand Down

0 comments on commit 56dfc62

Please sign in to comment.