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 String
ACCESS_NOT_CONFIGURED_REASON
static String
ACCOUNT_DISABLED_REASON
static String
FIELD_SIZE_TOO_LARGE_REASON
static String
GLOBAL_DOMAIN
static ApiErrorExtractor
INSTANCE
Singleton instance of the ApiErrorExtractor.static String
QUOTA_EXCEEDED_REASON
static String
RATE_LIMITED_REASON
static String
RESOURCE_NOT_READY_REASON
static int
STATUS_CODE_RANGE_NOT_SATISFIABLE
static String
USAGE_LIMITS_DOMAIN
static String
USER_PROJECT_MISSING_MESSAGE
static String
USER_RATE_LIMITED_REASON
-
Constructor Summary
Constructors Constructor Description ApiErrorExtractor()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
accessDenied(IOException e)
Determines if the given exception indicates 'access denied'.boolean
accessDeniedNonRecoverable(IOException e)
Determines if the exception is a non-recoverable access denied code (such as account closed or marked for deletion).boolean
badRequest(IOException e)
Determines if the given exception indicates bad request.boolean
clientError(IOException e)
Determines if the exception is a client error.boolean
fieldSizeTooLarge(IOException e)
Determines if the given exception indicates 'field size too large'.String
getDebugInfo(IOException e)
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.String
getErrorMessage(IOException e)
Extracts the error message.static com.google.api.client.http.HttpResponseException
getHttpResponseException(Throwable throwable)
protected int
getHttpStatusCode(com.google.api.client.http.HttpResponseException e)
Returns HTTP status code from the given exception.protected com.google.api.client.googleapis.json.GoogleJsonError
getJsonError(IOException e)
If the exception is a GoogleJsonResponseException, get the error details, else return null.static com.google.api.client.googleapis.json.GoogleJsonResponseException
getJsonResponseException(Throwable throwable)
boolean
internalServerError(IOException e)
Determines if the exception is an internal server error.boolean
itemAlreadyExists(IOException e)
Determines if the given exception indicates 'item already exists'.boolean
itemNotFound(IOException e)
Determines if the given exception indicates 'item not found'.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.boolean
quotaExceeded(IOException e)
Determines if a given Throwable is caused by Quota Exceeded.boolean
rangeNotSatisfiable(IOException e)
Determines if the given exception indicates 'range not satisfiable'.boolean
rateLimited(IOException e)
Determines if a given Throwable is caused by a rate limit being applied.protected boolean
recursiveCheckForCode(IOException e, int code)
Recursively checks getCause() if outer exception isn't an instance of the correct class.boolean
requestFailure(IOException e)
Determines if the given exception indicates intermittent request failure or failure caused by user error.boolean
resourceNotReady(IOException e)
Determines if the given exception indicates 'resource not ready'.IOException
toUserPresentableException(IOException e, String action)
Converts the exception to a user-presentable error message.String
toUserPresentableMessage(IOException e)
String
toUserPresentableMessage(IOException e, String action)
Converts the exception to a user-presentable error message.boolean
unauthorized(IOException e)
Determines if the given exception indicates the request was unauthenticated.boolean
userProjectMissing(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)
-
-