Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions packages/utils/src/executeSafePromise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { Logger } from './Logger'
const logger = new Logger(module)

/**
* Execute a promise that should never reject. If it does, log the error and exit the process.
* Execute a promise that should never reject. If it does, log the error and exit the process
Comment thread
mondoreale marked this conversation as resolved.
* (in Node/Electron) or throw an unhandled error (in browsers).
* To be used in places where we want to "annotate" that the intention of a promise is never
* to reject (unless something is really wrong).
*/
Expand All @@ -12,11 +13,13 @@ export const executeSafePromise = async <T>(createPromise: () => Promise<T>): Pr
return await createPromise()
} catch (err: any) {
logger.fatal('Assertion failure!', { message: err?.message, err })
if (process.exit !== undefined) {

// Check if we're in a Node/Electron environment
if (typeof process !== 'undefined' && process.exit !== undefined) {
process.exit(1)
} else {
// cause an unhandled promise rejection on purpose
throw new Error('executeSafePromise: Assertion failure!', err)
// Browser environment - throw with proper error chaining
throw new Error('executeSafePromise: Assertion failure!', { cause: err })
}
Comment thread
mondoreale marked this conversation as resolved.
}
}