-
Notifications
You must be signed in to change notification settings - Fork 200
/
exceptions.py
135 lines (92 loc) · 3.67 KB
/
exceptions.py
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
# -*- coding: utf-8 -*-
"""
The class hierachy for exceptions is::
Error
+-- VersionMismatch
+-- EncoderError
| +-- EncoderMissingError
| +-- EncodingError
| +-- DecodingError
+-- CommunicationError
+-- ProtocolError
+-- StatusError
+-- ErrorResponse
+-- PartialErrorResponse
+-- ConnectionError
+-- TimeoutError
"""
class Error(Exception):
"""Base class for all exceptions in this module."""
pass
class VersionMismatch(Error):
"""Raised when daemon version is not supported by this client version."""
def __init__(self, current, minimum, maximum):
self.current = current
self.minimum = minimum
self.maximum = maximum
msg = "Unsupported daemon version '{}' (not in range: {} – {})".format(
current, minimum, maximum
)
Error.__init__(self, msg)
###############
# encoding.py #
###############
class EncoderError(Error):
"""Base class for all encoding and decoding related errors."""
def __init__(self, message, encoder_name):
self.encoder_name = encoder_name
Error.__init__(self, message)
class EncoderMissingError(EncoderError):
"""Raised when a requested encoder class does not actually exist."""
def __init__(self, encoder_name):
msg = "Unknown encoder: '{}'".format(encoder_name)
EncoderError.__init__(self, msg, encoder_name)
class EncodingError(EncoderError):
"""Raised when encoding a Python object into a byte string has failed
due to some problem with the input data."""
def __init__(self, encoder_name, original):
self.original = original
msg = "Object encoding error: {}".format(original)
EncoderError.__init__(self, msg, encoder_name)
class DecodingError(EncoderError):
"""Raised when decoding a byte string to a Python object has failed due to
some problem with the input data."""
def __init__(self, encoder_name, original):
self.original = original
msg = "Object decoding error: {}".format(original)
EncoderError.__init__(self, msg, encoder_name)
###########
# http.py #
###########
class CommunicationError(Error):
"""Base class for all network communication related errors."""
def __init__(self, original, _message=None):
self.original = original
if _message:
msg = _message
else:
msg = "{}: {}".format(original.__class__.__name__, str(original))
Error.__init__(self, msg)
class ProtocolError(CommunicationError):
"""Raised when parsing the response from the daemon has failed.
This can most likely occur if the service on the remote end isn't in fact
an IPFS daemon."""
class StatusError(CommunicationError):
"""Raised when the daemon responds with an error to our request."""
class ErrorResponse(StatusError):
"""Raised when the daemon has responded with an error message because the
requested operation could not be carried out."""
def __init__(self, message, original):
StatusError.__init__(self, original, message)
class PartialErrorResponse(ErrorResponse):
"""Raised when the daemon has responded with an error message after having
already returned some data.
The incomplete data returned may be accessed using the ``partial``
attribute."""
def __init__(self, message, original, partial):
self.partial = partial
ErrorResponse.__init__(self, message, original)
class ConnectionError(CommunicationError):
"""Raised when connecting to the service has failed on the socket layer."""
class TimeoutError(CommunicationError):
"""Raised when the daemon didn't respond in time."""