Messages are conversations once again
[diaspy.git] / diaspy / errors.py
1 #!/usr/bin/env python3
2
3
4 class DiaspyError(Exception):
5 """Base exception for all errors
6 raised by diaspy.
7 """
8 pass
9
10
11 class LoginError(DiaspyError):
12 """Exception raised when something
13 bad happens while performing actions
14 related to logging in.
15 """
16 pass
17
18
19 class UserError(DiaspyError):
20 """Exception raised when something related to users goes wrong.
21 """
22 pass
23
24
25 class InvalidHandleError(DiaspyError):
26 """Raised when invalid handle is found.
27 """
28 pass
29
30
31 class SearchError(DiaspyError):
32 """Exception raised when something related to search goes wrong.
33 """
34 pass
35
36
37 class ConversationError(DiaspyError):
38 """Exception raised when something related to conversations goes wrong.
39 """
40 pass
41
42
43 class AspectError(DiaspyError):
44 """Exception raised when something related to aspects goes wrong.
45 """
46 pass
47
48
49 class PostError(DiaspyError):
50 """Exception raised when something related to posts goes wrong.
51 """
52 pass
53
54
55 class StreamError(DiaspyError):
56 """Exception raised when something related to streams goes wrong.
57 """
58 pass
59
60
61 def react(r, message='', accepted=[200, 201, 202, 203, 204, 205, 206], exception=DiaspyError):
62 """This method tries to decides how to react
63 to a response code passed to it. If it's an
64 error code it will raise an exception (it will
65 call `throw()` method.
66
67 If response code is not accepted AND cannot
68 be matched to any exception, generic exception
69 (DiaspyError) is raised (provided that `exception`
70 param was left untouched).
71
72 By default `accepted` param contains all HTTP
73 success codes.
74
75 User can force type of exception to raise by passing
76 `exception` param.
77
78 :param r: response code
79 :type r: int
80 :param message: message for the exception
81 :type message: str
82 :param accepted: list of accepted error codes
83 :type accepted: list
84 :param exception: preferred exception to raise
85 :type exception: valid exception type (default: DiaspyError)
86 """
87 if r in accepted: e = None
88 else: e = DiaspyError
89
90 if e is not None: e = exception
91 throw(e, message=message)
92
93
94 def throw(e, message=''):
95 """This function throws an error with given message.
96 If None is passed as `e` throw() will not raise
97 anything.
98
99 :param e: exception to throw
100 :type e: any valid exception type or None
101 :param message: message for exception
102 :type message: str
103 """
104 if e is None: pass
105 else: raise e(message)