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