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 | ||
42 | ||
d84905af MM |
43 | class DataError(DiaspyError): |
44 | pass | |
45 | ||
46 | ||
47 | class InvalidDataError(DataError): | |
48 | pass | |
49 | ||
50 | ||
51 | class KeyMissingFromFetchedData(InvalidDataError): | |
52 | pass | |
53 | ||
54 | ||
65b1f099 MM |
55 | class UserError(DiaspyError): |
56 | """Exception raised when something related to users goes wrong. | |
57 | """ | |
58 | pass | |
59 | ||
60 | ||
615edb73 MM |
61 | class InvalidHandleError(DiaspyError): |
62 | """Raised when invalid handle is found. | |
63 | """ | |
64 | pass | |
65 | ||
66 | ||
7c6fbe5b | 67 | class SearchError(DiaspyError): |
1530cc84 MM |
68 | """Exception raised when something related to search goes wrong. |
69 | """ | |
70 | pass | |
71 | ||
72 | ||
7ebc8b0d MM |
73 | class ConversationError(DiaspyError): |
74 | """Exception raised when something related to conversations goes wrong. | |
75 | """ | |
76 | pass | |
77 | ||
78 | ||
9896b779 MM |
79 | class AspectError(DiaspyError): |
80 | """Exception raised when something related to aspects goes wrong. | |
81 | """ | |
82 | pass | |
83 | ||
57ceb864 MM |
84 | class UserIsNotMemberOfAspect(AspectError): |
85 | pass | |
86 | ||
9896b779 | 87 | |
fe2181b4 MM |
88 | class PostError(DiaspyError): |
89 | """Exception raised when something related to posts goes wrong. | |
90 | """ | |
91 | pass | |
92 | ||
93 | ||
78cc478a MM |
94 | class StreamError(DiaspyError): |
95 | """Exception raised when something related to streams goes wrong. | |
96 | """ | |
7c6fbe5b MM |
97 | pass |
98 | ||
99 | ||
f50cbea3 MM |
100 | class SettingsError(DiaspyError): |
101 | """Exception raised when something related to settings goes wrong. | |
102 | """ | |
103 | pass | |
104 | ||
105 | ||
22422846 | 106 | def react(r, message='', accepted=[200, 201, 202, 203, 204, 205, 206], exception=DiaspyError): |
2dbc82fe | 107 | """This method tries to decide how to react |
22422846 MM |
108 | to a response code passed to it. If it's an |
109 | error code it will raise an exception (it will | |
110 | call `throw()` method. | |
111 | ||
112 | If response code is not accepted AND cannot | |
113 | be matched to any exception, generic exception | |
114 | (DiaspyError) is raised (provided that `exception` | |
115 | param was left untouched). | |
116 | ||
117 | By default `accepted` param contains all HTTP | |
118 | success codes. | |
119 | ||
120 | User can force type of exception to raise by passing | |
121 | `exception` param. | |
122 | ||
123 | :param r: response code | |
124 | :type r: int | |
125 | :param message: message for the exception | |
126 | :type message: str | |
127 | :param accepted: list of accepted error codes | |
128 | :type accepted: list | |
129 | :param exception: preferred exception to raise | |
130 | :type exception: valid exception type (default: DiaspyError) | |
131 | """ | |
39af9756 | 132 | warnings.warn(DeprecationWarning) |
22422846 MM |
133 | if r in accepted: e = None |
134 | else: e = DiaspyError | |
135 | ||
136 | if e is not None: e = exception | |
137 | throw(e, message=message) | |
138 | ||
139 | ||
140 | def throw(e, message=''): | |
141 | """This function throws an error with given message. | |
142 | If None is passed as `e` throw() will not raise | |
143 | anything. | |
144 | ||
145 | :param e: exception to throw | |
146 | :type e: any valid exception type or None | |
147 | :param message: message for exception | |
148 | :type message: str | |
149 | """ | |
39af9756 | 150 | warnings.warn(DeprecationWarning) |
22422846 MM |
151 | if e is None: pass |
152 | else: raise e(message) |