exceptions.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. from ._compat import PY2, filename_to_ui, get_text_stderr
  2. from .utils import echo
  3. class ClickException(Exception):
  4. """An exception that Click can handle and show to the user."""
  5. #: The exit code for this exception
  6. exit_code = 1
  7. def __init__(self, message):
  8. if PY2:
  9. if message is not None:
  10. message = message.encode('utf-8')
  11. Exception.__init__(self, message)
  12. self.message = message
  13. def format_message(self):
  14. return self.message
  15. def show(self, file=None):
  16. if file is None:
  17. file = get_text_stderr()
  18. echo('Error: %s' % self.format_message(), file=file)
  19. class UsageError(ClickException):
  20. """An internal exception that signals a usage error. This typically
  21. aborts any further handling.
  22. :param message: the error message to display.
  23. :param ctx: optionally the context that caused this error. Click will
  24. fill in the context automatically in some situations.
  25. """
  26. exit_code = 2
  27. def __init__(self, message, ctx=None):
  28. ClickException.__init__(self, message)
  29. self.ctx = ctx
  30. def show(self, file=None):
  31. if file is None:
  32. file = get_text_stderr()
  33. color = None
  34. if self.ctx is not None:
  35. color = self.ctx.color
  36. echo(self.ctx.get_usage() + '\n', file=file, color=color)
  37. echo('Error: %s' % self.format_message(), file=file, color=color)
  38. class BadParameter(UsageError):
  39. """An exception that formats out a standardized error message for a
  40. bad parameter. This is useful when thrown from a callback or type as
  41. Click will attach contextual information to it (for instance, which
  42. parameter it is).
  43. .. versionadded:: 2.0
  44. :param param: the parameter object that caused this error. This can
  45. be left out, and Click will attach this info itself
  46. if possible.
  47. :param param_hint: a string that shows up as parameter name. This
  48. can be used as alternative to `param` in cases
  49. where custom validation should happen. If it is
  50. a string it's used as such, if it's a list then
  51. each item is quoted and separated.
  52. """
  53. def __init__(self, message, ctx=None, param=None,
  54. param_hint=None):
  55. UsageError.__init__(self, message, ctx)
  56. self.param = param
  57. self.param_hint = param_hint
  58. def format_message(self):
  59. if self.param_hint is not None:
  60. param_hint = self.param_hint
  61. elif self.param is not None:
  62. param_hint = self.param.opts or [self.param.human_readable_name]
  63. else:
  64. return 'Invalid value: %s' % self.message
  65. if isinstance(param_hint, (tuple, list)):
  66. param_hint = ' / '.join('"%s"' % x for x in param_hint)
  67. return 'Invalid value for %s: %s' % (param_hint, self.message)
  68. class MissingParameter(BadParameter):
  69. """Raised if click required an option or argument but it was not
  70. provided when invoking the script.
  71. .. versionadded:: 4.0
  72. :param param_type: a string that indicates the type of the parameter.
  73. The default is to inherit the parameter type from
  74. the given `param`. Valid values are ``'parameter'``,
  75. ``'option'`` or ``'argument'``.
  76. """
  77. def __init__(self, message=None, ctx=None, param=None,
  78. param_hint=None, param_type=None):
  79. BadParameter.__init__(self, message, ctx, param, param_hint)
  80. self.param_type = param_type
  81. def format_message(self):
  82. if self.param_hint is not None:
  83. param_hint = self.param_hint
  84. elif self.param is not None:
  85. param_hint = self.param.opts or [self.param.human_readable_name]
  86. else:
  87. param_hint = None
  88. if isinstance(param_hint, (tuple, list)):
  89. param_hint = ' / '.join('"%s"' % x for x in param_hint)
  90. param_type = self.param_type
  91. if param_type is None and self.param is not None:
  92. param_type = self.param.param_type_name
  93. msg = self.message
  94. if self.param is not None:
  95. msg_extra = self.param.type.get_missing_message(self.param)
  96. if msg_extra:
  97. if msg:
  98. msg += '. ' + msg_extra
  99. else:
  100. msg = msg_extra
  101. return 'Missing %s%s%s%s' % (
  102. param_type,
  103. param_hint and ' %s' % param_hint or '',
  104. msg and '. ' or '.',
  105. msg or '',
  106. )
  107. class NoSuchOption(UsageError):
  108. """Raised if click attempted to handle an option that does not
  109. exist.
  110. .. versionadded:: 4.0
  111. """
  112. def __init__(self, option_name, message=None, possibilities=None,
  113. ctx=None):
  114. if message is None:
  115. message = 'no such option: %s' % option_name
  116. UsageError.__init__(self, message, ctx)
  117. self.option_name = option_name
  118. self.possibilities = possibilities
  119. def format_message(self):
  120. bits = [self.message]
  121. if self.possibilities:
  122. if len(self.possibilities) == 1:
  123. bits.append('Did you mean %s?' % self.possibilities[0])
  124. else:
  125. possibilities = sorted(self.possibilities)
  126. bits.append('(Possible options: %s)' % ', '.join(possibilities))
  127. return ' '.join(bits)
  128. class BadOptionUsage(UsageError):
  129. """Raised if an option is generally supplied but the use of the option
  130. was incorrect. This is for instance raised if the number of arguments
  131. for an option is not correct.
  132. .. versionadded:: 4.0
  133. """
  134. def __init__(self, message, ctx=None):
  135. UsageError.__init__(self, message, ctx)
  136. class BadArgumentUsage(UsageError):
  137. """Raised if an argument is generally supplied but the use of the argument
  138. was incorrect. This is for instance raised if the number of values
  139. for an argument is not correct.
  140. .. versionadded:: 6.0
  141. """
  142. def __init__(self, message, ctx=None):
  143. UsageError.__init__(self, message, ctx)
  144. class FileError(ClickException):
  145. """Raised if a file cannot be opened."""
  146. def __init__(self, filename, hint=None):
  147. ui_filename = filename_to_ui(filename)
  148. if hint is None:
  149. hint = 'unknown error'
  150. ClickException.__init__(self, hint)
  151. self.ui_filename = ui_filename
  152. self.filename = filename
  153. def format_message(self):
  154. return 'Could not open file %s: %s' % (self.ui_filename, self.message)
  155. class Abort(RuntimeError):
  156. """An internal signalling exception that signals Click to abort."""