Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/rfc3986/exceptions.py: 57%

44 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-07 06:04 +0000

1"""Exceptions module for rfc3986.""" 

2from . import compat 

3 

4 

5class RFC3986Exception(Exception): 

6 """Base class for all rfc3986 exception classes.""" 

7 

8 pass 

9 

10 

11class InvalidAuthority(RFC3986Exception): 

12 """Exception when the authority string is invalid.""" 

13 

14 def __init__(self, authority): 

15 """Initialize the exception with the invalid authority.""" 

16 super().__init__( 

17 f"The authority ({compat.to_str(authority)}) is not valid." 

18 ) 

19 

20 

21class InvalidPort(RFC3986Exception): 

22 """Exception when the port is invalid.""" 

23 

24 def __init__(self, port): 

25 """Initialize the exception with the invalid port.""" 

26 super().__init__(f'The port ("{port}") is not valid.') 

27 

28 

29class ResolutionError(RFC3986Exception): 

30 """Exception to indicate a failure to resolve a URI.""" 

31 

32 def __init__(self, uri): 

33 """Initialize the error with the failed URI.""" 

34 super().__init__( 

35 "{} does not meet the requirements for resolution.".format( 

36 uri.unsplit() 

37 ) 

38 ) 

39 

40 

41class ValidationError(RFC3986Exception): 

42 """Exception raised during Validation of a URI.""" 

43 

44 pass 

45 

46 

47class MissingComponentError(ValidationError): 

48 """Exception raised when a required component is missing.""" 

49 

50 def __init__(self, uri, *component_names): 

51 """Initialize the error with the missing component name.""" 

52 verb = "was" 

53 if len(component_names) > 1: 

54 verb = "were" 

55 

56 self.uri = uri 

57 self.components = sorted(component_names) 

58 components = ", ".join(self.components) 

59 super().__init__( 

60 f"{components} {verb} required but missing", 

61 uri, 

62 self.components, 

63 ) 

64 

65 

66class UnpermittedComponentError(ValidationError): 

67 """Exception raised when a component has an unpermitted value.""" 

68 

69 def __init__(self, component_name, component_value, allowed_values): 

70 """Initialize the error with the unpermitted component.""" 

71 super().__init__( 

72 "{} was required to be one of {!r} but was {!r}".format( 

73 component_name, 

74 list(sorted(allowed_values)), 

75 component_value, 

76 ), 

77 component_name, 

78 component_value, 

79 allowed_values, 

80 ) 

81 self.component_name = component_name 

82 self.component_value = component_value 

83 self.allowed_values = allowed_values 

84 

85 

86class PasswordForbidden(ValidationError): 

87 """Exception raised when a URL has a password in the userinfo section.""" 

88 

89 def __init__(self, uri): 

90 """Initialize the error with the URI that failed validation.""" 

91 unsplit = getattr(uri, "unsplit", lambda: uri) 

92 super().__init__( 

93 '"{}" contained a password when validation forbade it'.format( 

94 unsplit() 

95 ) 

96 ) 

97 self.uri = uri 

98 

99 

100class InvalidComponentsError(ValidationError): 

101 """Exception raised when one or more components are invalid.""" 

102 

103 def __init__(self, uri, *component_names): 

104 """Initialize the error with the invalid component name(s).""" 

105 verb = "was" 

106 if len(component_names) > 1: 

107 verb = "were" 

108 

109 self.uri = uri 

110 self.components = sorted(component_names) 

111 components = ", ".join(self.components) 

112 super().__init__( 

113 f"{components} {verb} found to be invalid", 

114 uri, 

115 self.components, 

116 ) 

117 

118 

119class MissingDependencyError(RFC3986Exception): 

120 """Exception raised when an IRI is encoded without the 'idna' module."""