~cjwatson/lazr.restful/double-closing-brace

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# Copyright 2008 Canonical Ltd.  All rights reserved.

"""Error handling on the webservice."""

__metaclass__ = type
__all__ = [
    'ClientErrorView',
    'expose',
    'NotFoundView',
    'RequestExpiredView',
    'SystemErrorView',
    'UnauthorizedView',
    'WebServiceExceptionView',
    ]

import traceback

from zope.component import getUtility
from zope.interface import implementer

try:
    # This import brings some big ugly dependencies, and is not strictly
    # necessary.
    from zope.app.exception.interfaces import ISystemErrorView
except ImportError:
    from zope.interface import Interface
    class ISystemErrorView(Interface):
        """Error views that can classify their contexts as system errors
        """

        def isSystemError():
            """Return a boolean indicating whether the error is a system error"""

from lazr.restful.interfaces import (
    IWebServiceConfiguration,
    IWebServiceExceptionView,
    )


@implementer(IWebServiceExceptionView)
class WebServiceExceptionView:
    """Generic view handling exceptions on the web service."""

    def __init__(self, context, request):
        self.context = context
        self.request = request

    @property
    def status(self):
        """The HTTP status to use for the response.

        By default, use the __lazr_webservice_error__ attribute on
        the exception.
        """
        return getattr(self.context, '__lazr_webservice_error__', 500)

    def isSystemError(self):
        """See `ISystemErrorView`."""
        return self.status / 100 == 5

    def __call__(self):
        """Generate the HTTP response describing the exception."""
        response = self.request.response
        response.setStatus(self.status)
        response.setHeader('Content-Type', 'text/plain')
        if getattr(self.request, 'oopsid', None) is not None:
            response.setHeader('X-Lazr-OopsId', self.request.oopsid)

        show_tracebacks = getUtility(
            IWebServiceConfiguration).show_tracebacks
        server_error = self.status / 100 == 5
        if (not show_tracebacks and server_error):
            # Don't show the exception message; it might contain
            # private information.
            result = [self.context.__class__.__name__]
        else:
            # It's okay to show the exception message
            result = [str(self.context)]

        if show_tracebacks and server_error:
            result.append('\n\n')
            result.append(traceback.format_exc())

        return ''.join(result)


# lazr/restful/configure.zcml registers these classes as adapter
# factories for common Zope exceptions.
@implementer(ISystemErrorView)
class ClientErrorView(WebServiceExceptionView):
    """Client-induced error."""

    status = 400


@implementer(ISystemErrorView)
class SystemErrorView(WebServiceExceptionView):
    """Server error."""

    status = 500


class NotFoundView(WebServiceExceptionView):
    """View for NotFound."""

    status = 404


class UnauthorizedView(WebServiceExceptionView):
    """View for Unauthorized exception."""

    status = 401


# This is currently only configured/tested in Launchpad.
class RequestExpiredView(WebServiceExceptionView):
    """View for RequestExpired exception."""

    status = 503


def expose(exception, status=400):
    """Tell lazr.restful to deliver details about the exception to the client.
    """
    # Since Python lets us raise exception types without instantiating them
    # (like "raise RuntimeError" instead of "raise RuntimeError()", we want to
    # make sure the caller doesn't get confused and try that with us.
    if not isinstance(exception, BaseException):
        raise ValueError('This function only accepts exception instances. '
                         'Use the @error_status decorator to publish an '
                         'exception class as a web service error.')

    exception.__lazr_webservice_error__ = status

    # Mark the exception to indicate that its details should be sent to the
    # web service client.
    return exception