blob: f07e4486577381616e7e2f7f05747ed7374316a1 [file] [log] [blame]
Andrew Svetlov0baa72f2018-09-11 10:13:04 -07001"""asyncio exceptions."""
2
3
4__all__ = ('CancelledError', 'InvalidStateError', 'TimeoutError',
5 'IncompleteReadError', 'LimitOverrunError',
6 'SendfileNotAvailableError')
7
Andrew Svetlov0baa72f2018-09-11 10:13:04 -07008
Yury Selivanov431b5402019-05-27 14:45:12 +02009class CancelledError(BaseException):
Andrew Svetlov0baa72f2018-09-11 10:13:04 -070010 """The Future or Task was cancelled."""
11
12
Yury Selivanov431b5402019-05-27 14:45:12 +020013class TimeoutError(Exception):
Andrew Svetlov0baa72f2018-09-11 10:13:04 -070014 """The operation exceeded the given deadline."""
15
16
Yury Selivanov431b5402019-05-27 14:45:12 +020017class InvalidStateError(Exception):
Andrew Svetlov0baa72f2018-09-11 10:13:04 -070018 """The operation is not allowed in this state."""
19
20
21class SendfileNotAvailableError(RuntimeError):
22 """Sendfile syscall is not available.
23
24 Raised if OS does not support sendfile syscall for given socket or
25 file type.
26 """
27
28
29class IncompleteReadError(EOFError):
30 """
31 Incomplete read error. Attributes:
32
33 - partial: read bytes string before the end of stream was reached
34 - expected: total number of expected bytes (or None if unknown)
35 """
36 def __init__(self, partial, expected):
Zackery Spytz8085f742020-11-28 07:27:28 -070037 r_expected = 'undefined' if expected is None else repr(expected)
Andrew Svetlov0baa72f2018-09-11 10:13:04 -070038 super().__init__(f'{len(partial)} bytes read on a total of '
Zackery Spytz8085f742020-11-28 07:27:28 -070039 f'{r_expected} expected bytes')
Andrew Svetlov0baa72f2018-09-11 10:13:04 -070040 self.partial = partial
41 self.expected = expected
42
43 def __reduce__(self):
44 return type(self), (self.partial, self.expected)
45
46
47class LimitOverrunError(Exception):
48 """Reached the buffer limit while looking for a separator.
49
50 Attributes:
51 - consumed: total number of to be consumed bytes.
52 """
53 def __init__(self, message, consumed):
54 super().__init__(message)
55 self.consumed = consumed
56
57 def __reduce__(self):
58 return type(self), (self.args[0], self.consumed)