Skip to content

[#482] Migrated exceptions to use Mink exception classes. #522

Merged
AlexSkrypnyk merged 2 commits intomainfrom
feature/482-exceptions-from-mink
Jan 22, 2026
Merged

[#482] Migrated exceptions to use Mink exception classes. #522
AlexSkrypnyk merged 2 commits intomainfrom
feature/482-exceptions-from-mink

Conversation

@AlexSkrypnyk
Copy link
Member

@AlexSkrypnyk AlexSkrypnyk commented Jan 22, 2026

Closes #482

Summary by CodeRabbit

  • Documentation
    • Reflowed README for readability; expanded error-handling section with a structured table and examples.
  • Bug Fixes
    • Standardized assertion and error reporting to improve consistency and clarity of failure messages across the framework.
  • Tests
    • Updated feature tests and expectations to match refined error wording and exception semantics.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link

coderabbitai bot commented Jan 22, 2026

Walkthrough

Replaces generic exception throws across many traits and tests with Behat/Mink-specific exceptions (ExpectationException, ElementNotFoundException, UnsupportedDriverActionException), adds imports and session driver context to thrown exceptions, and updates feature expectations and CLI bootstrap to match new messages. README formatting only.

Changes

Cohort / File(s) Summary
Documentation
README.md
Reflowed formatting, replaced exception bullets with a Mink-based exception table, adjusted migration prompt and scaffold footer; presentation-only edits.
Core element & assertion traits
src/ElementTrait.php, src/FieldTrait.php, src/LinkTrait.php, src/PathTrait.php, src/ResponseTrait.php, src/XmlTrait.php, src/JavascriptTrait.php, src/KeyboardTrait.php
Replaced generic \Exception/RuntimeException throws with Behat\Mink\Exception\ExpectationException and/or Behat\Mink\Exception\ElementNotFoundException; added imports and passed session driver where applicable.
Cookie & file-related traits
src/CookieTrait.php, src/FileDownloadTrait.php, src/FileTrait.php
Swapped generic exceptions for Mink-specific exceptions and UnsupportedDriverActionException in cookie retrieval; improved error typing for file/link/download assertions.
Drupal-specific traits
src/Drupal/BlockTrait.php, src/Drupal/ContentBlockTrait.php, src/Drupal/EmailTrait.php, src/Drupal/FileTrait.php, src/Drupal/TaxonomyTrait.php, src/Drupal/UserTrait.php, src/Drupal/WatchdogTrait.php
Replaced generic exception throws with ExpectationException in assertion/validation failure paths; added imports and included session driver context.
Tests & features
tests/behat/bootstrap/BehatCliTrait.php, tests/behat/features/*.feature
Broadened CLI assertion to accept Mink exception types; updated many .feature expectations and scenario texts to match new exception classes and revised failure messages (e.g., "not found" phrasing, explicit exception types).

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~50 minutes

Possibly related PRs

  • #475 — updates CookieTrait exception paths and unsupported-driver handling; closely related to cookie exception changes here.
  • #476 — modifies LinkTrait behavior and href validation; overlaps with link assertion/message updates in this PR.
  • #483 — adjusts FieldTrait exception classes for missing/invalid field scenarios, matching field-related changes in this PR.

Suggested labels

PR: Requires more work

Poem

🐰 I hopped through traits from top to root,
Swapped vague throws for Mink’s tidy suit,
Errors trimmed to tidy, clearer sound,
Tests now match the messages found,
A carrot-cheer for bugs unbound! 🥕

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: migration of generic exceptions to Mink-specific exception classes throughout the codebase.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings

Comment @coderabbitai help to get the list of available commands and usage tips.

@AlexSkrypnyk AlexSkrypnyk force-pushed the feature/482-exceptions-from-mink branch 2 times, most recently from 5ae3f74 to 6c333ef Compare January 22, 2026 07:21
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (2)
src/Drupal/TaxonomyTrait.php (2)

137-154: Consider using ExpectationException for the vocabulary check at line 141.

This method is a "Then" assertion step (taxonomyAssertTermExistsByName). While the vocabulary not existing is a prerequisite failure, users running this step would expect a Mink-style ExpectationException for consistency with other assertion methods in this PR.

Currently, if the vocabulary doesn't exist, a RuntimeException is thrown (line 141), but if the term doesn't exist, an ExpectationException is thrown (line 152). This inconsistency may confuse users debugging test failures.

♻️ Optional: Consistent exception type
     if (!$vocab) {
-      throw new \RuntimeException(sprintf('The vocabulary "%s" does not exist.', $vocabulary_machine_name));
+      throw new ExpectationException(sprintf('The vocabulary "%s" does not exist.', $vocabulary_machine_name), $this->getSession()->getDriver());
     }

165-182: Same consideration for consistency at line 169.

Similar to taxonomyAssertTermExistsByName, this "Then" assertion method uses RuntimeException for the vocabulary check but ExpectationException for the term existence check.

♻️ Optional: Consistent exception type
     if (!$vocab) {
-      throw new \RuntimeException(sprintf('The vocabulary "%s" does not exist.', $vocabulary_machine_name));
+      throw new ExpectationException(sprintf('The vocabulary "%s" does not exist.', $vocabulary_machine_name), $this->getSession()->getDriver());
     }
🤖 Fix all issues with AI agents
In `@README.md`:
- Around line 294-314: The fenced code block in the "Exceptions" section
currently uses ``` with no language and triggers markdownlint MD040; update the
block delimiter to include a language identifier (e.g., change ``` to ```text)
so the example error messages (e.g., "Element matching css \"#my-element\" not
found.") are fenced as ```text to satisfy the linter.

In `@src/JavascriptTrait.php`:
- Around line 16-17: Update the docblocks in JavascriptTrait.php to reflect the
actual exception type being thrown: replace any occurrences of "@throws
\Exception" with "@throws \Behat\Mink\Exception\ExpectationException" (or the
fully-qualified Exception used) for the methods in this file (including the
block around lines 282-313) so the `@throws` accurately matches the thrown
ExpectationException referenced by the code.

Comment on lines 294 to +314
### Exceptions

- `\Exception` is thrown when an assertion fails.
- `\RuntimeException` is thrown when a step does not have enough contextual
information to perform an action.
This library uses [Mink exception classes](https://mink.behat.org/en/latest/)
for
consistent error handling:

| Exception | When thrown |
|------------------------------------|------------------------------------------------------|
| `ElementNotFoundException` | Element, field, link, or selector not found on page |
| `ExpectationException` | Assertion fails (value mismatch, state verification) |
| `UnsupportedDriverActionException` | Feature requires specific driver (e.g., Selenium) |
| `\RuntimeException` | Invalid input or processing error (not an assertion) |

Example error messages:

```
Element matching css "#my-element" not found.
Link with title "My Link" not found.
Select with id|name|label "My Select" not found.
The cookie with name "session" was not set.
```
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

Add a language identifier to the fenced code block.
This resolves markdownlint MD040.

🛠️ Suggested fix
-```
+```text
 Element matching css "#my-element" not found.
 Link with title "My Link" not found.
 Select with id|name|label "My Select" not found.
 The cookie with name "session" was not set.
</details>

<details>
<summary>🧰 Tools</summary>

<details>
<summary>🪛 markdownlint-cli2 (0.18.1)</summary>

309-309: Fenced code blocks should have a language specified

(MD040, fenced-code-language)

</details>

</details>

<details>
<summary>🤖 Prompt for AI Agents</summary>

In @README.md around lines 294 - 314, The fenced code block in the "Exceptions"
section currently uses with no language and triggers markdownlint MD040; update the block delimiter to include a language identifier (e.g., change to

\"#my-element\" not found.") are fenced as ```text to satisfy the linter.

Comment on lines +16 to 17
use Behat\Mink\Exception\ExpectationException;

Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

Update the @throws docblock to match the current exception type.
The docblock still says @throws \Exception, which no longer reflects the thrown exception.

Also applies to: 282-313

🤖 Prompt for AI Agents
In `@src/JavascriptTrait.php` around lines 16 - 17, Update the docblocks in
JavascriptTrait.php to reflect the actual exception type being thrown: replace
any occurrences of "@throws \Exception" with "@throws
\Behat\Mink\Exception\ExpectationException" (or the fully-qualified Exception
used) for the methods in this file (including the block around lines 282-313) so
the `@throws` accurately matches the thrown ExpectationException referenced by the
code.

@AlexSkrypnyk AlexSkrypnyk force-pushed the feature/482-exceptions-from-mink branch from 6c333ef to b0bcec4 Compare January 22, 2026 07:39
@AlexSkrypnyk AlexSkrypnyk force-pushed the feature/482-exceptions-from-mink branch from b0bcec4 to 475f623 Compare January 22, 2026 07:51
@codecov
Copy link

codecov bot commented Jan 22, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (c26b6c0) to head (475f623).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #522   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           34        34           
  Lines         2364      2351   -13     
=========================================
- Hits          2364      2351   -13     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@AlexSkrypnyk AlexSkrypnyk merged commit b47c251 into main Jan 22, 2026
5 checks passed
@AlexSkrypnyk AlexSkrypnyk deleted the feature/482-exceptions-from-mink branch January 22, 2026 08:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use exceptions from Mink

1 participant