def handle_api_exceptions(max_attempts=1):
    """Return a function decorator that wraps a given function in a
    try-except block that will handle various exceptions that may
    occur during an API request to reddit. A maximum number of retry 
    attempts may be specified.
    """
    def decorator(func):
        @wraps(func)
        def wrapper(*args, **kwargs):
            retries = 0
            while retries < max_attempts:
                sleep_time = None
                error_msg = ""
                try:
                    return func(*args, **kwargs)
                except praw.errors.RateLimitExceeded as e:
                    error_msg = "Rate Limit exceeded"
                    sleep_time = e.sleep_time
                except praw.errors.HTTPException as e:
                    error_msg = "HTTPError \"{error}\" occurred: ".format(
                        error=e)
                    # Quit when encountering an HTTP 403 "Forbidden" errors.
                    if '403' in str(e):
                        log(error_msg)
                        return
                # Handle and log miscellaneous API exceptions
                except praw.errors.APIException as e:
                    error_msg = "API Exception \"{error}\" occurred: ".format(
                        error=e)
                except SocketError as e:
                    error_msg = "SocketError \"{error}\" occurred: ".format(
                        error=e)
                    log(error_msg)
                sleep_time = sleep_time or retries * 150
                log("{0} in {f}. Sleeping for {t} seconds. "
                    "Attempt {rt} of {at}.".format(error_msg, f=func.__name__,
                        t=sleep_time, rt=retries+1, at=max_attempts))
                time.sleep(sleep_time)
                retries += 1
        return wrapper
    return decorator
dataset/ETHPy150Open renfredxh/compilebot/compilebot/compilebot.py/handle_api_exceptions/original