Esempio n. 1
0
def handle_exception(exception):
    wrapped = ResponseException(str(exception), ErrorCode.INTERNAL_ERROR,
                                type(exception))
    app.logger.error(
        'An API error occurred with the status code of {status} ({exception}).'
        .format(status=wrapped.status, exception=wrapped.wrappedException))
    raise exceptions.ApiError('Could not process the request',
                              status_code=http.client.NOT_FOUND)
Esempio n. 2
0
def handle_exception(exception):
    wrapped = ResponseException(str(exception), ErrorCode.INTERNAL_ERROR,
                                type(exception))

    logger.info(
        'An API error occurred with the status code of {status} ({exception}).'
        .format(status=wrapped.status, exception=wrapped.wrappedException))

    if is_retrievable_from_cache(wrapped.status, request.path):
        logger.info('Attempting to retrieving the cached request from S3...')

        # Retrieve the information needed to construct a URL for the S3 bucket
        # where the cached API responses live.
        formatted_url = utils.format_url(request.url)
        s3_bucket = utils.get_bucket()
        bucket_region = env.get_credential('region')
        cached_url = "http://s3-{0}.amazonaws.com/{1}/cached-calls/{2}".format(
            bucket_region, s3_bucket.name, formatted_url)

        # Attempt to retrieve the cached data from S3.
        cached_data = utils.get_cached_request(cached_url)

        # If the cached data was returned, we can return that to the client.
        # Otherwise, log the error and raise an API error.
        if cached_data is not None:
            logger.info('Successfully retrieved cached request from S3.')
            return cached_data
        else:
            logger.error(
                'An error occured while retrieving the cached file from S3.')
            raise exceptions.ApiError(
                'The requested URL could not be found.'.format(request.url),
                status_code=http.client.NOT_FOUND)
    else:
        raise exceptions.ApiError(
            'The requested URL could not be found.'.format(request.url),
            status_code=http.client.NOT_FOUND)
Esempio n. 3
0
def forbidden(exception):
    wrapped = ResponseException(str(exception), exception.code,
                                type(exception))
    return wrapped.wrappedException, wrapped.status
Esempio n. 4
0
def page_not_found(exception):
    wrapped = ResponseException(str(exception), exception.code,
                                type(exception))
    return wrapped.wrappedException, wrapped.status