|
1 | 1 | from django.http import JsonResponse, HttpResponseNotAllowed |
2 | 2 |
|
3 | | -class ServerException(Exception): |
| 3 | + |
| 4 | +class PrintableException(Exception): |
| 5 | + |
| 6 | + def toDict(self): |
| 7 | + return {"type": self.__class__.__name__} |
| 8 | + |
| 9 | + def __str__(self): |
| 10 | + return self.__class__.__name__ |
| 11 | + |
| 12 | + def __repr__(self): |
| 13 | + return repr(self.toDict()) |
| 14 | + |
| 15 | + |
| 16 | +class ServerException(PrintableException): |
4 | 17 |
|
5 | 18 | def buildResponse(self): |
6 | 19 | raise NotImplementedError() |
7 | 20 |
|
| 21 | + |
8 | 22 | class JSONServerException(ServerException): |
9 | 23 |
|
10 | | - def __init__(self, status, message=None, data=None): |
| 24 | + def __init__(self, status): |
11 | 25 | self._status = status |
12 | | - self._message = message |
13 | | - self._data = data |
14 | | - |
15 | | - def getData(self): |
16 | | - return self._data |
17 | 26 |
|
18 | 27 | def getStatus(self): |
19 | 28 | return self._status |
20 | 29 |
|
| 30 | + def buildResponse(self): |
| 31 | + return JsonResponse(self.toDict(), status=self._status) |
| 32 | + |
| 33 | + |
| 34 | +class CommonException(PrintableException): |
| 35 | + |
| 36 | + def __init__(self, message=None, cause=None): |
| 37 | + self._message = message |
| 38 | + self._cause = cause |
| 39 | + |
| 40 | + def getCause(self): |
| 41 | + return self._cause |
| 42 | + |
21 | 43 | def getMessage(self): |
22 | 44 | return self._message |
23 | 45 |
|
24 | | - def buildResponse(self): |
25 | | - content = {} |
| 46 | + def toDict(self): |
| 47 | + content = super().toDict() |
26 | 48 | if self._message is not None: |
27 | 49 | content["message"] = self._message |
28 | | - if self._data is not None: |
29 | | - content["data"] = self._data |
30 | | - return JsonResponse(content, status=self._status) |
| 50 | + if self._cause is not None: |
| 51 | + content["cause"] = self._cause.toDict() |
| 52 | + return content |
| 53 | + |
| 54 | + def __str__(self): |
| 55 | + txt = self.__class__.__name__ |
| 56 | + if self._message is not None: |
| 57 | + txt += ": {}".format(self._message) |
| 58 | + if self._cause is not None: |
| 59 | + txt += "\nCaused by:\n{}".format(str(self._cause)) |
| 60 | + return txt |
| 61 | + |
| 62 | + |
| 63 | +class CommonJSONServerException(JSONServerException, CommonException): |
31 | 64 |
|
| 65 | + def __init__(self, status, message=None, cause=None): |
| 66 | + JSONServerException.__init__(self, status) |
| 67 | + CommonException.__init__(self, message, cause) |
32 | 68 |
|
33 | | -class AuthenticationException(JSONServerException): |
34 | 69 |
|
35 | | - def __init__(self, message=None, data=None): |
36 | | - super().__init__(401, message, data) |
| 70 | +class AuthenticationException(CommonJSONServerException): |
37 | 71 |
|
| 72 | + def __init__(self, message=None, cause=None): |
| 73 | + super().__init__(401, message, cause) |
38 | 74 |
|
39 | | -class BadRequestException(JSONServerException): |
40 | 75 |
|
41 | | - def __init__(self, message=None, data=None): |
42 | | - super().__init__(400, message, data) |
| 76 | +class BadRequestException(CommonJSONServerException): |
43 | 77 |
|
| 78 | + def __init__(self, message=None, cause=None): |
| 79 | + super().__init__(400, message, cause) |
44 | 80 |
|
45 | | -class NotFoundException(JSONServerException): |
46 | 81 |
|
47 | | - def __init__(self, message=None, data=None): |
48 | | - super().__init__(404, message, data) |
| 82 | +class NotFoundException(CommonJSONServerException): |
49 | 83 |
|
| 84 | + def __init__(self, message=None, cause=None): |
| 85 | + super().__init__(404, message, cause) |
50 | 86 |
|
51 | | -class ServerErrorException(JSONServerException): |
52 | 87 |
|
53 | | - def __init__(self, message=None, data=None): |
54 | | - super().__init__(500, message, data) |
| 88 | +class ServerErrorException(CommonJSONServerException): |
| 89 | + |
| 90 | + def __init__(self, message=None, cause=None): |
| 91 | + super().__init__(500, message, cause) |
55 | 92 |
|
56 | 93 |
|
57 | 94 | class MethodNotAllowedException(ServerException): |
58 | 95 |
|
59 | 96 | def __init__(self, allowedMethods): |
60 | 97 | self._allowedMethods = allowedMethods |
61 | 98 |
|
| 99 | + def toDict(self): |
| 100 | + content = super().toDict() |
| 101 | + content["allowedMethods"] = self._allowedMethods |
| 102 | + return content |
| 103 | + |
| 104 | + def __str__(self): |
| 105 | + return "{}\nAllowed methods:\n{}".format(self.__class__.__name__, self._allowedMethods) |
| 106 | + |
62 | 107 | def buildResponse(self): |
63 | 108 | return HttpResponseNotAllowed(self._allowedMethods) |
0 commit comments