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