Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decorator should fail safe #19

Open
johnarnold opened this issue Nov 10, 2017 · 1 comment
Open

Decorator should fail safe #19

johnarnold opened this issue Nov 10, 2017 · 1 comment

Comments

@johnarnold
Copy link

Scenario: Using redis store, and the store is remote

Expected behavior: If the store is unavailable, catch exceptions, log a warning, and just run the normal function.

Actual behavior: exceptions bubble up

@johnarnold
Copy link
Author

An example workaround, credit @tombo315

def memo_safe(**opts):
    """
    Memoizes a function (fail-safe)
    :param opts: options to pass to Memoize call
    :return: the wrapped function.
    """

    def outer(f):
        @wraps(f)
        def inner(*args, **kwargs):

            if memo is None:
                return f(*args, **kwargs)

            try:
                m_func = memo(f, **opts)
                ret = m_func(*args, **kwargs)
            except Exception as e:
                logger.warning("On memoized call: {}".format(str(e)))
                ret = f(*args, **kwargs)
            return ret

        return inner

    return outer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant