1 # pylint: disable=W0401,W0614 |
|
2 # copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
|
3 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
|
4 # |
|
5 # This file is part of CubicWeb. |
|
6 # |
|
7 # CubicWeb is free software: you can redistribute it and/or modify it under the |
|
8 # terms of the GNU Lesser General Public License as published by the Free |
|
9 # Software Foundation, either version 2.1 of the License, or (at your option) |
|
10 # any later version. |
|
11 # |
|
12 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
|
13 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|
14 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
|
15 # details. |
|
16 # |
|
17 # You should have received a copy of the GNU Lesser General Public License along |
|
18 # with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
|
19 """exceptions used in the core of the CubicWeb web application""" |
|
20 |
|
21 __docformat__ = "restructuredtext en" |
|
22 |
|
23 from six.moves import http_client |
|
24 |
|
25 from cubicweb._exceptions import * |
|
26 from cubicweb.utils import json_dumps |
|
27 |
|
28 |
|
29 class DirectResponse(Exception): |
|
30 """Used to supply a twitted HTTP Response directly""" |
|
31 def __init__(self, response): |
|
32 self.response = response |
|
33 |
|
34 class InvalidSession(CubicWebException): |
|
35 """raised when a session id is found but associated session is not found or |
|
36 invalid""" |
|
37 |
|
38 # Publish related exception |
|
39 |
|
40 class PublishException(CubicWebException): |
|
41 """base class for publishing related exception""" |
|
42 |
|
43 def __init__(self, *args, **kwargs): |
|
44 self.status = kwargs.pop('status', http_client.OK) |
|
45 super(PublishException, self).__init__(*args, **kwargs) |
|
46 |
|
47 class LogOut(PublishException): |
|
48 """raised to ask for deauthentication of a logged in user""" |
|
49 def __init__(self, url=None): |
|
50 super(LogOut, self).__init__() |
|
51 self.url = url |
|
52 |
|
53 class Redirect(PublishException): |
|
54 """raised to redirect the http request""" |
|
55 def __init__(self, location, status=http_client.SEE_OTHER): |
|
56 super(Redirect, self).__init__(status=status) |
|
57 self.location = location |
|
58 |
|
59 class StatusResponse(PublishException): |
|
60 |
|
61 def __init__(self, status, content=''): |
|
62 super(StatusResponse, self).__init__(status=status) |
|
63 self.content = content |
|
64 |
|
65 def __repr__(self): |
|
66 return '%s(%r, %r)' % (self.__class__.__name__, self.status, self.content) |
|
67 |
|
68 # Publish related error |
|
69 |
|
70 class RequestError(PublishException): |
|
71 """raised when a request can't be served because of a bad input""" |
|
72 |
|
73 def __init__(self, *args, **kwargs): |
|
74 kwargs.setdefault('status', http_client.BAD_REQUEST) |
|
75 super(RequestError, self).__init__(*args, **kwargs) |
|
76 |
|
77 |
|
78 class NothingToEdit(RequestError): |
|
79 """raised when an edit request doesn't specify any eid to edit""" |
|
80 |
|
81 def __init__(self, *args, **kwargs): |
|
82 kwargs.setdefault('status', http_client.BAD_REQUEST) |
|
83 super(NothingToEdit, self).__init__(*args, **kwargs) |
|
84 |
|
85 class ProcessFormError(RequestError): |
|
86 """raised when posted data can't be processed by the corresponding field |
|
87 """ |
|
88 def __init__(self, *args, **kwargs): |
|
89 kwargs.setdefault('status', http_client.BAD_REQUEST) |
|
90 super(ProcessFormError, self).__init__(*args, **kwargs) |
|
91 |
|
92 class NotFound(RequestError): |
|
93 """raised when something was not found. In most case, |
|
94 a 404 error should be returned""" |
|
95 |
|
96 def __init__(self, *args, **kwargs): |
|
97 kwargs.setdefault('status', http_client.NOT_FOUND) |
|
98 super(NotFound, self).__init__(*args, **kwargs) |
|
99 |
|
100 class RemoteCallFailed(RequestError): |
|
101 """raised when a json remote call fails |
|
102 """ |
|
103 def __init__(self, reason='', status=http_client.INTERNAL_SERVER_ERROR): |
|
104 super(RemoteCallFailed, self).__init__(reason, status=status) |
|
105 self.reason = reason |
|
106 |
|
107 def dumps(self): |
|
108 return json_dumps({'reason': self.reason}) |
|