Skip to content

Conversation

@KaranPradhan266
Copy link

@KaranPradhan266 KaranPradhan266 commented Dec 1, 2025

and CLI

Description

fixes #6258

Contribution Checklist:

  • 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.

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

    • Script errors in pre-request, post-response, and test phases now produce clear failure entries (e.g., "Pre-Request Script Error", "Post-Response Script Error", "Test Script Error") and are consistently recorded.
    • Partial results from failed scripts are preserved and merged; next-request and stop-execution indicators are respected.
    • Skipped pre-request paths return a clear "skipped" status and preserve test results.
  • Refactor

    • Centralized, consistent script-result construction and error handling across runtimes.

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

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Dec 1, 2025

Walkthrough

Centralizes and standardizes script error handling: runtimes return partial results on throw, CLI runner catches and merges those partial results (including skipped pre-request), and Electron IPC normalizes and appends descriptive failure entries for pre-request, post-response, and test script errors.

Changes

Cohort / File(s) Summary
CLI runner: single-request error & skip handling
packages/bruno-cli/src/runner/run-single-request.js
Wraps pre-request/post-response/test execution in try/catch; on error merges error.partialResults.results into existing results, appends a descriptive failure entry (e.g., "Pre-Request Script Error"), preserves nextRequestName and stopExecution from partial results, logs updated results. Converts pre-request skip path to return a skipped response with status: "skipped" and statusText: "request skipped via pre-request script", retaining preRequestTestResults.
Electron IPC: centralized script-error normalization
packages/bruno-electron/src/ipc/network/index.js
Adds internal helper appendScriptErrorResult inside registerNetworkIpc to adopt error.partialResults, append a normalized failure entry labeled per script phase, and return a consistent scriptResult object. Helper is invoked for pre-request, post-response, and test script errors across single-request and folder-run flows.
Script runtimes: error-safe partial results
packages/bruno-js/src/runtime/script-runtime.js
Introduces buildRequestScriptResult / buildResponseScriptResult builders and wraps runtime execution (nodevm / QuickJS) in try/catch. On error attaches scriptError and partialResults to the thrown error; on success returns the standardized result. Consolidates return paths and ensures partial results are available to callers on exceptions.

Sequence Diagram(s)

sequenceDiagram
    autonumber
    participant CLI as CLI runner
    participant IPC as Electron IPC
    participant RT as Script Runtime
    participant Runner as Folder/Request Runner

    CLI->>RT: run pre-request script
    alt success
        RT-->>CLI: preRequestResult
        CLI->>Runner: continue request execution
    else error
        RT-->>CLI: throw error (includes partialResults, nextRequestName, stopExecution)
        CLI->>IPC: send partialResults + error info
        IPC->>IPC: appendScriptErrorResult(phase="Pre-Request")
        IPC-->>Runner: normalized scriptResult (includes failure entry)
    end

    Runner->>RT: run post-response/test scripts
    alt success
        RT-->>Runner: scriptResult
    else error
        RT-->>Runner: throw error (includes partialResults, nextRequestName, stopExecution)
        Runner->>IPC: normalize via appendScriptErrorResult
        IPC-->>Runner: normalized scriptResult with failure entry
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

  • Verify safe handling of missing error.message and undefined error.partialResults.
  • Check that nextRequestName and stopExecution propagation preserves runner control flow across folder-run and single-request paths.
  • Confirm builders (buildRequestScriptResult / buildResponseScriptResult) produce shapes expected by CLI and IPC consumers and avoid duplicating failure entries.

Suggested reviewers

  • helloanoop
  • bijin-bruno
  • lohit-bruno

Poem

Pre-scripts fell and left a trace,
Partial truths now find their place.
Failures named, the flow intact —
Errors logged and context stacked. ✨

Pre-merge checks and finishing touches

✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Mark test script errors as failed in runner' clearly and specifically describes the main change—ensuring script errors are treated as failures in the runner.
Linked Issues check ✅ Passed All coding requirements from issue #6258 are met: Pre-Request, Post-Response, and Test script errors are now caught and marked as failures in both Electron and CLI runners.
Out of Scope Changes check ✅ Passed All changes are directly scoped to fixing script error handling in the runner—try-catch wrapping, error result normalization, and partial result preservation remain aligned with issue #6258.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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

Copy link
Contributor

@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: 0

🧹 Nitpick comments (1)
packages/bruno-cli/src/runner/run-single-request.js (1)

687-705: CLI test error handling correctly surfaces script errors

The new catch block correctly:

  • Preserves any error.partialResults.results.
  • Appends a synthetic "Test Script Error" failure.
  • Propagates nextRequestName from error.partialResults.
  • Reuses logResults so the CLI output reflects the failure.

This aligns well with the goal of marking script errors as failing tests, while keeping chaining semantics intact.

If you find this pattern spreading further, consider extracting the “append test-script error entry” logic into a small shared helper (similar to the Electron helper) to keep behavior consistent across environments, but that’s optional.

📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between dd72ee5 and ae2d2dc.

📒 Files selected for processing (2)
  • packages/bruno-cli/src/runner/run-single-request.js (1 hunks)
  • packages/bruno-electron/src/ipc/network/index.js (3 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
packages/bruno-cli/src/runner/run-single-request.js (1)
packages/bruno-cli/src/commands/run.js (1)
  • nextRequestName (627-627)
🔇 Additional comments (3)
packages/bruno-electron/src/ipc/network/index.js (3)

403-421: Helper cleanly normalizes test results on script errors

appendTestScriptErrorResult is a good abstraction here: it keeps existing testResults shape (including env and nextRequestName) and guarantees a synthetic "Test Script Error" failure entry whenever an error is present, while staying a no‑op when error is falsy.

This gives you a single, consistent place to evolve test‑error behavior in the Electron path.


883-921: Standalone request path now reliably emits failing results on test script errors

Using appendTestScriptErrorResult here ensures that:

  • The renderer always receives at least one failing test entry when the test script throws.
  • Any partial results and environment/global-environment updates from partialResults are preserved.

This should fix the “tests appear passed despite script error” symptom for the single-request Electron path.


1534-1573: Folder/runner path correctly integrates test script errors into results and flow control

Applying appendTestScriptErrorResult in the folder runner:

  • Ensures testResults.results always contains a failing "Test Script Error" entry when a test script crashes.
  • Preserves nextRequestName from partial results, so jump-based control flow still works.
  • Keeps env/global env fields intact for downstream updates.

This aligns the runner’s behavior with the standalone request path and should prevent “Passed” statuses when the test script itself fails.

@Pragadesh44-Bruno
Copy link
Collaborator

@KaranPradhan266 Thanks for the PR contribution. However, if the tests are failing, it marks the request as failed. But if the post-response (which contains the tests) has syntactical issues, it shows the request as passed. We should definitely address this as well, right?

image

@KaranPradhan266
Copy link
Author

@Pragadesh44-Bruno Thanks for pointing this out!

I tested the scenario you described — adding a syntactically invalid
Post-Response Script (for example just }) — and I can confirm that
Bruno was still marking the request as “Passed” even though the UI showed a Post-Response Script Error.

After updating the PR, both syntax errors and runtime exceptions in
Post-Response Scripts now correctly mark the entire request as Failed
in both the Electron runner and the CLI runner.

Also, just to clarify: the unit test failures reported in CI are not
related to the changes in this PR. Additionally, Pre-Request Script
errors are already resulting in a failed test run, as expected.

I’ve attached screenshots below for your reference.
Please let me know if there’s anything else you’d like me to address.
If everything looks good, I’ll push the final commit shortly.

For Pre-request:
image

For Post-request:
image

@KaranPradhan266
Copy link
Author

@Pragadesh44-Bruno
Also, after updating the PR, both syntax errors and runtime exceptions in Post-Response Scripts now correctly mark the entire request as failed in both the Electron runner and the CLI runner. The new Electron helper appendPostResponseScriptErrorResult mirrors appendTestScriptErrorResult, and after I commit the updated changes I can also unify them into a single appendScriptErrorResult(type, results, error) utility for consistency or keep them separate if you prefer. Do let me know!

@KaranPradhan266
Copy link
Author

@Pragadesh44-Bruno Added a single appendScriptErrorResult helper and used it for pre-request, post-response, and test script errors. Do let me know if this works!
image

@Pragadesh44-Bruno
Copy link
Collaborator

@KaranPradhan266 Thanks for your changes to the PR. I'll be reviewing it today and will approve it

…l results for pre-request and post-response scripts across CLI and Electron. This ensures that tests passing before an error are still reported.
@Pragadesh44-Bruno
Copy link
Collaborator

@KaranPradhan266 I have created a PR in your branch that fixes several bugs in the error logging implementation.

  1. packages/bruno-js/src/runtime/script-runtime.js

    • Problem: When a script threw an error, any test() calls that passed before the error were lost.
    • Fix: Wrapped script execution in a try-catch block for both runRequestScript() and runResponseScript(). When an error occurs, partialResults are attached to the error before re-throwing.
  2. packages/bruno-electron/src/ipc/network/index.js

    • Problem: Post-response errors weren't extracting partialResults, causing passed tests to be lost in the Electron app.
    • Fix: Added partialResults extraction for post-response errors in two places (single-request and folder-run flows).
  3. packages/bruno-cli/src/runner/run-single-request.js

    • Problem: Pre-request scripts had no error handling, causing errors to bubble up and lose all partial results.
    • Fix: Added full try-catch error handling for pre-request scripts, mirroring the existing post-response pattern.

Before:
image

After:
image

feat: Enhance error handling in script execution
@pull-request-size pull-request-size bot added size/L and removed size/M labels Dec 3, 2025
Copy link
Contributor

@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

🧹 Nitpick comments (1)
packages/bruno-cli/src/runner/run-single-request.js (1)

221-246: Consider extracting a helper for error result handling.

The error handling pattern is repeated three times (pre-request, post-response, test). The Electron package uses appendScriptErrorResult for this. A similar helper here would reduce duplication:

const appendScriptErrorResult = (scriptType, partialResults, error) => {
  return [
    ...(partialResults || []),
    {
      status: 'fail',
      description: `${scriptType} Script Error`,
      error: error.message || `An error occurred while executing the ${scriptType.toLowerCase()} script.`
    }
  ];
};

This is optional but would improve maintainability if more script types are added.

Also applies to: 664-682, 731-747

📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 4e99929 and 0cdf703.

📒 Files selected for processing (3)
  • packages/bruno-cli/src/runner/run-single-request.js (3 hunks)
  • packages/bruno-electron/src/ipc/network/index.js (7 hunks)
  • packages/bruno-js/src/runtime/script-runtime.js (4 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/*.{js,jsx,ts,tsx}

📄 CodeRabbit inference engine (CODING_STANDARDS.md)

**/*.{js,jsx,ts,tsx}: Use 2 spaces for indentation, never tabs
Use single quotes for strings instead of double quotes
Always add semicolons at the end of statements
No trailing commas in code
Always use parentheses around parameters in arrow functions, even for single parameters
For multiline constructs, put opening braces on the same line with a minimum of 2 elements for multiline formatting
No newlines inside function parentheses
Space before and after the arrow in arrow functions: () => {}
No space between function name and parentheses: func() not func ()
Semicolons should go at the end of the line, not on a new line
Function names should be concise and descriptive
Add JSDoc comments to provide details to abstractions
Avoid single-line abstractions where all that is being done is increasing the call stack with one additional function
Add meaningful comments to explain complex code flow instead of obvious comments

Files:

  • packages/bruno-js/src/runtime/script-runtime.js
  • packages/bruno-electron/src/ipc/network/index.js
  • packages/bruno-cli/src/runner/run-single-request.js
🧠 Learnings (1)
📚 Learning: 2025-12-03T06:32:11.721Z
Learnt from: sanish-bruno
Repo: usebruno/bruno PR: 6090
File: packages/bruno-js/src/runtime/hooks-runtime.js:8-135
Timestamp: 2025-12-03T06:32:11.721Z
Learning: vm2 runtime is being removed from the Bruno application and QuickJS will become the default runtime for script execution.

Applied to files:

  • packages/bruno-js/src/runtime/script-runtime.js
🧬 Code graph analysis (3)
packages/bruno-js/src/runtime/script-runtime.js (4)
packages/bruno-js/src/sandbox/quickjs/utils/index.js (1)
  • error (50-50)
packages/bruno-electron/src/utils/oauth2.js (1)
  • error (77-77)
packages/bruno-js/src/sandbox/quickjs/index.js (2)
  • executeQuickJsVmAsync (92-190)
  • script (160-176)
packages/bruno-app/src/utils/codemirror/autocomplete.js (1)
  • context (365-365)
packages/bruno-electron/src/ipc/network/index.js (2)
packages/bruno-app/src/components/ResponsePane/ScriptError/index.js (2)
  • preRequestError (7-7)
  • postResponseError (8-8)
packages/bruno-cli/src/runner/run-single-request.js (1)
  • testResults (700-700)
packages/bruno-cli/src/runner/run-single-request.js (2)
packages/bruno-cli/src/commands/run.js (6)
  • result (552-563)
  • runtimeVariables (334-334)
  • collectionPath (295-295)
  • processEnvVars (466-468)
  • runSingleRequestByPathname (519-543)
  • nextRequestName (627-627)
packages/bruno-cli/src/runner/prepare-request.js (3)
  • decomment (5-5)
  • request (16-16)
  • collectionPath (18-18)
🔇 Additional comments (12)
packages/bruno-electron/src/ipc/network/index.js (7)

403-433: LGTM - Clean centralization of script error handling.

Good use of maps for script-type-specific descriptions and sensible fallbacks for unknown script types. The null-safe spreading ensures robustness when scriptResult is undefined.


693-697: LGTM - Proper partial results preservation.

Correctly extracts partial results before normalizing the error, ensuring any passing tests before the script failure are retained.


864-871: LGTM - Consistent error handling with helpful inline comment.

The comment at lines 864-866 clearly explains the rationale for preserving partial results.


946-947: LGTM - Consistent with existing test script error handling.

Correctly applies the normalized error result appending after the existing partial results extraction logic.


1291-1295: LGTM - Folder-run pre-request error handling mirrors single-request flow.


1519-1525: LGTM - Folder-run post-response error handling is consistent.


1613-1614: LGTM - Folder-run test script error handling is consistent.

packages/bruno-js/src/runtime/script-runtime.js (4)

70-82: LGTM - Clean extraction of result builder.

Consolidates the return object construction and avoids duplication across runtime branches.


84-127: LGTM - Consistent error handling across both runtimes.

The pattern of capturing the error, attaching partial results, and re-throwing ensures callers can display tests that passed before the script failed.


200-243: LGTM - Response script error handling mirrors request script pattern.

Both nodevm and quickjs paths consistently capture errors, attach partial results, and re-throw.


186-198: Verify persistentEnvVariables serialization consistency between builders.

buildRequestScriptResult (line 76) and buildResponseScriptResult (line 191) may handle persistentEnvVariables differently—one directly and one wrapped in cleanJson(). Confirm whether this difference is intentional or if both should use the same serialization approach for consistency.

packages/bruno-cli/src/runner/run-single-request.js (1)

173-246: LGTM - Comprehensive pre-request script error handling.

Properly handles the skip case with early return, and the error case correctly extracts partial results, appends a failure entry, and preserves flow control properties (nextRequestName, stopExecution).

Copy link
Contributor

@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: 0

🧹 Nitpick comments (1)
packages/bruno-cli/src/runner/run-single-request.js (1)

734-755: Test script error handling is consistent with other script paths; consider a small helper for DRYness

The test-script error handler mirrors the post-response handler: it merges error.partialResults.results, appends a 'Test Script Error' failure, and preserves nextRequestName and stopExecution. This is exactly what we want so that test script syntax/runtime errors are surfaced as failed tests and runner-level control flags are still honored.

Given the three handlers (pre-request, post-response, tests) now share the same “partialResults + synthetic failure + nextRequestName/stopExecution” pattern, you might later want to factor that into a tiny local helper (similar in spirit to appendScriptErrorResult on the Electron side) to keep behavior changes centralized. Not necessary for this PR, but it would reduce future drift.

📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 0cdf703 and 9abe6aa.

📒 Files selected for processing (1)
  • packages/bruno-cli/src/runner/run-single-request.js (3 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/*.{js,jsx,ts,tsx}

📄 CodeRabbit inference engine (CODING_STANDARDS.md)

**/*.{js,jsx,ts,tsx}: Use 2 spaces for indentation. No tabs, just spaces
Stick to single quotes for strings. For JSX/TSX attributes, use double quotes (e.g., )
Always add semicolons at the end of statements
No trailing commas
Always use parentheses around parameters in arrow functions, even for single params
For multiline constructs, put opening braces on the same line, and ensure consistency. Minimum 2 elements for multiline
No newlines inside function parentheses
Space before and after the arrow in arrow functions. () => {} is good
No space between function name and parentheses. func() not func ()
Semicolons go at the end of the line, not on a new line
Names for functions need to be concise and descriptive
Add in JSDoc comments to add more details to the abstractions if needed
Add in meaningful comments instead of obvious ones where complex code flow is explained properly

Files:

  • packages/bruno-cli/src/runner/run-single-request.js
🧬 Code graph analysis (1)
packages/bruno-cli/src/runner/run-single-request.js (1)
packages/bruno-cli/src/runner/prepare-request.js (2)
  • decomment (5-5)
  • request (16-16)
🔇 Additional comments (2)
packages/bruno-cli/src/runner/run-single-request.js (2)

665-685: Post-response script error handling now correctly preserves partial results and stopExecution

This catch block looks solid: it pulls error.partialResults.results into postResponseTestResults, appends a clear 'Post-Response Script Error' failure, and now also preserves both nextRequestName and stopExecution from partialResults, which was missing earlier. This aligns the CLI behavior with the Electron IPC path and respects bru.runner.stopExecution() even when the script later throws.


173-247: Pre-request error handling: avoid duplicate logging and verify behavior change

Nice job wiring partialResults, nextRequestName, and stopExecution through the pre-request path. A couple of details to tighten up:

  • In the error path you call logResults(preRequestTestResults, 'Pre-Request Tests') and this may be called again at line 618, so pre-request failures could be printed twice on the CLI when the script throws. Verify that this double logging doesn't occur.
  • Previously, a thrown pre-request script error would bubble to the outer try and short-circuit the request; now it's caught and the HTTP request proceeds with a synthetic failed test entry. Confirm that the higher-level runner treats any non-pass pre-request result as a failed request, so that the run is still marked as failed overall.

You can address the potential double logging and optionally log results in the skip path with something like:

-        if (result?.skipRequest) {
-          return {
+        if (result?.skipRequest) {
+          logResults(result?.results || [], 'Pre-Request Tests');
+          return {
@@
-        logResults(preRequestTestResults, 'Pre-Request Tests');
+        // Let the global pre-request logger handle printing these results

Verify that the CLI runner marks the overall request as failed when preRequestTestResults contains a failed entry.

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.

Runner with test marked as Passed whereas there is an error in the test script

3 participants