-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ux): add error better ux for hash not found
This changes introduces the use of `PoetryRuntimeError` that allows better error information propagation to facilitate improved ux for users encountering errors. Resolves: #10057
- Loading branch information
Showing
4 changed files
with
92 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,65 @@ | ||
from __future__ import annotations | ||
|
||
import dataclasses | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
from cleo.exceptions import CleoError | ||
|
||
|
||
if TYPE_CHECKING: | ||
from cleo.io.io import IO | ||
|
||
|
||
class PoetryConsoleError(CleoError): | ||
pass | ||
|
||
|
||
class GroupNotFoundError(PoetryConsoleError): | ||
pass | ||
|
||
|
||
@dataclasses.dataclass | ||
class ConsoleMessage: | ||
text: str | ||
debug: bool = False | ||
|
||
@property | ||
def stripped(self) -> str: | ||
from cleo._utils import strip_tags | ||
|
||
return strip_tags(self.text) | ||
|
||
|
||
class PoetryRuntimeError(PoetryConsoleError): | ||
def __init__( | ||
self, | ||
reason: str, | ||
messages: list[ConsoleMessage] | None = None, | ||
exit_code: int = 1, | ||
) -> None: | ||
super().__init__(reason) | ||
self.exit_code = exit_code | ||
self._messages = messages or [] | ||
self._messages.insert(0, ConsoleMessage(reason + ("\n" if messages else ""))) | ||
|
||
def write(self, io: IO) -> None: | ||
io.write_error_line(self.get_text(debug=io.is_verbose(), strip=False)) | ||
|
||
def get_text( | ||
self, debug: bool = False, indent: str = "", strip: bool = False | ||
) -> str: | ||
text = "" | ||
for message in self._messages: | ||
if message.debug and not debug: | ||
continue | ||
|
||
message_text = message.stripped if strip else message.text | ||
if indent: | ||
message_text = f"\n{indent}".join(message_text.splitlines()) | ||
text += f"{indent}{message_text}\n{indent}\n" | ||
|
||
return text.rstrip(f"{indent}\n") | ||
|
||
def __str__(self) -> str: | ||
return self._messages[0].stripped.strip() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters