Package com.google.cloud.hadoop.util
Class ApiErrorExtractor
- java.lang.Object
-
- com.google.cloud.hadoop.util.ApiErrorExtractor
-
public class ApiErrorExtractor extends Object
Translates exceptions from API calls into higher-level meaning, while allowing injectability for testing how API errors are handled.
-
-
Field Summary
Fields Modifier and Type Field Description static StringACCESS_NOT_CONFIGURED_REASONstatic StringACCOUNT_DISABLED_REASONstatic StringFIELD_SIZE_TOO_LARGE_REASONstatic StringGLOBAL_DOMAINstatic ApiErrorExtractorINSTANCESingleton instance of the ApiErrorExtractor.static StringQUOTA_EXCEEDED_REASONstatic StringRATE_LIMITED_REASONstatic StringRESOURCE_NOT_READY_REASONstatic intSTATUS_CODE_RANGE_NOT_SATISFIABLEstatic StringUSAGE_LIMITS_DOMAINstatic StringUSER_PROJECT_MISSING_MESSAGEstatic StringUSER_RATE_LIMITED_REASON
-
Constructor Summary
Constructors Constructor Description ApiErrorExtractor()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanaccessDenied(IOException e)Determines if the given exception indicates 'access denied'.booleanaccessDeniedNonRecoverable(IOException e)Determines if the exception is a non-recoverable access denied code (such as account closed or marked for deletion).booleanbadRequest(IOException e)Determines if the given exception indicates bad request.booleanclientError(IOException e)Determines if the exception is a client error.booleanfieldSizeTooLarge(IOException e)Determines if the given exception indicates 'field size too large'.StringgetDebugInfo(IOException e)protected com.google.api.client.googleapis.json.GoogleJsonError.ErrorInfogetErrorInfo(IOException e)Get the first ErrorInfo from an IOException if it is an instance of GoogleJsonResponseException, otherwise return null.StringgetErrorMessage(IOException e)Extracts the error message.static com.google.api.client.http.HttpResponseExceptiongetHttpResponseException(Throwable throwable)protected intgetHttpStatusCode(com.google.api.client.http.HttpResponseException e)Returns HTTP status code from the given exception.protected com.google.api.client.googleapis.json.GoogleJsonErrorgetJsonError(IOException e)If the exception is a GoogleJsonResponseException, get the error details, else return null.static com.google.api.client.googleapis.json.GoogleJsonResponseExceptiongetJsonResponseException(Throwable throwable)booleaninternalServerError(IOException e)Determines if the exception is an internal server error.booleanitemAlreadyExists(IOException e)Determines if the given exception indicates 'item already exists'.booleanitemNotFound(IOException e)Determines if the given exception indicates 'item not found'.booleanpreconditionNotMet(IOException e)Determines if the given IOException indicates 'precondition not met' Recursively checks getCause() if outer exception isn't an instance of the correct class.booleanquotaExceeded(IOException e)Determines if a given Throwable is caused by Quota Exceeded.booleanrangeNotSatisfiable(IOException e)Determines if the given exception indicates 'range not satisfiable'.booleanrateLimited(IOException e)Determines if a given Throwable is caused by a rate limit being applied.protected booleanrecursiveCheckForCode(IOException e, int code)Recursively checks getCause() if outer exception isn't an instance of the correct class.booleanrequestFailure(IOException e)Determines if the given exception indicates intermittent request failure or failure caused by user error.booleanresourceNotReady(IOException e)Determines if the given exception indicates 'resource not ready'.IOExceptiontoUserPresentableException(IOException e, String action)Converts the exception to a user-presentable error message.StringtoUserPresentableMessage(IOException e)StringtoUserPresentableMessage(IOException e, String action)Converts the exception to a user-presentable error message.booleanunauthorized(IOException e)Determines if the given exception indicates the request was unauthenticated.booleanuserProjectMissing(IOException e)Determines if the given exception indicates that 'userProject' is missing in request.
-
-
-
Field Detail
-
INSTANCE
public static final ApiErrorExtractor INSTANCE
Singleton instance of the ApiErrorExtractor.
-
STATUS_CODE_RANGE_NOT_SATISFIABLE
public static final int STATUS_CODE_RANGE_NOT_SATISFIABLE
- See Also:
- Constant Field Values
-
GLOBAL_DOMAIN
public static final String GLOBAL_DOMAIN
- See Also:
- Constant Field Values
-
USAGE_LIMITS_DOMAIN
public static final String USAGE_LIMITS_DOMAIN
- See Also:
- Constant Field Values
-
RATE_LIMITED_REASON
public static final String RATE_LIMITED_REASON
- See Also:
- Constant Field Values
-
USER_RATE_LIMITED_REASON
public static final String USER_RATE_LIMITED_REASON
- See Also:
- Constant Field Values
-
QUOTA_EXCEEDED_REASON
public static final String QUOTA_EXCEEDED_REASON
- See Also:
- Constant Field Values
-
ACCOUNT_DISABLED_REASON
public static final String ACCOUNT_DISABLED_REASON
- See Also:
- Constant Field Values
-
ACCESS_NOT_CONFIGURED_REASON
public static final String ACCESS_NOT_CONFIGURED_REASON
- See Also:
- Constant Field Values
-
RESOURCE_NOT_READY_REASON
public static final String RESOURCE_NOT_READY_REASON
- See Also:
- Constant Field Values
-
FIELD_SIZE_TOO_LARGE_REASON
public static final String FIELD_SIZE_TOO_LARGE_REASON
- See Also:
- Constant Field Values
-
USER_PROJECT_MISSING_MESSAGE
public static final String USER_PROJECT_MISSING_MESSAGE
- See Also:
- Constant Field Values
-
-
Method Detail
-
requestFailure
public boolean requestFailure(IOException e)
Determines if the given exception indicates intermittent request failure or failure caused by user error.
-
accessDenied
public boolean accessDenied(IOException e)
Determines if the given exception indicates 'access denied'. Recursively checks getCause() if outer exception isn't an instance of the correct class.Warning: this method only checks for access denied status code, however this may include potentially recoverable reason codes such as rate limiting. For alternative, see
accessDeniedNonRecoverable(IOException).
-
badRequest
public boolean badRequest(IOException e)
Determines if the given exception indicates bad request.
-
unauthorized
public boolean unauthorized(IOException e)
Determines if the given exception indicates the request was unauthenticated. This can be caused by attaching invalid credentials to a request.
-
accessDeniedNonRecoverable
public boolean accessDeniedNonRecoverable(IOException e)
Determines if the exception is a non-recoverable access denied code (such as account closed or marked for deletion).
-
clientError
public boolean clientError(IOException e)
Determines if the exception is a client error.
-
internalServerError
public boolean internalServerError(IOException e)
Determines if the exception is an internal server error.
-
itemAlreadyExists
public boolean itemAlreadyExists(IOException e)
Determines if the given exception indicates 'item already exists'. Recursively checks getCause() if outer exception isn't an instance of the correct class.
-
itemNotFound
public boolean itemNotFound(IOException e)
Determines if the given exception indicates 'item not found'. Recursively checks getCause() if outer exception isn't an instance of the correct class.
-
fieldSizeTooLarge
public boolean fieldSizeTooLarge(IOException e)
Determines if the given exception indicates 'field size too large'. Recursively checks getCause() if outer exception isn't an instance of the correct class.
-
resourceNotReady
public boolean resourceNotReady(IOException e)
Determines if the given exception indicates 'resource not ready'. Recursively checks getCause() if outer exception isn't an instance of the correct class.
-
preconditionNotMet
public boolean preconditionNotMet(IOException e)
Determines if the given IOException indicates 'precondition not met' Recursively checks getCause() if outer exception isn't an instance of the correct class.
-
rangeNotSatisfiable
public boolean rangeNotSatisfiable(IOException e)
Determines if the given exception indicates 'range not satisfiable'. Recursively checks getCause() if outer exception isn't an instance of the correct class.
-
rateLimited
public boolean rateLimited(IOException e)
Determines if a given Throwable is caused by a rate limit being applied. Recursively checks getCause() if outer exception isn't an instance of the correct class.- Parameters:
e- The Throwable to check.- Returns:
- True if the Throwable is a result of rate limiting being applied.
-
quotaExceeded
public boolean quotaExceeded(IOException e)
Determines if a given Throwable is caused by Quota Exceeded. Recursively checks getCause() if outer exception isn't an instance of the correct class.
-
userProjectMissing
public boolean userProjectMissing(IOException e)
Determines if the given exception indicates that 'userProject' is missing in request. Recursively checks getCause() if outer exception isn't an instance of the correct class.
-
getErrorMessage
public String getErrorMessage(IOException e)
Extracts the error message.
-
toUserPresentableException
public IOException toUserPresentableException(IOException e, String action) throws IOException
Converts the exception to a user-presentable error message. Specifically, extracts message field for HTTP 4xx codes, and creates a generic "Internal Server Error" for HTTP 5xx codes.- Parameters:
e- the exceptionaction- the description of the action being performed at the time of error.- Throws:
IOException- See Also:
toUserPresentableMessage(IOException, String)
-
toUserPresentableMessage
public String toUserPresentableMessage(IOException e, @Nullable String action)
Converts the exception to a user-presentable error message. Specifically, extracts message field for HTTP 4xx codes, and creates a generic "Internal Server Error" for HTTP 5xx codes.
-
toUserPresentableMessage
public String toUserPresentableMessage(IOException e)
-
getDebugInfo
@Nullable public String getDebugInfo(IOException e)
-
getHttpStatusCode
protected int getHttpStatusCode(com.google.api.client.http.HttpResponseException e)
Returns HTTP status code from the given exception.Note: GoogleJsonResponseException.getStatusCode() method is marked final therefore it cannot be mocked using Mockito. We use this helper so that we can override it in tests.
-
getErrorInfo
@Nullable protected com.google.api.client.googleapis.json.GoogleJsonError.ErrorInfo getErrorInfo(IOException e)
Get the first ErrorInfo from an IOException if it is an instance of GoogleJsonResponseException, otherwise return null.
-
getJsonError
@Nullable protected com.google.api.client.googleapis.json.GoogleJsonError getJsonError(IOException e)
If the exception is a GoogleJsonResponseException, get the error details, else return null.
-
recursiveCheckForCode
protected boolean recursiveCheckForCode(IOException e, int code)
Recursively checks getCause() if outer exception isn't an instance of the correct class.
-
getJsonResponseException
@Nullable public static com.google.api.client.googleapis.json.GoogleJsonResponseException getJsonResponseException(Throwable throwable)
-
getHttpResponseException
@Nullable public static com.google.api.client.http.HttpResponseException getHttpResponseException(Throwable throwable)
-
-