File: errors.py

package info (click to toggle)
python-diaspy 0.6.0-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 352 kB
  • sloc: python: 1,600; makefile: 149
file content (164 lines) | stat: -rw-r--r-- 3,564 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env python3

"""This module contains custom exceptions that are raised by diaspy.
These are not described by DIASPORA* protocol as exceptions that should be
raised by API implementations but are specific to this particular implementation.

If your program should catch all exceptions raised by diaspy and
does not need to handle them specifically you can use following code:

	# this line imports all errors
	from diaspy.errors import *

	try:
		# your code...
	except DiaspyError as e:
		# your error handling code...
	finally:
		# closing code...
"""

import warnings


class DiaspyError(Exception):
	"""Base exception for all errors
	raised by diaspy.
	"""
	pass


class LoginError(DiaspyError):
	"""Exception raised when something
	bad happens while performing actions
	related to logging in.
	"""
	pass


class TokenError(DiaspyError):
	pass

class CSRFProtectionKickedIn(TokenError):
	pass


class DataError(DiaspyError):
	pass


class InvalidDataError(DataError):
	pass


class KeyMissingFromFetchedData(InvalidDataError):
	pass


class UserError(DiaspyError):
	"""Exception raised when something related to users goes wrong.
	"""
	pass


class InvalidHandleError(DiaspyError):
	"""Raised when invalid handle is found.
	"""
	pass


class SearchError(DiaspyError):
	"""Exception raised when something related to search goes wrong.
	"""
	pass


class ConversationError(DiaspyError):
	"""Exception raised when something related to conversations goes wrong.
	"""
	pass


class AspectError(DiaspyError):
	"""Exception raised when something related to aspects goes wrong.
	"""
	pass

class UserIsNotMemberOfAspect(AspectError):
	pass


class PostError(DiaspyError):
	"""Exception raised when something related to posts goes wrong.
	"""
	pass


class StreamError(DiaspyError):
	"""Exception raised when something related to streams goes wrong.
	"""
	pass


class SettingsError(DiaspyError):
	"""Exception raised when something related to settings goes wrong.
	"""
	pass

class SearchError(DiaspyError):
	"""Exception raised when something related to searching goes wrong.
	"""
	pass

class TagError(DiaspyError):
	"""Exception raised when something related to a tag goes wrong.
	"""
	pass

def react(r, message='', accepted=[200, 201, 202, 203, 204, 205, 206], exception=DiaspyError):
	"""This method tries to decide how to react
	to a response code passed to it. If it's an
	error code it will raise an exception (it will
	call `throw()` method.

	If response code is not accepted AND cannot
	be matched to any exception, generic exception
	(DiaspyError) is raised (provided that `exception`
	param was left untouched).

	By default `accepted` param contains all HTTP
	success codes.

	User can force type of exception to raise by passing
	`exception` param.

	:param r: response code
	:type r: int
	:param message: message for the exception
	:type message: str
	:param accepted: list of accepted error codes
	:type accepted: list
	:param exception: preferred exception to raise
	:type exception: valid exception type (default: DiaspyError)
	"""
	warnings.warn(DeprecationWarning)
	if r in accepted: e = None
	else: e = DiaspyError

	if e is not None: e = exception
	throw(e, message=message)


def throw(e, message=''):
	"""This function throws an error with given message.
	If None is passed as `e` throw() will not raise
	anything.

	:param e: exception to throw
	:type e: any valid exception type or None
	:param message: message for exception
	:type message: str
	"""
	warnings.warn(DeprecationWarning)
	if e is None: pass
	else: raise e(message)