import { AsyncLocalStorage } from 'node:async_hooks'; import { randomUUID } from 'node:crypto'; import { isApplicationErrorCorrelationId } from '../../data/model/application_error'; const applicationErrorCorrelation = new AsyncLocalStorage(); export function getApplicationErrorCorrelationId(): string | undefined { return applicationErrorCorrelation.getStore(); } export function createApplicationErrorCorrelationId(): string { return randomUUID(); } export function runWithApplicationErrorCorrelation( correlationId: string, operation: () => T, ): T { if (!isApplicationErrorCorrelationId(correlationId)) { throw new TypeError('Application error correlation ID must be a lowercase UUID v4.'); } return applicationErrorCorrelation.run(correlationId, operation); } /** Starts a boundary correlation only when the caller is not already nested in one. */ export function runAtApplicationErrorBoundary(operation: () => T): T { return getApplicationErrorCorrelationId() === undefined ? runWithApplicationErrorCorrelation(createApplicationErrorCorrelationId(), operation) : operation(); }