Skip to content

Conversation

@mondoreale
Copy link
Contributor

@mondoreale mondoreale commented Dec 25, 2025

This pull request updates the executeSafePromise utility to improve its error handling across different environments. The main enhancement is more robust detection of whether the code is running in Node/Electron or in a browser, and handling fatal promise rejections appropriately in each case.

Changes

Improvements to error handling and environment detection:

  • Updated the documentation for executeSafePromise to clarify that in Node/Electron, the process exits on fatal errors, while in browsers, an unhandled error is thrown.

  • Improved environment detection by explicitly checking if process is defined before calling process.exit, ensuring compatibility with browser environments.

  • In browser environments, changed the thrown error to use the cause property for proper error chaining, aligning with modern JavaScript error handling best practices.

Future steps

We may want to improve test coverage on this. I've tried but karma doesn't let it slide so easily.

Otherwise `process` breaks in browsers.
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR refactors the executeSafePromise utility to improve cross-platform compatibility, specifically making it work in browser environments without requiring polyfills. The key improvements focus on robust environment detection and proper error handling for both Node/Electron and browser contexts.

Key changes:

  • Enhanced environment detection by checking if process is defined before attempting to call process.exit()
  • Updated browser error handling to use the cause property for proper error chaining, following modern JavaScript standards
  • Improved documentation to clarify behavior differences between Node/Electron and browser environments

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Contributor

Copilot AI commented Dec 25, 2025

@mondoreale I've opened a new pull request, #3311, to work on those changes. Once the pull request is ready, I'll request review from you.

…#3311)

## Summary

Adds missing test coverage for error handling paths in
`executeSafePromise` introduced by the browser compatibility refactor
(#3310).

## Changes

- **Node/Electron environment tests**: Verify `process.exit(1)` is
called on promise rejection and fatal errors are logged
- **Browser environment tests**: Verify errors are thrown with proper
error chaining via `cause` property

The environment detection behavior is implicitly verified by the Node
and browser test blocks, which mock or remove `process.exit` and verify
the appropriate code path is taken.

## Checklist before requesting a review

- [ ] Is this a breaking change? If it is, be clear in summary.
- [x] Read through code myself one more time.
- [x] Make sure any and all `TODO` comments left behind are meant to be
left in.
- [x] Has reasonable passing test coverage?
- [ ] Updated changelog if applicable.
- [ ] Updated documentation if applicable.

<!-- START COPILOT CODING AGENT TIPS -->
---

✨ Let Copilot coding agent [set things up for
you](https://github.com/streamr-dev/network/issues/new?title=✨+Set+up+Copilot+instructions&body=Configure%20instructions%20for%20this%20repository%20as%20documented%20in%20%5BBest%20practices%20for%20Copilot%20coding%20agent%20in%20your%20repository%5D%28https://gh.io/copilot-coding-agent-tips%29%2E%0A%0A%3COnboard%20this%20repo%3E&assignees=copilot)
— coding agent works faster and does higher quality work when set up for
your repo.

<!-- CURSOR_SUMMARY -->
---

> [!NOTE]
> Expands test coverage for `executeSafePromise` to validate
error-handling in different runtimes.
> 
> - Adds Node/Electron tests in
`packages/utils/test/executeSafePromise.test.ts` to assert
`process.exit(1)` is invoked on rejection and that exit is triggered
after fatal errors
> - Adds browser-environment tests by removing `process.exit` to assert
a thrown error with message `executeSafePromise: Assertion failure!` and
proper `cause` chaining
> - Retains success-path test to confirm resolved value handling
> 
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
ff0e81f. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->

---------

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: mondoreale <[email protected]>
@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
1 out of 2 committers have signed the CLA.

✅ mondoreale
❌ Copilot
You have signed the CLA already but the status is still pending? Let us recheck it.

@mondoreale mondoreale merged commit 91c09c8 into main Dec 26, 2025
42 of 43 checks passed
@mondoreale mondoreale deleted the cross-environment-executeSafePromise branch December 26, 2025 00:49
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.

3 participants