source: uri/rfc3986/exceptions.py@ 1276

Last change on this file since 1276 was 230, checked in by wouter, 3 years ago

#91 clone https://pypi.org/project/rfc3986/

File size: 3.8 KB
Line 
1# -*- coding: utf-8 -*-
2"""Exceptions module for rfc3986."""
3
4from . import compat
5
6
7class RFC3986Exception(Exception):
8 """Base class for all rfc3986 exception classes."""
9
10 pass
11
12
13class InvalidAuthority(RFC3986Exception):
14 """Exception when the authority string is invalid."""
15
16 def __init__(self, authority):
17 """Initialize the exception with the invalid authority."""
18 super(InvalidAuthority, self).__init__(
19 u"The authority ({0}) is not valid.".format(
20 compat.to_str(authority)
21 )
22 )
23
24
25class InvalidPort(RFC3986Exception):
26 """Exception when the port is invalid."""
27
28 def __init__(self, port):
29 """Initialize the exception with the invalid port."""
30 super(InvalidPort, self).__init__(
31 'The port ("{0}") is not valid.'.format(port)
32 )
33
34
35class ResolutionError(RFC3986Exception):
36 """Exception to indicate a failure to resolve a URI."""
37
38 def __init__(self, uri):
39 """Initialize the error with the failed URI."""
40 super(ResolutionError, self).__init__(
41 "{0} is not an absolute URI.".format(uri.unsplit())
42 )
43
44
45class ValidationError(RFC3986Exception):
46 """Exception raised during Validation of a URI."""
47
48 pass
49
50
51class MissingComponentError(ValidationError):
52 """Exception raised when a required component is missing."""
53
54 def __init__(self, uri, *component_names):
55 """Initialize the error with the missing component name."""
56 verb = "was"
57 if len(component_names) > 1:
58 verb = "were"
59
60 self.uri = uri
61 self.components = sorted(component_names)
62 components = ", ".join(self.components)
63 super(MissingComponentError, self).__init__(
64 "{} {} required but missing".format(components, verb),
65 uri,
66 self.components,
67 )
68
69
70class UnpermittedComponentError(ValidationError):
71 """Exception raised when a component has an unpermitted value."""
72
73 def __init__(self, component_name, component_value, allowed_values):
74 """Initialize the error with the unpermitted component."""
75 super(UnpermittedComponentError, self).__init__(
76 "{} was required to be one of {!r} but was {!r}".format(
77 component_name,
78 list(sorted(allowed_values)),
79 component_value,
80 ),
81 component_name,
82 component_value,
83 allowed_values,
84 )
85 self.component_name = component_name
86 self.component_value = component_value
87 self.allowed_values = allowed_values
88
89
90class PasswordForbidden(ValidationError):
91 """Exception raised when a URL has a password in the userinfo section."""
92
93 def __init__(self, uri):
94 """Initialize the error with the URI that failed validation."""
95 unsplit = getattr(uri, "unsplit", lambda: uri)
96 super(PasswordForbidden, self).__init__(
97 '"{}" contained a password when validation forbade it'.format(
98 unsplit()
99 )
100 )
101 self.uri = uri
102
103
104class InvalidComponentsError(ValidationError):
105 """Exception raised when one or more components are invalid."""
106
107 def __init__(self, uri, *component_names):
108 """Initialize the error with the invalid component name(s)."""
109 verb = "was"
110 if len(component_names) > 1:
111 verb = "were"
112
113 self.uri = uri
114 self.components = sorted(component_names)
115 components = ", ".join(self.components)
116 super(InvalidComponentsError, self).__init__(
117 "{} {} found to be invalid".format(components, verb),
118 uri,
119 self.components,
120 )
121
122
123class MissingDependencyError(RFC3986Exception):
124 """Exception raised when an IRI is encoded without the 'idna' module."""
Note: See TracBrowser for help on using the repository browser.