• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2015 gRPC authors.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14"""Utilities for working with callables."""
15
16import abc
17import collections
18import enum
19import functools
20import logging
21
22import six
23
24logging.basicConfig()
25_LOGGER = logging.getLogger(__name__)
26
27
28class Outcome(six.with_metaclass(abc.ABCMeta)):
29    """A sum type describing the outcome of some call.
30
31  Attributes:
32    kind: One of Kind.RETURNED or Kind.RAISED respectively indicating that the
33      call returned a value or raised an exception.
34    return_value: The value returned by the call. Must be present if kind is
35      Kind.RETURNED.
36    exception: The exception raised by the call. Must be present if kind is
37      Kind.RAISED.
38  """
39
40    @enum.unique
41    class Kind(enum.Enum):
42        """Identifies the general kind of the outcome of some call."""
43
44        RETURNED = object()
45        RAISED = object()
46
47
48class _EasyOutcome(
49        collections.namedtuple('_EasyOutcome',
50                               ['kind', 'return_value', 'exception']), Outcome):
51    """A trivial implementation of Outcome."""
52
53
54def _call_logging_exceptions(behavior, message, *args, **kwargs):
55    try:
56        return _EasyOutcome(Outcome.Kind.RETURNED, behavior(*args, **kwargs),
57                            None)
58    except Exception as e:  # pylint: disable=broad-except
59        _LOGGER.exception(message)
60        return _EasyOutcome(Outcome.Kind.RAISED, None, e)
61
62
63def with_exceptions_logged(behavior, message):
64    """Wraps a callable in a try-except that logs any exceptions it raises.
65
66  Args:
67    behavior: Any callable.
68    message: A string to log if the behavior raises an exception.
69
70  Returns:
71    A callable that when executed invokes the given behavior. The returned
72      callable takes the same arguments as the given behavior but returns a
73      future.Outcome describing whether the given behavior returned a value or
74      raised an exception.
75  """
76
77    @functools.wraps(behavior)
78    def wrapped_behavior(*args, **kwargs):
79        return _call_logging_exceptions(behavior, message, *args, **kwargs)
80
81    return wrapped_behavior
82
83
84def call_logging_exceptions(behavior, message, *args, **kwargs):
85    """Calls a behavior in a try-except that logs any exceptions it raises.
86
87  Args:
88    behavior: Any callable.
89    message: A string to log if the behavior raises an exception.
90    *args: Positional arguments to pass to the given behavior.
91    **kwargs: Keyword arguments to pass to the given behavior.
92
93  Returns:
94    An Outcome describing whether the given behavior returned a value or raised
95      an exception.
96  """
97    return _call_logging_exceptions(behavior, message, *args, **kwargs)
98