Coverage for nexios\exceptions.py: 72%

25 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-05-21 20:31 +0100

1from __future__ import annotations 

2import http 

3import typing 

4 

5 

6class HTTPException(Exception): 

7 def __init__( 

8 self, 

9 status_code: int, 

10 detail: typing.Optional[typing.Any] = None, 

11 headers: typing.Dict[str, typing.Any] = {}, 

12 ) -> None: 

13 super().__init__(detail or http.HTTPStatus(status_code).phrase) 

14 self.status_code = status_code 

15 self.detail = self.args[0] 

16 self.headers = headers 

17 

18 def __str__(self) -> str: 

19 return f"HTTP {self.status_code}: {self.detail}" 

20 

21 def __repr__(self) -> str: 

22 return f"{self.__class__.__name__}({self.status_code}, {self.detail!r})" 

23 

24 

25class NotFoundException(HTTPException): 

26 def __init__( 

27 self, 

28 detail: typing.Optional[str] = None, 

29 headers: typing.Dict[str, typing.Any] = {}, 

30 ) -> None: 

31 super().__init__(status_code=404, detail=detail or "Not Found", headers=headers) 

32 

33 

34class WebSocketException(Exception): 

35 def __init__(self, code: int, reason: typing.Optional[str] = None) -> None: 

36 super().__init__(reason or "") 

37 self.code = code 

38 self.reason = self.args[0] 

39 

40 def __str__(self) -> str: 

41 return f"WebSocket {self.code}: {self.reason}" 

42 

43 def __repr__(self) -> str: 

44 return f"{self.__class__.__name__}({self.code}, {self.reason!r})"