|
1
|
|
|
''' |
|
2
|
|
|
Definition of our own error classes. |
|
3
|
|
|
|
|
4
|
|
|
django.http responses are regular returns, the transaction management therefore |
|
5
|
|
|
always commit changes even if we return erroneous responses (400, 404, ...). We can |
|
6
|
|
|
bypass this behaviour by throwing exception that send correct HTTP status to the user |
|
7
|
|
|
but abort the transaction. |
|
8
|
|
|
''' |
|
9
|
|
|
|
|
10
|
|
|
from django.http import HttpResponse, HttpResponseRedirect, HttpResponsePermanentRedirect, HttpResponseNotModified, \ |
|
11
|
|
|
HttpResponseBadRequest, HttpResponseNotFound, HttpResponseForbidden, HttpResponseNotAllowed, HttpResponseGone, \ |
|
12
|
|
|
HttpResponseServerError |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
class OreException(Exception): |
|
16
|
|
|
|
|
17
|
|
|
''' |
|
18
|
|
|
As of Python 2.7, BaseException has no more default message attribute. |
|
19
|
|
|
We therefore define our own base class for this. |
|
20
|
|
|
''' |
|
21
|
|
|
_message = "" |
|
22
|
|
|
|
|
23
|
|
|
def _get_message(self): |
|
24
|
|
|
return self._message |
|
25
|
|
|
|
|
26
|
|
|
def _set_message(self, message): |
|
27
|
|
|
self._message = message |
|
28
|
|
|
|
|
29
|
|
|
def __init__(self, message=''): |
|
30
|
|
|
self.message = message |
|
31
|
|
|
message = property(_get_message, _set_message) |
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
class HttpResponseRedirectAnswer(Exception): |
|
35
|
|
|
|
|
36
|
|
|
def __init__(self, target): |
|
37
|
|
|
self.target = target |
|
38
|
|
|
|
|
39
|
|
|
def result(self): |
|
40
|
|
|
return HttpResponseRedirect(self.target) |
|
41
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
class HttpResponsePermanentRedirectAnswer(Exception): |
|
44
|
|
|
|
|
45
|
|
|
def __init__(self, target): |
|
46
|
|
|
self.target = target |
|
47
|
|
|
|
|
48
|
|
|
def result(self): |
|
49
|
|
|
return HttpResponsePermanentRedirect(self.target) |
|
50
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
class HttpResponseNotModifiedAnswer(OreException): |
|
53
|
|
|
|
|
54
|
|
|
def result(self): |
|
55
|
|
|
return HttpResponseNotModified(self.message) |
|
56
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
class HttpResponseBadRequestAnswer(OreException): |
|
59
|
|
|
|
|
60
|
|
|
def result(self): |
|
61
|
|
|
return HttpResponseBadRequest(self.message) |
|
62
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
class HttpResponseNotFoundAnswer(OreException): |
|
65
|
|
|
|
|
66
|
|
|
def result(self): |
|
67
|
|
|
return HttpResponseNotFound(self.message) |
|
68
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
class HttpResponseForbiddenAnswer(OreException): |
|
71
|
|
|
|
|
72
|
|
|
def result(self): |
|
73
|
|
|
return HttpResponseForbidden(self.message) |
|
74
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
class HttpResponseNotAllowedAnswer(Exception): |
|
77
|
|
|
|
|
78
|
|
|
def __init__(self, allowedMethods): |
|
79
|
|
|
self.allowedMethods = allowedMethods |
|
80
|
|
|
|
|
81
|
|
|
def result(self): |
|
82
|
|
|
return HttpResponseNotAllowed(self.allowedMethods) |
|
83
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
class HttpResponseGoneAnswer(OreException): |
|
86
|
|
|
|
|
87
|
|
|
def result(self): |
|
88
|
|
|
return HttpResponseGone(self.message) |
|
89
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
class HttpResponseServerErrorAnswer(OreException): |
|
92
|
|
|
|
|
93
|
|
|
def result(self): |
|
94
|
|
|
return HttpResponseServerError(self.message) |
|
95
|
|
|
|
|
96
|
|
|
|
|
97
|
|
|
class HttpResponseCreated(HttpResponse): |
|
98
|
|
|
status_code = 201 |
|
99
|
|
|
|
|
100
|
|
|
|
|
101
|
|
|
class HttpResponseCreatedAnswer(Exception): |
|
102
|
|
|
|
|
103
|
|
|
def result(self): |
|
104
|
|
|
return HttpResponseCreated() |
|
105
|
|
|
|
|
106
|
|
|
|
|
107
|
|
|
class HttpResponseNoResponse(HttpResponse): |
|
108
|
|
|
status_code = 204 |
|
109
|
|
|
|
|
110
|
|
|
|
|
111
|
|
|
class HttpResponseAccepted(HttpResponse): |
|
112
|
|
|
status_code = 202 |
|
113
|
|
|
|
|
114
|
|
|
|
|
115
|
|
|
class HttpResponseNoResponseAnswer(OreException): |
|
116
|
|
|
|
|
117
|
|
|
def result(self): |
|
118
|
|
|
return HttpResponseNoResponse(self.message) |
|
119
|
|
|
|
|
120
|
|
|
|
|
121
|
|
|
class HttpErrorMiddleware(object): |
|
122
|
|
|
|
|
123
|
|
|
def process_exception(self, request, exception): |
|
124
|
|
|
if hasattr(exception, 'result'): |
|
125
|
|
|
return exception.result() |
|
126
|
|
|
else: |
|
127
|
|
|
# default exception handling kicks in |
|
128
|
|
|
return None |
|
129
|
|
|
|