Skip to content

Commit 98508fa

Browse files
committed
Make executeSafePromise work in browser without polyfills
Otherwise `process` breaks in browsers.
1 parent 77c7b07 commit 98508fa

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

packages/utils/src/executeSafePromise.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import { Logger } from './Logger'
33
const logger = new Logger(module)
44

55
/**
6-
* Execute a promise that should never reject. If it does, log the error and exit the process.
6+
* Execute a promise that should never reject. If it does, log the error and exit the process
7+
* (in Node/Electron) or throw an unhandled error (in browsers).
78
* To be used in places where we want to "annotate" that the intention of a promise is never
89
* to reject (unless something is really wrong).
910
*/
@@ -12,11 +13,13 @@ export const executeSafePromise = async <T>(createPromise: () => Promise<T>): Pr
1213
return await createPromise()
1314
} catch (err: any) {
1415
logger.fatal('Assertion failure!', { message: err?.message, err })
15-
if (process.exit !== undefined) {
16+
17+
// Check if we're in a Node/Electron environment
18+
if (typeof process !== 'undefined' && process.exit !== undefined) {
1619
process.exit(1)
1720
} else {
18-
// cause an unhandled promise rejection on purpose
19-
throw new Error('executeSafePromise: Assertion failure!', err)
21+
// Browser environment - throw with proper error chaining
22+
throw new Error('executeSafePromise: Assertion failure!', { cause: err })
2023
}
2124
}
2225
}

0 commit comments

Comments
 (0)