| 57 | * @returns Result of the function |
| 58 | */ |
| 59 | export async function tryExec<T>( |
| 60 | fn: () => Promise<T> | T, |
| 61 | errorMessage = "An error occurred", |
| 62 | verbose = false |
| 63 | ): Promise<T> { |
| 64 | try { |
| 65 | return await fn(); |
| 66 | } catch (error) { |
| 67 | if (error instanceof SparcBenchError) { |
| 68 | throw error; |
| 69 | } else if (error instanceof Error) { |
| 70 | throw new SparcBenchError(`${errorMessage}: ${error.message}`, "EXECUTION_ERROR"); |
| 71 | } else { |
| 72 | throw new SparcBenchError(`${errorMessage}: ${String(error)}`, "EXECUTION_ERROR"); |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Error codes for SPARC-Bench |