Moved `User().fetchprofile()` functionality to `Search().users()`
[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 pass
27
28
29 def react(r, message='', accepted=[200, 201, 202, 203, 204, 205, 206], exception=DiaspyError):
30 """This method tries to decides how to react
31 to a response code passed to it. If it's an
32 error code it will raise an exception (it will
33 call `throw()` method.
34
35 If response code is not accepted AND cannot
36 be matched to any exception, generic exception
37 (DiaspyError) is raised (provided that `exception`
38 param was left untouched).
39
40 By default `accepted` param contains all HTTP
41 success codes.
42
43 User can force type of exception to raise by passing
44 `exception` param.
45
46 :param r: response code
47 :type r: int
48 :param message: message for the exception
49 :type message: str
50 :param accepted: list of accepted error codes
51 :type accepted: list
52 :param exception: preferred exception to raise
53 :type exception: valid exception type (default: DiaspyError)
54 """
55 if r in accepted: e = None
56 else: e = DiaspyError
57
58 if e is not None: e = exception
59 throw(e, message=message)
60
61
62 def throw(e, message=''):
63 """This function throws an error with given message.
64 If None is passed as `e` throw() will not raise
65 anything.
66
67 :param e: exception to throw
68 :type e: any valid exception type or None
69 :param message: message for exception
70 :type message: str
71 """
72 if e is None: pass
73 else: raise e(message)