Steve Piercy
2018-09-22 e22970cd21eb36c2a658c843bb5cb4f59d77fd19
commit | author | age
0c1c39 1 from pyramid.httpexceptions import (
0e2914 2     HTTPBadRequest,
0c1c39 3     HTTPNotFound,
CM 4     HTTPForbidden,
5     )
4184d9 6
8c2a9e 7 NotFound = HTTPNotFound # bw compat
CM 8 Forbidden = HTTPForbidden # bw compat
d96ff9 9
e6c2d2 10 CR = '\n'
CM 11
65dee6 12
DS 13 class BadCSRFOrigin(HTTPBadRequest):
14     """
15     This exception indicates the request has failed cross-site request forgery
16     origin validation.
17     """
18     title = "Bad CSRF Origin"
19     explanation = (
20         "Access is denied. This server can not verify that the origin or "
21         "referrer of your request matches the current site. Either your "
22         "browser supplied the wrong Origin or Referrer or it did not supply "
23         "one at all."
24     )
25
26
0e2914 27 class BadCSRFToken(HTTPBadRequest):
MM 28     """
29     This exception indicates the request has failed cross-site request
30     forgery token validation.
31     """
32     title = 'Bad CSRF Token'
33     explanation = (
34         'Access is denied.  This server can not verify that your cross-site '
35         'request forgery token belongs to your login session.  Either you '
36         'supplied the wrong cross-site request forgery token or your session '
37         'no longer exists.  This may be due to session timeout or because '
38         'browser is not supplying the credentials required, as can happen '
39         'when the browser has cookies turned off.')
40
99edc5 41 class PredicateMismatch(HTTPNotFound):
d96ff9 42     """
4d059a 43     This exception is raised by multiviews when no view matches
DB 44     all given predicates.
45
46     This exception subclasses the :class:`HTTPNotFound` exception for a
47     specific reason: if it reaches the main exception handler, it should
48     be treated as :class:`HTTPNotFound`` by any exception view
49     registrations. Thus, typically, this exception will not be seen
50     publicly.
25c64c 51
4d059a 52     However, this exception will be raised if the predicates of all
DB 53     views configured to handle another exception context cannot be
54     successfully matched.  For instance, if a view is configured to
55     handle a context of ``HTTPForbidden`` and the configured with
56     additional predicates, then :class:`PredicateMismatch` will be
57     raised if:
58
59     * An original view callable has raised :class:`HTTPForbidden` (thus
60       invoking an exception view); and
61     * The given request fails to match all predicates for said
62       exception view associated with :class:`HTTPForbidden`.
63
64     The same applies to any type of exception being handled by an
65     exception view.
d96ff9 66     """
d75fe7 67
0482bd 68 class URLDecodeError(UnicodeDecodeError):
CM 69     """
fd5ae9 70     This exception is raised when :app:`Pyramid` cannot
0482bd 71     successfully decode a URL or a URL path segment.  This exception
024fd8 72     behaves just like the Python builtin
0482bd 73     :exc:`UnicodeDecodeError`. It is a subclass of the builtin
CM 74     :exc:`UnicodeDecodeError` exception only for identity purposes,
75     mostly so an exception view can be registered when a URL cannot be
76     decoded.
77     """
78
79ef3d 79 class ConfigurationError(Exception):
a294cd 80     """ Raised when inappropriate input values are supplied to an API
CM 81     method of a :term:`Configurator`"""
b4c212 82
79ef3d 83 class ConfigurationConflictError(ConfigurationError):
CM 84     """ Raised when a configuration conflict is detected during action
85     processing"""
9b496b 86
79ef3d 87     def __init__(self, conflicts):
CM 88         self._conflicts = conflicts
89
90     def __str__(self):
91         r = ["Conflicting configuration actions"]
8e606d 92         items = sorted(self._conflicts.items())
79ef3d 93         for discriminator, infos in items:
CM 94             r.append("  For: %s" % (discriminator, ))
95             for info in infos:
8e606d 96                 for line in str(info).rstrip().split(CR):
25c64c 97                     r.append("    " + line)
79ef3d 98
e6c2d2 99         return CR.join(r)
79ef3d 100
CM 101
102 class ConfigurationExecutionError(ConfigurationError):
103     """An error occurred during execution of a configuration action
104     """
105
106     def __init__(self, etype, evalue, info):
107         self.etype, self.evalue, self.info = etype, evalue, info
108
109     def __str__(self):
110         return "%s: %s\n  in:\n  %s" % (self.etype, self.evalue, self.info)
66fe1d 111
e8c66a 112
66fe1d 113 class CyclicDependencyError(Exception):
CM 114     """ The exception raised when the Pyramid topological sorter detects a
115     cyclic dependency."""
116     def __init__(self, cycles):
117         self.cycles = cycles
118
119     def __str__(self):
120         L = []
121         cycles = self.cycles
122         for cycle in cycles:
123             dependent = cycle
124             dependees = cycles[cycle]
125             L.append('%r sorts before %r' % (dependent, dependees))
126         msg = 'Implicit ordering cycle:' + '; '.join(L)
127         return msg