1 from traceback import format_exception

2 from typing import List

3

4

5 class BrokenResourceError(Exception):

6 """

7 Raised when trying to use a resource that has been rendered unusable due to external causes

8 (e.g. a send stream whose peer has disconnected).

9 """

10

11

12 class BrokenWorkerProcess(Exception):

13 """

14 Raised by :func:`run_sync_in_process` if the worker process terminates abruptly or otherwise

15 misbehaves.

16 """

17

18

19 class BusyResourceError(Exception):

20 """Raised when two tasks are trying to read from or write to the same resource concurrently."""

21

22 def __init__(self, action: str):

23 super().__init__(f"Another task is already {action} this resource")

24

25

26 class ClosedResourceError(Exception):

27 """Raised when trying to use a resource that has been closed."""

28

29

30 class DelimiterNotFound(Exception):

31 """

32 Raised during :meth:`~anyio.streams.buffered.BufferedByteReceiveStream.receive_until` if the

33 maximum number of bytes has been read without the delimiter being found.

34 """

35

36 def __init__(self, max_bytes: int) -> None:

37 super().__init__(

38 f"The delimiter was not found among the first {max_bytes} bytes"

39 )

40

41

42 class EndOfStream(Exception):

43 """Raised when trying to read from a stream that has been closed from the other end."""

44

45

46 class ExceptionGroup(BaseException):

47 """

48 Raised when multiple exceptions have been raised in a task group.

49

50 :var ~typing.Sequence[BaseException] exceptions: the sequence of exceptions raised together

51 """

52

53 SEPARATOR = "----------------------------\n"

54

55 exceptions: List[BaseException]

56

57 def __str__(self) -> str:

58 tracebacks = [

59 "".join(format_exception(type(exc), exc, exc.__traceback__))

60 for exc in self.exceptions

61 ]

62 return (

63 f"{len(self.exceptions)} exceptions were raised in the task group:\n"

64 f"{self.SEPARATOR}{self.SEPARATOR.join(tracebacks)}"

65 )

66

67 def __repr__(self) -> str:

68 exception_reprs = ", ".join(repr(exc) for exc in self.exceptions)

69 return f"<{self.__class__.__name__}: {exception_reprs}>"

70

71

72 class IncompleteRead(Exception):

73 """

74 Raised during :meth:`~anyio.streams.buffered.BufferedByteReceiveStream.receive_exactly` or

75 :meth:`~anyio.streams.buffered.BufferedByteReceiveStream.receive_until` if the

76 connection is closed before the requested amount of bytes has been read.

77 """

78

79 def __init__(self) -> None:

80 super().__init__(

81 "The stream was closed before the read operation could be completed"

82 )

83

84

85 class TypedAttributeLookupError(LookupError):

86 """

87 Raised by :meth:`~anyio.TypedAttributeProvider.extra` when the given typed attribute is not

88 found and no default value has been given.

89 """

90

91

92 class WouldBlock(Exception):

93 """Raised by ``X_nowait`` functions if ``X()`` would block."""