( phase: string, fn: () => Promise<T> | T, data?: Record<string, unknown>, )
| 144 | } |
| 145 | |
| 146 | export async function withDiagnosticTimer<T>( |
| 147 | phase: string, |
| 148 | fn: () => Promise<T> | T, |
| 149 | data?: Record<string, unknown>, |
| 150 | ): Promise<T> { |
| 151 | const start = Date.now(); |
| 152 | try { |
| 153 | const result = await fn(); |
| 154 | emitDiagnostic({ |
| 155 | level: 'info', |
| 156 | phase, |
| 157 | durationMs: Date.now() - start, |
| 158 | data, |
| 159 | }); |
| 160 | return result; |
| 161 | } catch (error) { |
| 162 | emitDiagnostic({ |
| 163 | level: 'error', |
| 164 | phase, |
| 165 | durationMs: Date.now() - start, |
| 166 | data: { |
| 167 | ...(data ?? {}), |
| 168 | error: error instanceof Error ? error.message : String(error), |
| 169 | }, |
| 170 | }); |
| 171 | throw error; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | export function flushDiagnosticsToSessionFile(options: { force?: boolean } = {}): string | null { |
| 176 | const scope = diagnosticsStorage.getStore(); |
no test coverage detected