Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename custom exceptions form *Exception to *Error #179

Merged
merged 1 commit into from
Nov 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
58 changes: 38 additions & 20 deletions src/cleo/exceptions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,56 @@
from cleo._utils import find_similar_names


class CleoException(Exception):
class CleoError(Exception):
"""
Base Cleo exception.
"""

exit_code: int | None = None


class CleoSimpleException(Exception):
class CleoLogicError(CleoError):
"""
Raised when there is error in command arguments
and/or options configuration logic.
"""

pass

class CleoRuntimeError(CleoError):
"""
Raised when command is called with invalid options or arguments.
"""

class LogicException(CleoException):

pass
class CleoValueError(CleoError):
"""
Raised when wrong value was given to Cleo components.
"""


class RuntimeException(CleoException):
class CleoNoSuchOptionError(CleoError):
"""
Raised when command does not have given option.
"""

pass

class CleoUserError(CleoError):
"""
Base exception for user errors.
"""

class ValueException(CleoException):

pass
class CleoMissingArgumentsError(CleoUserError):
"""
Raised when called command was not given required arguments.
"""


class MissingArgumentsException(CleoSimpleException):
class CleoCommandNotFoundError(CleoUserError):
"""
Raised when called command does not exist.
"""

pass


class NoSuchOptionException(CleoException):

pass


class CommandNotFoundException(CleoSimpleException):
def __init__(self, name: str, commands: list[str] | None = None) -> None:
message = f'The command "{name}" does not exist.'

Expand All @@ -56,7 +70,11 @@ def __init__(self, name: str, commands: list[str] | None = None) -> None:
super().__init__(message)


class NamespaceNotFoundException(CleoSimpleException):
class CleoNamespaceNotFoundError(CleoUserError):
"""
Raised when called namespace has no commands.
"""

def __init__(self, name: str, namespaces: list[str] | None = None) -> None:
message = f'There are no commands in the "{name}" namespace.'

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