Coverage for pyngrok/exception.py: 100.00%

28 statements  

« prev     ^ index     » next       coverage.py v7.4.0, created at 2023-12-30 22:54 +0000

1from typing import Any, Optional, List 

2 

3from pyngrok.log import NgrokLog 

4 

5__author__ = "Alex Laird" 

6__copyright__ = "Copyright 2023, Alex Laird" 

7__version__ = "7.0.0" 

8 

9 

10class PyngrokError(Exception): 

11 """ 

12 Raised when a general ``pyngrok`` error has occurred. 

13 """ 

14 pass 

15 

16 

17class PyngrokSecurityError(PyngrokError): 

18 """ 

19 Raised when a ``pyngrok`` security error has occurred. 

20 """ 

21 pass 

22 

23 

24class PyngrokNgrokInstallError(PyngrokError): 

25 """ 

26 Raised when an error has occurred while downloading and installing the ``ngrok`` binary. 

27 """ 

28 pass 

29 

30 

31class PyngrokNgrokError(PyngrokError): 

32 """ 

33 Raised when an error occurs interacting directly with the ``ngrok`` binary. 

34 """ 

35 

36 def __init__(self, 

37 error: str, 

38 ngrok_logs: Optional[List[NgrokLog]] = None, 

39 ngrok_error: Optional[str] = None) -> None: 

40 super(PyngrokNgrokError, self).__init__(error) 

41 

42 #: The ``ngrok`` logs, which may be useful for debugging the error. 

43 self.ngrok_logs: List[NgrokLog] = ngrok_logs if ngrok_logs else [] 

44 #: The error that caused the ``ngrok`` process to fail. 

45 self.ngrok_error: Optional[str] = ngrok_error 

46 

47 

48class PyngrokNgrokHTTPError(PyngrokNgrokError): 

49 """ 

50 Raised when an error occurs making a request to the ``ngrok`` web interface. The ``body`` 

51 contains the error response received from ``ngrok``. 

52 """ 

53 

54 # When Python <3.9 support is dropped, headers type can be changed to Dict[str, str]|MutableMapping[str, str]|Any 

55 def __init__(self, 

56 error: str, 

57 url: str, 

58 status_code: int, 

59 message: Optional[str], 

60 headers: Any, 

61 body: str) -> None: 

62 super(PyngrokNgrokHTTPError, self).__init__(error) 

63 

64 #: The request URL that failed. 

65 self.url: str = url 

66 #: The response status code from ``ngrok``. 

67 self.status_code: int = status_code 

68 #: The response message from ``ngrok``. 

69 self.message: Optional[str] = message 

70 #: The request headers sent to ``ngrok``. 

71 self.headers: Any = headers 

72 #: The response body from ``ngrok``. 

73 self.body: str = body 

74 

75 

76class PyngrokNgrokURLError(PyngrokNgrokError): 

77 """ 

78 Raised when an error occurs when trying to initiate an API request. 

79 """ 

80 

81 # When Python <3.9 support is dropped, reason type can be changed to str|BaseException 

82 def __init__(self, 

83 error: str, 

84 reason: Any) -> None: 

85 super(PyngrokNgrokURLError, self).__init__(error) 

86 

87 #: The reason for the URL error. 

88 self.reason: Any = reason