25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
3.7KB

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