Skip to content

Commit

Permalink
Apply exceptions refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Secrus committed Nov 15, 2022
1 parent 6f034a9 commit 5154d66
Show file tree
Hide file tree
Showing 20 changed files with 89 additions and 89 deletions.
26 changes: 13 additions & 13 deletions src/cleo/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
from cleo.events.console_events import ERROR
from cleo.events.console_events import TERMINATE
from cleo.events.console_terminate_event import ConsoleTerminateEvent
from cleo.exceptions import CleoException
from cleo.exceptions import CleoSimpleException
from cleo.exceptions import CommandNotFoundException
from cleo.exceptions import LogicException
from cleo.exceptions import NamespaceNotFoundException
from cleo.exceptions import CleoCommandNotFoundError
from cleo.exceptions import CleoError
from cleo.exceptions import CleoLogicError
from cleo.exceptions import CleoNamespaceNotFoundError
from cleo.exceptions import CleoUserError
from cleo.io.inputs.argument import Argument
from cleo.io.inputs.argv_input import ArgvInput
from cleo.io.inputs.definition import Definition
Expand Down Expand Up @@ -186,7 +186,7 @@ def add(self, command: Command) -> Command | None:
return None

if not command.name:
raise LogicException(
raise CleoLogicError(
f'The command "{command.__class__.__name__}" cannot have an empty name'
)

Expand All @@ -201,11 +201,11 @@ def get(self, name: str) -> Command:
self._init()

if not self.has(name):
raise CommandNotFoundException(name)
raise CleoCommandNotFoundError(name)

if name not in self._commands:
# The command was registered in a different name in the command loader
raise CommandNotFoundException(name)
raise CleoCommandNotFoundError(name)

command = self._commands[name]

Expand Down Expand Up @@ -261,7 +261,7 @@ def find_namespace(self, namespace: str) -> str:
all_namespaces = self.get_namespaces()

if namespace not in all_namespaces:
raise NamespaceNotFoundException(namespace, all_namespaces)
raise CleoNamespaceNotFoundError(namespace, all_namespaces)

return namespace

Expand All @@ -279,7 +279,7 @@ def find(self, name: str) -> Command:
name for name, command in self._commands.items() if not command.hidden
]

raise CommandNotFoundException(name, all_commands)
raise CleoCommandNotFoundError(name, all_commands)

def all(self, namespace: str | None = None) -> dict[str, Command]:
self._init()
Expand Down Expand Up @@ -370,7 +370,7 @@ def _run(self, io: IO) -> int:

# Errors must be ignored, full binding/validation
# happens later when the command is known.
with suppress(CleoException):
with suppress(CleoError):
# Makes ArgvInput.first_argument() able to
# distinguish an option from an argument.
io.input.bind(input_definition)
Expand Down Expand Up @@ -442,7 +442,7 @@ def _run_command(self, command: Command, io: IO) -> int:
try:
command.merge_application_definition()
io.input.bind(command.definition)
except CleoException:
except CleoError:
# Ignore invalid option/arguments for now,
# to allow the listeners to customize the definition
pass
Expand Down Expand Up @@ -498,7 +498,7 @@ def render_error(self, error: Exception, io: IO) -> None:
trace = ExceptionTrace(
error, solution_provider_repository=self._solution_provider_repository
)
simple = not io.is_verbose() or isinstance(error, CleoSimpleException)
simple = not io.is_verbose() or isinstance(error, CleoUserError)
trace.render(io.error_output, simple)

def _configure_io(self, io: IO) -> None:
Expand Down
6 changes: 3 additions & 3 deletions src/cleo/color.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import os

from cleo.exceptions import ValueException
from cleo.exceptions import CleoValueError


class Color:
Expand Down Expand Up @@ -107,14 +107,14 @@ def _parse_color(self, color: str, background: bool) -> str:
color = color[0] * 2 + color[1] * 2 + color[2] * 2

if len(color) != 6:
raise ValueException(f'"{color}" is an invalid color')
raise CleoValueError(f'"{color}" is an invalid color')

return ("4" if background else "3") + self._convert_hex_color_to_ansi(
int(color, 16)
)

if color not in self.COLORS:
raise ValueException(
raise CleoValueError(
f'"{color}" is an invalid color.'
f' It must be one of {", ".join(self.COLORS.keys())}'
)
Expand Down
4 changes: 2 additions & 2 deletions src/cleo/commands/base_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from typing import TYPE_CHECKING

from cleo.exceptions import CleoException
from cleo.exceptions import CleoError
from cleo.io.inputs.definition import Definition


Expand Down Expand Up @@ -102,7 +102,7 @@ def run(self, io: IO) -> int:

try:
io.input.bind(self.definition)
except CleoException:
except CleoError:
if not self._ignore_validation_errors:
raise

Expand Down
4 changes: 2 additions & 2 deletions src/cleo/descriptors/application_description.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from collections import defaultdict
from typing import TYPE_CHECKING

from cleo.exceptions import CommandNotFoundException
from cleo.exceptions import CleoCommandNotFoundError


if TYPE_CHECKING:
Expand Down Expand Up @@ -43,7 +43,7 @@ def command(self, name: str) -> Command:
return self._commands[name]
if name in self._aliases:
return self._aliases[name]
raise CommandNotFoundException(name)
raise CleoCommandNotFoundError(name)

def _inspect_application(self) -> None:
namespace = None
Expand Down
4 changes: 2 additions & 2 deletions src/cleo/events/console_error_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import TYPE_CHECKING

from cleo.events.console_event import ConsoleEvent
from cleo.exceptions import CleoException
from cleo.exceptions import CleoError


if TYPE_CHECKING:
Expand Down Expand Up @@ -31,7 +31,7 @@ def exit_code(self) -> int:
if self._exit_code is not None:
return self._exit_code

if isinstance(self._error, CleoException) and self._error.exit_code is not None:
if isinstance(self._error, CleoError) and self._error.exit_code is not None:
return self._error.exit_code

return 1
Expand Down
4 changes: 2 additions & 2 deletions src/cleo/formatters/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import re

from cleo.exceptions import ValueException
from cleo.exceptions import CleoValueError
from cleo.formatters.style import Style
from cleo.formatters.style_stack import StyleStack

Expand Down Expand Up @@ -71,7 +71,7 @@ def has_style(self, name: str) -> bool:

def style(self, name: str) -> Style:
if not self.has_style(name):
raise ValueException(f'Undefined style: "{name}"')
raise CleoValueError(f'Undefined style: "{name}"')

return self._styles[name]

Expand Down
4 changes: 2 additions & 2 deletions src/cleo/formatters/style_stack.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from cleo.exceptions import ValueException
from cleo.exceptions import CleoValueError
from cleo.formatters.style import Style


Expand Down Expand Up @@ -38,4 +38,4 @@ def pop(self, style: Style | None = None) -> Style:

return stacked_style

raise ValueException("Invalid nested tag found")
raise CleoValueError("Invalid nested tag found")
6 changes: 3 additions & 3 deletions src/cleo/io/inputs/argument.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from typing import Any

from cleo.exceptions import LogicException
from cleo.exceptions import CleoLogicError


class Argument:
Expand Down Expand Up @@ -46,13 +46,13 @@ def is_list(self) -> bool:

def set_default(self, default: Any | None = None) -> None:
if self._required and default is not None:
raise LogicException("Cannot set a default value for required arguments")
raise CleoLogicError("Cannot set a default value for required arguments")

if self._is_list:
if default is None:
default = []
elif not isinstance(default, list):
raise LogicException(
raise CleoLogicError(
"A default value for a list argument must be a list"
)

Expand Down
16 changes: 8 additions & 8 deletions src/cleo/io/inputs/argv_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
from typing import TYPE_CHECKING
from typing import Any

from cleo.exceptions import NoSuchOptionException
from cleo.exceptions import RuntimeException
from cleo.exceptions import CleoNoSuchOptionError
from cleo.exceptions import CleoRuntimeError
from cleo.io.inputs.input import Input


Expand Down Expand Up @@ -193,7 +193,7 @@ def _parse_short_option_set(self, name: str) -> None:
length = len(name)
for i in range(length):
if not self._definition.has_shortcut(name[i]):
raise RuntimeException(f'The option "{name[i]}" does not exist')
raise CleoRuntimeError(f'The option "{name[i]}" does not exist')

option = self._definition.option_for_shortcut(name[i])
if option.accepts_value():
Expand Down Expand Up @@ -259,24 +259,24 @@ def _parse_argument(self, token: str) -> None:
else:
message = f'No arguments expected, got "{token}"'

raise RuntimeException(message)
raise CleoRuntimeError(message)

def _add_short_option(self, shortcut: str, value: Any) -> None:
if not self._definition.has_shortcut(shortcut):
raise NoSuchOptionException(f'The option "-{shortcut}" does not exist')
raise CleoNoSuchOptionError(f'The option "-{shortcut}" does not exist')

self._add_long_option(
self._definition.option_for_shortcut(shortcut).name, value
)

def _add_long_option(self, name: str, value: Any) -> None:
if not self._definition.has_option(name):
raise NoSuchOptionException(f'The option "--{name}" does not exist')
raise CleoNoSuchOptionError(f'The option "--{name}" does not exist')

option = self._definition.option(name)

if value is not None and not option.accepts_value():
raise RuntimeException(f'The "--{name}" option does not accept a value')
raise CleoRuntimeError(f'The "--{name}" option does not accept a value')

if value in ["", None] and option.accepts_value() and self._parsed:
# If the option accepts a value, either required or optional,
Expand All @@ -289,7 +289,7 @@ def _add_long_option(self, name: str, value: Any) -> None:

if value is None:
if option.requires_value():
raise RuntimeException(f'The "--{name}" option requires a value')
raise CleoRuntimeError(f'The "--{name}" option requires a value')

if not option.is_list() and option.is_flag():
value = True
Expand Down
12 changes: 6 additions & 6 deletions src/cleo/io/inputs/definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from typing import Any
from typing import Sequence

from cleo.exceptions import LogicException
from cleo.exceptions import CleoLogicError
from cleo.io.inputs.option import Option


Expand Down Expand Up @@ -94,15 +94,15 @@ def add_arguments(self, arguments: list[Argument]) -> None:

def add_argument(self, argument: Argument) -> None:
if argument.name in self._arguments:
raise LogicException(
raise CleoLogicError(
f'An argument with name "{argument.name}" already exists'
)

if self._has_a_list_argument:
raise LogicException("Cannot add an argument after a list argument")
raise CleoLogicError("Cannot add an argument after a list argument")

if argument.is_required() and self._has_optional:
raise LogicException("Cannot add a required argument after an optional one")
raise CleoLogicError("Cannot add a required argument after an optional one")

if argument.is_list():
self._has_a_list_argument = True
Expand Down Expand Up @@ -149,15 +149,15 @@ def add_options(self, options: list[Option]) -> None:

def add_option(self, option: Option) -> None:
if option.name in self._options and option != self._options[option.name]:
raise LogicException(f'An option named "{option.name}" already exists')
raise CleoLogicError(f'An option named "{option.name}" already exists')

if option.shortcut:
for shortcut in option.shortcut.split("|"):
if (
shortcut in self._shortcuts
and option.name != self._shortcuts[shortcut]
):
raise LogicException(
raise CleoLogicError(
f'An option with shortcut "{shortcut}" already exists'
)

Expand Down
14 changes: 7 additions & 7 deletions src/cleo/io/inputs/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from typing import TextIO

from cleo._compat import shell_quote
from cleo.exceptions import MissingArgumentsException
from cleo.exceptions import ValueException
from cleo.exceptions import CleoMissingArgumentsError
from cleo.exceptions import CleoValueError
from cleo.io.inputs.definition import Definition


Expand Down Expand Up @@ -107,13 +107,13 @@ def validate(self) -> None:
missing_arguments.append(argument.name)

if missing_arguments:
raise MissingArgumentsException(
raise CleoMissingArgumentsError(
f'Not enough arguments (missing: "{", ".join(missing_arguments)}")'
)

def argument(self, name: str) -> Any:
if not self._definition.has_argument(name):
raise ValueException(f'The argument "{name}" does not exist')
raise CleoValueError(f'The argument "{name}" does not exist')

if name in self._arguments:
return self._arguments[name]
Expand All @@ -122,7 +122,7 @@ def argument(self, name: str) -> Any:

def set_argument(self, name: str, value: Any) -> None:
if not self._definition.has_argument(name):
raise ValueException(f'The argument "{name}" does not exist')
raise CleoValueError(f'The argument "{name}" does not exist')

self._arguments[name] = value

Expand All @@ -131,7 +131,7 @@ def has_argument(self, name: str) -> bool:

def option(self, name: str) -> Any:
if not self._definition.has_option(name):
raise ValueException(f'The option "--{name}" does not exist')
raise CleoValueError(f'The option "--{name}" does not exist')

if name in self._options:
return self._options[name]
Expand All @@ -140,7 +140,7 @@ def option(self, name: str) -> Any:

def set_option(self, name: str, value: Any) -> None:
if not self._definition.has_option(name):
raise ValueException(f'The option "--{name}" does not exist')
raise CleoValueError(f'The option "--{name}" does not exist')

self._options[name] = value

Expand Down

0 comments on commit 5154d66

Please sign in to comment.