* __upd__: Add `Comment()` to `diaspy.models.Post.comments` on `diaspy.models.Post...
[diaspy.git] / diaspy / errors.py
CommitLineData
22422846
MM
1#!/usr/bin/env python3
2
39af9756
MM
3"""This module contains custom exceptions that are raised by diaspy.
4These are not described by DIASPORA* protocol as exceptions that should be
5raised by API implementations but are specific to this particular implementation.
6
7If your program should catch all exceptions raised by diaspy and
8does not need to handle them specifically you can use following code:
9
d95ff94a
C
10 # this line imports all errors
11 from diaspy.errors import *
12
13 try:
14 # your code...
15 except DiaspyError as e:
16 # your error handling code...
17 finally:
18 # closing code...
39af9756
MM
19"""
20
21import warnings
22
22422846
MM
23
24class DiaspyError(Exception):
d95ff94a
C
25 """Base exception for all errors
26 raised by diaspy.
27 """
28 pass
22422846
MM
29
30
31class LoginError(DiaspyError):
d95ff94a
C
32 """Exception raised when something
33 bad happens while performing actions
34 related to logging in.
35 """
36 pass
22422846
MM
37
38
39af9756 39class TokenError(DiaspyError):
d95ff94a 40 pass
2cf8467c 41
2bb910ea 42class CSRFProtectionKickedIn(TokenError):
d95ff94a 43 pass
2bb910ea 44
2cf8467c 45
d84905af 46class DataError(DiaspyError):
d95ff94a 47 pass
d84905af
MM
48
49
50class InvalidDataError(DataError):
d95ff94a 51 pass
d84905af
MM
52
53
54class KeyMissingFromFetchedData(InvalidDataError):
d95ff94a 55 pass
d84905af
MM
56
57
65b1f099 58class UserError(DiaspyError):
d95ff94a
C
59 """Exception raised when something related to users goes wrong.
60 """
61 pass
65b1f099
MM
62
63
615edb73 64class InvalidHandleError(DiaspyError):
d95ff94a
C
65 """Raised when invalid handle is found.
66 """
67 pass
615edb73
MM
68
69
7c6fbe5b 70class SearchError(DiaspyError):
d95ff94a
C
71 """Exception raised when something related to search goes wrong.
72 """
73 pass
1530cc84 74
b9d087bb
C
75class NotificationError(DiaspyError):
76 """Exception raised when something related to notifications goes wrong.
77 """
78 pass
1530cc84 79
7ebc8b0d 80class ConversationError(DiaspyError):
d95ff94a
C
81 """Exception raised when something related to conversations goes wrong.
82 """
83 pass
7ebc8b0d
MM
84
85
9896b779 86class AspectError(DiaspyError):
d95ff94a
C
87 """Exception raised when something related to aspects goes wrong.
88 """
89 pass
9896b779 90
57ceb864 91class UserIsNotMemberOfAspect(AspectError):
d95ff94a 92 pass
57ceb864 93
9896b779 94
fe2181b4 95class PostError(DiaspyError):
d95ff94a
C
96 """Exception raised when something related to posts goes wrong.
97 """
98 pass
fe2181b4
MM
99
100
78cc478a 101class StreamError(DiaspyError):
d95ff94a
C
102 """Exception raised when something related to streams goes wrong.
103 """
104 pass
7c6fbe5b
MM
105
106
f50cbea3 107class SettingsError(DiaspyError):
d95ff94a
C
108 """Exception raised when something related to settings goes wrong.
109 """
110 pass
f50cbea3 111
0c28bf0b 112class SearchError(DiaspyError):
d95ff94a
C
113 """Exception raised when something related to searching goes wrong.
114 """
115 pass
0c28bf0b
C
116
117class TagError(DiaspyError):
8a24529b 118 """Exception raised when something related to a tag goes wrong.
d95ff94a
C
119 """
120 pass
f50cbea3 121
22422846 122def react(r, message='', accepted=[200, 201, 202, 203, 204, 205, 206], exception=DiaspyError):
d95ff94a
C
123 """This method tries to decide how to react
124 to a response code passed to it. If it's an
125 error code it will raise an exception (it will
126 call `throw()` method.
127
128 If response code is not accepted AND cannot
129 be matched to any exception, generic exception
130 (DiaspyError) is raised (provided that `exception`
131 param was left untouched).
132
133 By default `accepted` param contains all HTTP
134 success codes.
135
136 User can force type of exception to raise by passing
137 `exception` param.
138
139 :param r: response code
140 :type r: int
141 :param message: message for the exception
142 :type message: str
143 :param accepted: list of accepted error codes
144 :type accepted: list
145 :param exception: preferred exception to raise
146 :type exception: valid exception type (default: DiaspyError)
147 """
148 warnings.warn(DeprecationWarning)
149 if r in accepted: e = None
150 else: e = DiaspyError
151
152 if e is not None: e = exception
153 throw(e, message=message)
22422846
MM
154
155
156def throw(e, message=''):
d95ff94a
C
157 """This function throws an error with given message.
158 If None is passed as `e` throw() will not raise
159 anything.
160
161 :param e: exception to throw
162 :type e: any valid exception type or None
163 :param message: message for exception
164 :type message: str
165 """
166 warnings.warn(DeprecationWarning)
167 if e is None: pass
168 else: raise e(message)