Skip to content

Fix/req.on fail() are not persisted issue - #8873

Merged
vijayh-bruno merged 11 commits into
usebruno:mainfrom
sharan-bruno:fix/req.onFail()_are_not_persisted_issue
Aug 24, 2026
Merged

vijayh-bruno merged 11 commits into
usebruno:mainfrom
sharan-bruno:fix/req.onFail()_are_not_persisted_issue

Conversation

@sharan-bruno

@sharan-bruno sharan-bruno commented Aug 4, 2026 •

Copy link
Copy Markdown
Contributor

BRU-3948

Description

Worked on top of #8575. Verified the implementation there — it works as expected and the changes look good. Only the E2E coverage was missing, so this PR adds it.

Problem

When a request fails at the connection level (wrong URL, unreachable host, etc.), the req.onFail handler did run, but the variables it set were never applied — they were computed and dropped. #8575 fixed that, but there were no E2E tests guarding the behaviour, so a regression would go unnoticed.

Fix

The fix in #8575 propagates the handler's result through:

await executeRequestOnFailHandler(request, error, (onFailScriptResult) => {
  sendVariableUpdates(onFailScriptResult, { collectionUid, requestUid, collection });
});

This PR adds an E2E test for exactly that path: a fixture collection whose request points at an unreachable port, with a pre-request script that seeds var / envVar / globalEnvVar in the main body and overwrites them inside the handler. After sending, the Variables tab is checked to confirm the runtime and environment values were actually updated. Two supporting changes were needed to assert on them — VariablesEditor had no data-testids, so its sections and rows are now addressable, and a shared openVariablesTab action was added to the page helpers.

Screenshots

Before After
image image

Contribution Checklist:

  • I've used AI significantly to create this pull request
  • The pull request only addresses one issue or adds one feature.
  • The pull request does not introduce any breaking changes
  • I have added screenshots or gifs to help explain the change if applicable.
  • I have read the contribution guidelines.
  • Create an issue and link to the pull request.
  • I've run the claude code review skill locally.

Note: Keeping the PR small and focused helps make it easier to review and merge. If you have multiple changes you want to make, please consider submitting them as separate pull requests.

Publishing to New Package Managers

Please see here for more information.

Summary by CodeRabbit

  • Bug Fixes

    • Fixed onFail handlers so variable updates persist after failed requests.
    • Preserved runtime, environment, and global variable updates across desktop, CLI, and collection runs.
    • Ensured asynchronous onFail results are correctly returned and applied.
  • Tests

    • Added coverage for failed-request variable persistence.
    • Added end-to-end validation for environment and global variable updates.
    • Improved automated access to the Variables view and its values.

@coderabbitai

coderabbitai Bot commented Aug 4, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

Walkthrough

req.onFail handlers now return variable updates after request failures. Electron and CLI execution paths apply those updates. Runtime, integration, and end-to-end tests verify runtime, environment, and global environment persistence.

Changes

req.onFail variable propagation

Layer / File(s) Summary
Return script results from onFail handlers
packages/bruno-js/src/runtime/script-runtime.js, packages/bruno-js/tests/runtime.spec.js
NodeVM and QuickJS wrap request.onFailHandler and return updated variable values.
Forward results through Electron network execution
packages/bruno-electron/src/ipc/network/index.js, packages/bruno-electron/tests/network/execute-request-error-handler.spec.js
Request and collection-run error paths pass returned onFail results to sendVariableUpdates.
Apply results in CLI execution
packages/bruno-cli/src/runner/run-single-request.js, packages/bruno-cli/tests/integration/helpers/tmp-dir.js, packages/bruno-cli/tests/integration/run-onfail-persistence.spec.js, packages/bruno-tests/workspaces/onfail/*
CLI request failures run the onFail handler, synchronize returned updates, and verify persistence in isolated workspace fixtures.
Validate persistence and expose variable locators
tests/scripting/req-api/onfail/*, packages/bruno-app/src/components/VariablesEditor/index.js, tests/utils/page/*
Adds request fixtures, test initialization data, stable variable test IDs, Playwright helpers, and end-to-end persistence coverage.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Sequence Diagram(s)

sequenceDiagram
  participant RequestExecution
  participant ScriptRuntime
  participant executeRequestOnFailHandler
  participant sendVariableUpdates
  RequestExecution->>ScriptRuntime: Run request onFail handler
  ScriptRuntime-->>executeRequestOnFailHandler: Return script result
  executeRequestOnFailHandler->>sendVariableUpdates: Forward variable updates
  sendVariableUpdates-->>RequestExecution: Complete failure handling
Loading

Possibly related PRs

  • usebruno/bruno#8575: Implements the same req.onFail variable-result propagation across runtime, Electron, CLI, and tests.
  • usebruno/bruno#8620: Modifies CLI variable-update persistence, including global environment updates.
  • usebruno/bruno#8830: Modifies runtime-variable update propagation in Electron network execution.

Suggested reviewers: bijin-bruno, vijayh-bruno, helloanoop

Poem

A request fails; the script replies,
Runtime values change their ties.
Electron forwards updated state,
CLI persists the result.
Tests verify each variable.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title identifies the main change: fixing persistence of variables updated by req.onFail handlers.
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 3

🧹 Nitpick comments (1)
tests/utils/page/actions.ts (1)

1065-1071: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Store the locators in named variables.

Create named locators for the More Actions button, Variables menu item, and Variables tab before test.step. This improves page-module readability and follows the E2E locator convention.

Proposed refactor
 const openVariablesTab = async (page: Page) => {
+  const moreActionsButton = page.getByTestId('more-actions');
+  const variablesMenuItem = page.getByTestId('more-actions-variables');
+  const variablesTab = page
+    .locator('.request-tab')
+    .filter({ has: page.getByText('Variables', { exact: true }) });
+
   await test.step('Open the Variables tab', async () => {
-    await page.getByTestId('more-actions').click();
-    await page.getByTestId('more-actions-variables').click();
-    await expect(
-      page.locator('.request-tab').filter({ has: page.getByText('Variables', { exact: true }) })
-    ).toBeVisible();
+    await moreActionsButton.click();
+    await variablesMenuItem.click();
+    await expect(variablesTab).toBeVisible();
   });
 };

As per path instructions, use locator variables for locators in E2E code.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/utils/page/actions.ts` around lines 1065 - 1071, Create named locators
in openVariablesTab for the More Actions button, Variables menu item, and
Variables tab before entering test.step, then use those variables for the clicks
and visibility assertion while preserving the existing behavior.

Source: Path instructions

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/bruno-electron/src/ipc/network/index.js`:
- Around line 1069-1071: The oauth2CredentialsToReset field from
onFailScriptResult is being discarded in both failure callback paths, preventing
OAuth2 credentials from being reset even when the onFail handler requests it. At
packages/bruno-electron/src/ipc/network/index.js#L1069-L1071, add a call to
resetOauth2Credentials with onFailScriptResult.oauth2CredentialsToReset after
the sendVariableUpdates call in the callback passed to
executeRequestOnFailHandler. At
packages/bruno-electron/src/ipc/network/index.js#L1948-L1950, apply the same
resetOauth2Credentials call with oauth2CredentialsToReset after
sendVariableUpdates in the collection-run failure callback.

In `@packages/bruno-js/tests/runtime.spec.js`:
- Around line 124-145: Extend the req.onFail variable-update test to also
instantiate ScriptRuntime with the QuickJS runtime, execute the same script,
invoke request.onFailHandler, and assert the updated runtime, environment, and
global environment variables. Preserve the existing NodeVM coverage while
ensuring the QuickJS wrapper returns all three updates.

In `@tests/scripting/req-api/onfail/fixtures/collection/onFail.bru`:
- Line 8: Update the request URL fixture used by the onFail test to target a
test-owned, per-test endpoint with deterministic failure behavior instead of the
shared port 9999. Ensure the endpoint remains unreachable or otherwise
guaranteed to fail, so req.onFail always executes safely during parallel test
runs.

---

Nitpick comments:
In `@tests/utils/page/actions.ts`:
- Around line 1065-1071: Create named locators in openVariablesTab for the More
Actions button, Variables menu item, and Variables tab before entering
test.step, then use those variables for the clicks and visibility assertion
while preserving the existing behavior.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 4bed7db0-2ae6-4d2e-8364-9ee028aa8eb1

📥 Commits

Reviewing files that changed from the base of the PR and between 6090912 and b9fe745.

📒 Files selected for processing (12)
  • packages/bruno-app/src/components/VariablesEditor/index.js
  • packages/bruno-electron/src/ipc/network/index.js
  • packages/bruno-electron/tests/network/execute-request-error-handler.spec.js
  • packages/bruno-js/src/runtime/script-runtime.js
  • packages/bruno-js/tests/runtime.spec.js
  • tests/scripting/req-api/onfail/fixtures/collection/bruno.json
  • tests/scripting/req-api/onfail/fixtures/collection/environments/Test.bru
  • tests/scripting/req-api/onfail/fixtures/collection/onFail.bru
  • tests/scripting/req-api/onfail/init-user-data/collection-security.json
  • tests/scripting/req-api/onfail/init-user-data/preferences.json
  • tests/scripting/req-api/onfail/req-onfail-env-variable-persistent.spec.ts
  • tests/utils/page/actions.ts

Comment thread packages/bruno-js/tests/runtime.spec.js Resolved
@coderabbitai

coderabbitai Bot commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Note

GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.

@coderabbitai coderabbitai Bot mentioned this pull request Aug 5, 2026
2 of 4 tasks

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@tests/scripting/req-api/onfail/fixtures/collection/onFail.yml`:
- Line 8: Update the failing request URL in the onFail fixture to use a
test-owned or runner-controlled endpoint rather than the fixed port 9999. Ensure
the endpoint is guaranteed to refuse or fail the connection during the test so
req.onFail is invoked deterministically, while preserving the existing request
path and failure scenario.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: b178f5d6-445c-4565-a538-091f5439315f

📥 Commits

Reviewing files that changed from the base of the PR and between a92ea79 and 3928081.

📒 Files selected for processing (8)
  • packages/bruno-app/src/components/VariablesEditor/index.js
  • packages/bruno-js/src/runtime/script-runtime.js
  • tests/scripting/req-api/onfail/fixtures/collection/environments/Test.yml
  • tests/scripting/req-api/onfail/fixtures/collection/onFail.yml
  • tests/scripting/req-api/onfail/fixtures/collection/opencollection.yml
  • tests/scripting/req-api/onfail/init-user-data/global-environments.json
  • tests/scripting/req-api/onfail/req-onfail-env-variable-persistent.spec.ts
  • tests/utils/page/locators.ts
🚧 Files skipped from review as they are similar to previous changes (2)
  • packages/bruno-js/src/runtime/script-runtime.js
  • packages/bruno-app/src/components/VariablesEditor/index.js

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
tests/scripting/req-api/onfail/req-onfail-env-variable-persistent.spec.ts (1)

7-12: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Keep E2E fixtures under the test fixture directory.

FIXTURE_DIR points to packages/bruno-tests/workspaces/onfail, not tests/scripting/req-api/onfail/fixtures. Move the workspace fixture under the test's fixtures directory and update the path reference.

As per path instructions, fixtures used by tests/** should be nested under the matching fixtures directory.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/scripting/req-api/onfail/req-onfail-env-variable-persistent.spec.ts`
around lines 7 - 12, Move the onfail workspace fixture from
packages/bruno-tests/workspaces/onfail into
tests/scripting/req-api/onfail/fixtures, then update FIXTURE_DIR to reference
the relocated fixture while preserving the existing workspaceFixturePath setup.

Source: Path instructions

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/bruno-tests/workspaces/onfail/onfail-collection/onFail.yml`:
- Around line 6-8: Replace the fixed port in the onFail test setup with a
dynamically allocated loopback port by starting a test-owned server via
server.listen(0, '127.0.0.1'), then inject the assigned port into the request
URL in onFail.yml while preserving the intentionally unreachable-path behavior.

---

Nitpick comments:
In `@tests/scripting/req-api/onfail/req-onfail-env-variable-persistent.spec.ts`:
- Around line 7-12: Move the onfail workspace fixture from
packages/bruno-tests/workspaces/onfail into
tests/scripting/req-api/onfail/fixtures, then update FIXTURE_DIR to reference
the relocated fixture while preserving the existing workspaceFixturePath setup.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: c779d6b7-7cf2-4d03-a93e-2b6fc6e0fda1

📥 Commits

Reviewing files that changed from the base of the PR and between 3928081 and b86cc1c.

📒 Files selected for processing (11)
  • packages/bruno-cli/src/runner/run-single-request.js
  • packages/bruno-cli/tests/integration/run-onfail-persistence.spec.js
  • packages/bruno-tests/workspaces/onfail/environments/Global.yml
  • packages/bruno-tests/workspaces/onfail/onfail-collection/bruno.json
  • packages/bruno-tests/workspaces/onfail/onfail-collection/environments/Test.yml
  • packages/bruno-tests/workspaces/onfail/onfail-collection/onFail.yml
  • packages/bruno-tests/workspaces/onfail/onfail-collection/opencollection.yml
  • packages/bruno-tests/workspaces/onfail/workspace.yml
  • tests/scripting/req-api/onfail/init-user-data/collection-security.json
  • tests/scripting/req-api/onfail/init-user-data/preferences.json
  • tests/scripting/req-api/onfail/req-onfail-env-variable-persistent.spec.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • tests/scripting/req-api/onfail/init-user-data/collection-security.json

Comment thread tests/utils/page/actions.ts Resolved

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 3

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/bruno-cli/tests/integration/helpers/tmp-dir.js`:
- Around line 19-21: Update the temporary-directory setup helper around
createTmpDir and fs.cpSync to catch copy failures, remove tmpDir synchronously,
and rethrow the original error. Preserve the existing successful return path and
ensure cleanup occurs before the error propagates.

In `@packages/bruno-cli/tests/integration/run-onfail-persistence.spec.js`:
- Around line 37-40: Capture the result returned by runCli in the run-onfail
persistence test and assert its exit code matches the documented status for the
handled connection failure before validating persisted files. Make the assertion
clearly identify the CLI execution behavior under test.

In
`@tests/scripting/req-api/onfail/fixtures/workspace/onfail-collection/onFail.yml`:
- Around line 6-8: Update the onFail fixture’s request URL to use a
deterministic, test-controlled failure endpoint rather than the hardcoded
127.0.0.1:59999 address. Ensure the endpoint is explicitly unreachable or served
by a disposable test server under harness control so the request consistently
exercises req.onFail.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 3659dad3-2081-42bd-a93d-ba3ec084d884

📥 Commits

Reviewing files that changed from the base of the PR and between b86cc1c and 9c988e0.

📒 Files selected for processing (13)
  • packages/bruno-cli/src/runner/run-single-request.js
  • packages/bruno-cli/tests/integration/helpers/tmp-dir.js
  • packages/bruno-cli/tests/integration/run-onfail-persistence.spec.js
  • tests/scripting/req-api/onfail/fixtures/workspace/environments/Global.yml
  • tests/scripting/req-api/onfail/fixtures/workspace/onfail-collection/bruno.json
  • tests/scripting/req-api/onfail/fixtures/workspace/onfail-collection/environments/Test.yml
  • tests/scripting/req-api/onfail/fixtures/workspace/onfail-collection/onFail.yml
  • tests/scripting/req-api/onfail/fixtures/workspace/onfail-collection/opencollection.yml
  • tests/scripting/req-api/onfail/fixtures/workspace/workspace.yml
  • tests/scripting/req-api/onfail/req-onfail-env-variable-persistent.spec.ts
  • tests/utils/page/actions.ts
  • tests/utils/page/collection/collection-header.ts
  • tests/utils/page/locators.ts
🚧 Files skipped from review as they are similar to previous changes (3)
  • packages/bruno-cli/src/runner/run-single-request.js
  • tests/utils/page/actions.ts
  • tests/utils/page/locators.ts

sanish-bruno
sanish-bruno previously approved these changes Aug 11, 2026
…e runCli results in onFail persistence tests
@vijayh-bruno
vijayh-bruno merged commit 203a622 into usebruno:main Aug 24, 2026
14 of 15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants