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

feat(ux): add error better ux for hash not found #10065

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

abn
Copy link
Member

@abn abn commented Jan 17, 2025

This changes introduces the use of PoetryRuntimeError that allows better error information propagation to facilitate improved ux for users encountering errors.

Resolves: #10057

Before

Screenshot From 2025-01-17 03-14-01

After

Screenshot From 2025-01-17 03-13-21

After (verbose)

Screenshot From 2025-01-17 03-12-38

After (Multiple)

image

Summary by Sourcery

Improve the user experience when a package hash is not found during installation by providing more informative error messages.

Bug Fixes:

  • Fixed an issue where unclear error messages were displayed when package hashes did not match the lock file during installation.

Enhancements:

  • Improved error handling and reporting during package installation.

This changes introduces the use of `PoetryRuntimeError` that allows
better error information propagation to facilitate improved ux for users
encountering errors.

Resolves: python-poetry#10057
@abn abn added area/error-handling Bad error messages/insufficient error handling area/ux Features and improvements related to the user experience labels Jan 17, 2025
@abn abn requested a review from a team January 17, 2025 02:18
@abn
Copy link
Member Author

abn commented Jan 17, 2025

@sourcery-ai review

1 similar comment
@abn
Copy link
Member Author

abn commented Jan 17, 2025

@sourcery-ai review

Copy link

sourcery-ai bot commented Jan 17, 2025

Reviewer's Guide by Sourcery

This PR introduces the PoetryRuntimeError to improve user experience when encountering hash mismatches during package installation. This involves changes to exception handling, error messaging, and tests.

Sequence diagram for hash mismatch error handling

sequenceDiagram
    participant Chooser as Package Chooser
    participant Error as PoetryRuntimeError
    participant Executor as Executor
    participant IO as IO Interface

    Chooser->>Error: create(reason, messages)
    Note over Chooser,Error: Hash mismatch detected
    Error->>Error: format messages
    Executor->>Error: get_text(verbose, indent)
    Error->>IO: write error message
    Note over IO: Display formatted error<br/>with optional debug info
Loading

Class diagram for new error handling classes

classDiagram
    class CleoError {
    }
    class PoetryConsoleError {
    }
    class GroupNotFoundError {
    }
    class ConsoleMessage {
        +str text
        +bool debug
        +str stripped()
    }
    class PoetryRuntimeError {
        -list[ConsoleMessage] _messages
        -int exit_code
        +__init__(reason: str, messages: list[ConsoleMessage], exit_code: int)
        +write(io: IO)
        +get_text(debug: bool, indent: str, strip: bool) str
        +__str__() str
    }
    CleoError <|-- PoetryConsoleError
    PoetryConsoleError <|-- GroupNotFoundError
    PoetryConsoleError <|-- PoetryRuntimeError
    PoetryRuntimeError o-- ConsoleMessage
    note for PoetryRuntimeError "New class for improved error handling"
Loading

File-Level Changes

Change Details Files
Introduce PoetryRuntimeError for improved error handling
  • Added PoetryRuntimeError class to handle runtime errors with custom messages and exit codes.
  • Added ConsoleMessage dataclass to structure error messages with debug information.
  • Updated PoetryRuntimeError to include methods for writing and formatting error messages with debug information and indentation.
  • Added __str__ method to PoetryRuntimeError to provide a concise error message.
src/poetry/console/exceptions.py
Update package installation error handling
  • Modified _get_links in chooser.py to raise PoetryRuntimeError with detailed information about hash mismatches.
  • Updated _execute_operation in executor.py to handle PoetryRuntimeError and display formatted error messages with debug information.
src/poetry/installation/chooser.py
src/poetry/installation/executor.py
Update tests for improved error handling
  • Modified tests to check for PoetryRuntimeError instead of RuntimeError.
  • Added assertions to verify the content and formatting of the error messages, including debug information.
tests/installation/test_chooser.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @abn - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟡 Testing: 2 issues found
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

src/poetry/installation/executor.py Show resolved Hide resolved
Comment on lines +373 to +378
reason = f"Downloaded distributions for {package.name} ({package.version}) did not match any known checksums in your lock file."
assert str(e.value) == reason

text = e.value.get_text(debug=True, strip=True)
assert reason in text
assert files[0]["hash"] in text
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (testing): Test both debug and non-debug text.

It would be beneficial to also test the non-debug version of the error message to ensure that the correct information is displayed to users in both verbose and non-verbose modes.

Suggested change
reason = f"Downloaded distributions for {package.name} ({package.version}) did not match any known checksums in your lock file."
assert str(e.value) == reason
text = e.value.get_text(debug=True, strip=True)
assert reason in text
assert files[0]["hash"] in text
reason = f"Downloaded distributions for {package.name} ({package.version}) did not match any known checksums in your lock file."
assert str(e.value) == reason
# Test non-debug error message
text = e.value.get_text(debug=False, strip=True)
assert reason in text
assert files[0]["hash"] not in text # Hash should not be included in non-debug output
# Test debug error message
debug_text = e.value.get_text(debug=True, strip=True)
assert reason in debug_text
assert files[0]["hash"] in debug_text # Hash should be included in debug output

Comment on lines +376 to 380
text = e.value.get_text(debug=True, strip=True)
assert reason in text
assert files[0]["hash"] in text


Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (testing): Add tests for multiple mismatched hashes.

Consider adding a test case where multiple files have mismatched hashes to ensure the error message handles this scenario correctly. The current tests only cover a single mismatch.

Suggested change
text = e.value.get_text(debug=True, strip=True)
assert reason in text
assert files[0]["hash"] in text
text = e.value.get_text(debug=True, strip=True)
assert reason in text
assert files[0]["hash"] in text
def test_chooser_with_multiple_mismatched_hashes(package, chooser):
files = [
{
"name": "demo-0.1.0.tar.gz",
"hash": "incorrect_hash_1",
"url": "https://files.pythonhosted.org/demo-0.1.0.tar.gz",
},
{
"name": "demo-0.1.0-py2.py3-none-any.whl",
"hash": "incorrect_hash_2",
"url": "https://files.pythonhosted.org/demo-0.1.0-py2.py3-none-any.whl",
}
]
package.files = files
with pytest.raises(PoetryRuntimeError) as e:
chooser.choose_for(package)
reason = f"Downloaded distributions for {package.name} ({package.version}) did not match any known checksums in your lock file."
assert str(e.value) == reason
text = e.value.get_text(debug=True, strip=True)
assert reason in text
# Verify both mismatched hashes are mentioned in the error
assert files[0]["hash"] in text
assert files[1]["hash"] in text

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/error-handling Bad error messages/insufficient error handling area/ux Features and improvements related to the user experience
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Poetry cannot install Numpy 2.2.1
1 participant