* __upd__: `diaspy.models.Post.like()`, `diaspy.models.Post.delete_like()`, `diaspy...
[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
MM
74
75
7ebc8b0d 76class ConversationError(DiaspyError):
d95ff94a
C
77 """Exception raised when something related to conversations goes wrong.
78 """
79 pass
7ebc8b0d
MM
80
81
9896b779 82class AspectError(DiaspyError):
d95ff94a
C
83 """Exception raised when something related to aspects goes wrong.
84 """
85 pass
9896b779 86
57ceb864 87class UserIsNotMemberOfAspect(AspectError):
d95ff94a 88 pass
57ceb864 89
9896b779 90
fe2181b4 91class PostError(DiaspyError):
d95ff94a
C
92 """Exception raised when something related to posts goes wrong.
93 """
94 pass
fe2181b4
MM
95
96
78cc478a 97class StreamError(DiaspyError):
d95ff94a
C
98 """Exception raised when something related to streams goes wrong.
99 """
100 pass
7c6fbe5b
MM
101
102
f50cbea3 103class SettingsError(DiaspyError):
d95ff94a
C
104 """Exception raised when something related to settings goes wrong.
105 """
106 pass
f50cbea3 107
0c28bf0b 108class SearchError(DiaspyError):
d95ff94a
C
109 """Exception raised when something related to searching goes wrong.
110 """
111 pass
0c28bf0b
C
112
113class TagError(DiaspyError):
8a24529b 114 """Exception raised when something related to a tag goes wrong.
d95ff94a
C
115 """
116 pass
f50cbea3 117
22422846 118def react(r, message='', accepted=[200, 201, 202, 203, 204, 205, 206], exception=DiaspyError):
d95ff94a
C
119 """This method tries to decide how to react
120 to a response code passed to it. If it's an
121 error code it will raise an exception (it will
122 call `throw()` method.
123
124 If response code is not accepted AND cannot
125 be matched to any exception, generic exception
126 (DiaspyError) is raised (provided that `exception`
127 param was left untouched).
128
129 By default `accepted` param contains all HTTP
130 success codes.
131
132 User can force type of exception to raise by passing
133 `exception` param.
134
135 :param r: response code
136 :type r: int
137 :param message: message for the exception
138 :type message: str
139 :param accepted: list of accepted error codes
140 :type accepted: list
141 :param exception: preferred exception to raise
142 :type exception: valid exception type (default: DiaspyError)
143 """
144 warnings.warn(DeprecationWarning)
145 if r in accepted: e = None
146 else: e = DiaspyError
147
148 if e is not None: e = exception
149 throw(e, message=message)
22422846
MM
150
151
152def throw(e, message=''):
d95ff94a
C
153 """This function throws an error with given message.
154 If None is passed as `e` throw() will not raise
155 anything.
156
157 :param e: exception to throw
158 :type e: any valid exception type or None
159 :param message: message for exception
160 :type message: str
161 """
162 warnings.warn(DeprecationWarning)
163 if e is None: pass
164 else: raise e(message)