Merge branch 'fladrian-patch-for-d-0.2.0.0' into devel
[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 TokenError(Exception):
20 pass
21
22
23 class UserError(DiaspyError):
24 """Exception raised when something related to users goes wrong.
25 """
26 pass
27
28
29 class InvalidHandleError(DiaspyError):
30 """Raised when invalid handle is found.
31 """
32 pass
33
34
35 class SearchError(DiaspyError):
36 """Exception raised when something related to search goes wrong.
37 """
38 pass
39
40
41 class ConversationError(DiaspyError):
42 """Exception raised when something related to conversations goes wrong.
43 """
44 pass
45
46
47 class AspectError(DiaspyError):
48 """Exception raised when something related to aspects goes wrong.
49 """
50 pass
51
52
53 class PostError(DiaspyError):
54 """Exception raised when something related to posts goes wrong.
55 """
56 pass
57
58
59 class StreamError(DiaspyError):
60 """Exception raised when something related to streams goes wrong.
61 """
62 pass
63
64
65 def react(r, message='', accepted=[200, 201, 202, 203, 204, 205, 206], exception=DiaspyError):
66 """This method tries to decides how to react
67 to a response code passed to it. If it's an
68 error code it will raise an exception (it will
69 call `throw()` method.
70
71 If response code is not accepted AND cannot
72 be matched to any exception, generic exception
73 (DiaspyError) is raised (provided that `exception`
74 param was left untouched).
75
76 By default `accepted` param contains all HTTP
77 success codes.
78
79 User can force type of exception to raise by passing
80 `exception` param.
81
82 :param r: response code
83 :type r: int
84 :param message: message for the exception
85 :type message: str
86 :param accepted: list of accepted error codes
87 :type accepted: list
88 :param exception: preferred exception to raise
89 :type exception: valid exception type (default: DiaspyError)
90 """
91 if r in accepted: e = None
92 else: e = DiaspyError
93
94 if e is not None: e = exception
95 throw(e, message=message)
96
97
98 def throw(e, message=''):
99 """This function throws an error with given message.
100 If None is passed as `e` throw() will not raise
101 anything.
102
103 :param e: exception to throw
104 :type e: any valid exception type or None
105 :param message: message for exception
106 :type message: str
107 """
108 if e is None: pass
109 else: raise e(message)