Class 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.
    • Constructor Detail

      • ApiErrorExtractor

        public ApiErrorExtractor()
    • 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 exception
        action - 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.
      • 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)