Commit | Line | Data |
---|---|---|
22422846 MM |
1 | #!/usr/bin/env python3 |
2 | ||
39af9756 MM |
3 | """This module contains custom exceptions that are raised by diaspy. |
4 | These are not described by DIASPORA* protocol as exceptions that should be | |
5 | raised by API implementations but are specific to this particular implementation. | |
6 | ||
7 | If your program should catch all exceptions raised by diaspy and | |
8 | does not need to handle them specifically you can use following code: | |
9 | ||
10 | # this line imports all errors | |
11 | from diaspy.errors import * | |
12 | ||
13 | try: | |
14 | # your code... | |
15 | except DiaspyError as e: | |
16 | # your error handling code... | |
17 | finally: | |
18 | # closing code... | |
19 | """ | |
20 | ||
21 | import warnings | |
22 | ||
22422846 MM |
23 | |
24 | class DiaspyError(Exception): | |
25 | """Base exception for all errors | |
26 | raised by diaspy. | |
27 | """ | |
28 | pass | |
29 | ||
30 | ||
31 | class LoginError(DiaspyError): | |
32 | """Exception raised when something | |
33 | bad happens while performing actions | |
34 | related to logging in. | |
35 | """ | |
36 | pass | |
37 | ||
38 | ||
39af9756 | 39 | class TokenError(DiaspyError): |
2cf8467c MM |
40 | pass |
41 | ||
2bb910ea MM |
42 | class CSRFProtectionKickedIn(TokenError): |
43 | pass | |
44 | ||
2cf8467c | 45 | |
d84905af MM |
46 | class DataError(DiaspyError): |
47 | pass | |
48 | ||
49 | ||
50 | class InvalidDataError(DataError): | |
51 | pass | |
52 | ||
53 | ||
54 | class KeyMissingFromFetchedData(InvalidDataError): | |
55 | pass | |
56 | ||
57 | ||
65b1f099 MM |
58 | class UserError(DiaspyError): |
59 | """Exception raised when something related to users goes wrong. | |
60 | """ | |
61 | pass | |
62 | ||
63 | ||
615edb73 MM |
64 | class InvalidHandleError(DiaspyError): |
65 | """Raised when invalid handle is found. | |
66 | """ | |
67 | pass | |
68 | ||
69 | ||
7c6fbe5b | 70 | class SearchError(DiaspyError): |
1530cc84 MM |
71 | """Exception raised when something related to search goes wrong. |
72 | """ | |
73 | pass | |
74 | ||
75 | ||
7ebc8b0d MM |
76 | class ConversationError(DiaspyError): |
77 | """Exception raised when something related to conversations goes wrong. | |
78 | """ | |
79 | pass | |
80 | ||
81 | ||
9896b779 MM |
82 | class AspectError(DiaspyError): |
83 | """Exception raised when something related to aspects goes wrong. | |
84 | """ | |
85 | pass | |
86 | ||
57ceb864 MM |
87 | class UserIsNotMemberOfAspect(AspectError): |
88 | pass | |
89 | ||
9896b779 | 90 | |
fe2181b4 MM |
91 | class PostError(DiaspyError): |
92 | """Exception raised when something related to posts goes wrong. | |
93 | """ | |
94 | pass | |
95 | ||
96 | ||
78cc478a MM |
97 | class StreamError(DiaspyError): |
98 | """Exception raised when something related to streams goes wrong. | |
99 | """ | |
7c6fbe5b MM |
100 | pass |
101 | ||
102 | ||
f50cbea3 MM |
103 | class SettingsError(DiaspyError): |
104 | """Exception raised when something related to settings goes wrong. | |
105 | """ | |
106 | pass | |
107 | ||
108 | ||
22422846 | 109 | def react(r, message='', accepted=[200, 201, 202, 203, 204, 205, 206], exception=DiaspyError): |
2dbc82fe | 110 | """This method tries to decide how to react |
22422846 MM |
111 | to a response code passed to it. If it's an |
112 | error code it will raise an exception (it will | |
113 | call `throw()` method. | |
114 | ||
115 | If response code is not accepted AND cannot | |
116 | be matched to any exception, generic exception | |
117 | (DiaspyError) is raised (provided that `exception` | |
118 | param was left untouched). | |
119 | ||
120 | By default `accepted` param contains all HTTP | |
121 | success codes. | |
122 | ||
123 | User can force type of exception to raise by passing | |
124 | `exception` param. | |
125 | ||
126 | :param r: response code | |
127 | :type r: int | |
128 | :param message: message for the exception | |
129 | :type message: str | |
130 | :param accepted: list of accepted error codes | |
131 | :type accepted: list | |
132 | :param exception: preferred exception to raise | |
133 | :type exception: valid exception type (default: DiaspyError) | |
134 | """ | |
39af9756 | 135 | warnings.warn(DeprecationWarning) |
22422846 MM |
136 | if r in accepted: e = None |
137 | else: e = DiaspyError | |
138 | ||
139 | if e is not None: e = exception | |
140 | throw(e, message=message) | |
141 | ||
142 | ||
143 | def throw(e, message=''): | |
144 | """This function throws an error with given message. | |
145 | If None is passed as `e` throw() will not raise | |
146 | anything. | |
147 | ||
148 | :param e: exception to throw | |
149 | :type e: any valid exception type or None | |
150 | :param message: message for exception | |
151 | :type message: str | |
152 | """ | |
39af9756 | 153 | warnings.warn(DeprecationWarning) |
22422846 MM |
154 | if e is None: pass |
155 | else: raise e(message) |