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