1
# Copyright 2015, Google Inc.
4
# Redistribution and use in source and binary forms, with or without
5
# modification, are permitted provided that the following conditions are
8
# * Redistributions of source code must retain the above copyright
9
# notice, this list of conditions and the following disclaimer.
10
# * Redistributions in binary form must reproduce the above
11
# copyright notice, this list of conditions and the following disclaimer
12
# in the documentation and/or other materials provided with the
14
# * Neither the name of Google Inc. nor the names of its
15
# contributors may be used to endorse or promote products derived from
16
# this software without specific prior written permission.
18
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
"""Utilities for working with callables."""
39
class Outcome(object):
40
"""A sum type describing the outcome of some call.
43
kind: One of Kind.RETURNED or Kind.RAISED respectively indicating that the
44
call returned a value or raised an exception.
45
return_value: The value returned by the call. Must be present if kind is
47
exception: The exception raised by the call. Must be present if kind is
50
__metaclass__ = abc.ABCMeta
53
class Kind(enum.Enum):
54
"""Identifies the general kind of the outcome of some call."""
61
collections.namedtuple(
62
'_EasyOutcome', ['kind', 'return_value', 'exception']),
64
"""A trivial implementation of Outcome."""
67
def _call_logging_exceptions(behavior, message, *args, **kwargs):
69
return _EasyOutcome(Outcome.Kind.RETURNED, behavior(*args, **kwargs), None)
70
except Exception as e: # pylint: disable=broad-except
71
logging.exception(message)
72
return _EasyOutcome(Outcome.Kind.RAISED, None, e)
75
def with_exceptions_logged(behavior, message):
76
"""Wraps a callable in a try-except that logs any exceptions it raises.
79
behavior: Any callable.
80
message: A string to log if the behavior raises an exception.
83
A callable that when executed invokes the given behavior. The returned
84
callable takes the same arguments as the given behavior but returns a
85
future.Outcome describing whether the given behavior returned a value or
88
@functools.wraps(behavior)
89
def wrapped_behavior(*args, **kwargs):
90
return _call_logging_exceptions(behavior, message, *args, **kwargs)
91
return wrapped_behavior
94
def call_logging_exceptions(behavior, message, *args, **kwargs):
95
"""Calls a behavior in a try-except that logs any exceptions it raises.
98
behavior: Any callable.
99
message: A string to log if the behavior raises an exception.
100
*args: Positional arguments to pass to the given behavior.
101
**kwargs: Keyword arguments to pass to the given behavior.
104
An Outcome describing whether the given behavior returned a value or raised
107
return _call_logging_exceptions(behavior, message, *args, **kwargs)