Skip to content

fix(scripting): preserve variables set in req.onFail - #8575

Merged
vijayh-bruno merged 3 commits into
usebruno:fix/persist-on-fail-variable-updatesfrom
Max-Reisinger:bugfix/persist-on-fail-variable-updates
Jul 27, 2026
Merged

vijayh-bruno merged 3 commits into
usebruno:fix/persist-on-fail-variable-updatesfrom
Max-Reisinger:bugfix/persist-on-fail-variable-updates

Conversation

@Max-Reisinger

@Max-Reisinger Max-Reisinger commented Jul 12, 2026 •

Copy link
Copy Markdown
Contributor

Description

Variables updated with bru.setVar(), bru.setEnvVar(), or bru.setGlobalEnvVar() inside req.onFail() were updated in the script runtime but were not forwarded to the renderer after the handler completed.

Fixes #8569.

This change:

  • returns the latest script variable result after req.onFail() runs
  • forwards those updates through the existing variable update channels
  • applies the same behavior to individual requests and collection runs
  • adds regression tests for the handler result and variable updates

Validation

  • node --experimental-vm-modules ../../node_modules/jest/bin/jest.js tests/runtime.spec.js --runInBand (34 passed)
  • HOME=/tmp/bruno-jest-home npm run test --workspace=packages/bruno-electron -- tests/network/execute-request-error-handler.spec.js -t "should return the result" --runInBand
  • npm run lint -- packages/bruno-js/src/runtime/script-runtime.js packages/bruno-js/tests/runtime.spec.js packages/bruno-electron/src/ipc/network/index.js packages/bruno-electron/tests/network/execute-request-error-handler.spec.js

The full Electron test file contains existing environment-dependent DNS and timeout assertions. In this environment, one assertion receives EPERM in the sandbox and the DNS assertion receives a TLS disconnect outside it; the focused regression test passes.

Summary by CodeRabbit

  • Bug Fixes
    • Preserve and propagate variable changes made inside request failure (onFail) handlers, including updates to runtime and environment variables.
    • When a failure handler returns a value, that result is now used consistently to forward variable updates, improving correctness across request and collection/folder failure scenarios.
    • Failure scripts now produce consistent result payloads across supported execution modes.
  • Tests
    • Added and updated tests to verify onFail handler return values and that variable updates are correctly returned and applied.

@coderabbitai

coderabbitai Bot commented Jul 12, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

Walkthrough

Request onFailHandler results now include script variable mutations. HTTP and runner failure paths propagate truthy results through sendVariableUpdates, with tests covering returned handler values and runtime, environment, and global variable updates.

Changes

onFail variable propagation

Layer / File(s) Summary
Wrap script failure handlers
packages/bruno-js/src/runtime/script-runtime.js, packages/bruno-js/tests/runtime.spec.js
Failure handlers are wrapped across NodeVM and QuickJS execution, returning script results containing updated variables; tests verify runtime, environment, and global variable changes.
Forward failure results
packages/bruno-electron/src/ipc/network/index.js, packages/bruno-electron/tests/network/execute-request-error-handler.spec.js
The failure-handler helper returns its result, and HTTP and runner failure paths forward truthy results through sendVariableUpdates; tests verify the returned handler value and callback result.

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

Sequence Diagram(s)

sequenceDiagram
  participant ScriptRuntime
  participant NetworkFailurePath
  participant executeRequestOnFailHandler
  participant sendVariableUpdates
  ScriptRuntime->>NetworkFailurePath: expose request failure result
  NetworkFailurePath->>executeRequestOnFailHandler: handle failure
  executeRequestOnFailHandler-->>NetworkFailurePath: return script result
  NetworkFailurePath->>sendVariableUpdates: forward variable mutations
Loading

Possibly related PRs

  • usebruno/bruno#7040: Propagates non-cancel failure results and partial results through Electron network failure paths.
  • usebruno/bruno#8406: Updates variable propagation and collection-root writeback during request failure handling.

Suggested reviewers: helloanoop, lohit-bruno, naman-bruno, sid-bruno

Poem

When requests stumble, values remain,
OnFail carries state through every chain.
Runtime, env, and globals align,
Renderer receives the changed design.
A failed request still leaves things fine.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Linked Issues check ✅ Passed The changes address #8569 by preserving and forwarding req.onFail variable updates across request and collection runs.
Out of Scope Changes check ✅ Passed The modified runtime, network, and test changes all support the req.onFail variable-preservation fix.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main fix: preserving variables set inside req.onFail.
✨ Finishing Touches
🧪 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: 2

🤖 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/tests/network/execute-request-error-handler.spec.js`:
- Around line 40-54: Add IPC-level regression tests for the registered
network-failure paths, covering both standalone and runner requests rather than
only executeRequestOnFailHandler. Invoke each failure path and assert that the
renderer receives runtime, environment, and global-environment update events
with the expected data.

In `@packages/bruno-js/tests/runtime.spec.js`:
- Around line 124-145: Add QuickJS coverage to the “should return variable
updates made in req.onFail” test by running the same script and handler-result
assertions with a ScriptRuntime configured as “quickjs”, while preserving the
existing NodeVM coverage.
🪄 Autofix (Beta)

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

Run ID: 03fdb565-b1c1-4b1d-8e8e-0c2e135f016f

📥 Commits

Reviewing files that changed from the base of the PR and between 5346a72 and ad43430.

📒 Files selected for processing (4)
  • 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

Comment thread packages/bruno-js/tests/runtime.spec.js Resolved
@sanish-bruno
sanish-bruno changed the base branch from main to fix/persist-on-fail-variable-updates July 27, 2026 09:42
@vijayh-bruno
vijayh-bruno merged commit be7e2ed into usebruno:fix/persist-on-fail-variable-updates Jul 27, 2026
14 of 16 checks passed
@sreelakshmi-bruno

Copy link
Copy Markdown
Collaborator

Hi, this PR is now merged to a feature branch which will be reviewed and merged to main. This is planned to be released in v4.2.0, which is planned to go out in September. We'll update here when a nightly build is available. Thanks for your contribution!

@Max-Reisinger

Copy link
Copy Markdown
Contributor Author

Thanks for the update and for getting this merged! I appreciate the review and the release timeline. I’ll be happy to verify the fix once a nightly build is available.

sharan-bruno pushed a commit to sharan-bruno/bruno that referenced this pull request Aug 5, 2026
* fix(scripting): preserve variables set in onFail

* test(scripting): cover onFail variable propagation

---------

Co-authored-by: Elon <[email protected]>
sharan-bruno pushed a commit to sharan-bruno/bruno that referenced this pull request Aug 19, 2026
* fix(scripting): preserve variables set in onFail

* test(scripting): cover onFail variable propagation

---------

Co-authored-by: Elon <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Variables set in req.onFail() are lost

4 participants