* Execute an async operation with error handling and user feedback
(
operation: () => Promise<T>,
options: {
fallback?: T;
errorMessage?: string;
showNotice?: boolean;
noticeHandler?: SafeAsyncNoticeHandler;
logError?: boolean;
} = {}
)
| 26 | * Execute an async operation with error handling and user feedback |
| 27 | */ |
| 28 | static async execute<T>( |
| 29 | operation: () => Promise<T>, |
| 30 | options: { |
| 31 | fallback?: T; |
| 32 | errorMessage?: string; |
| 33 | showNotice?: boolean; |
| 34 | noticeHandler?: SafeAsyncNoticeHandler; |
| 35 | logError?: boolean; |
| 36 | } = {} |
| 37 | ): Promise<T | undefined> { |
| 38 | const { |
| 39 | fallback, |
| 40 | errorMessage = "An error occurred", |
| 41 | showNotice = true, |
| 42 | noticeHandler, |
| 43 | logError = true, |
| 44 | } = options; |
| 45 | |
| 46 | try { |
| 47 | return await operation(); |
| 48 | } catch (error) { |
| 49 | if (logError) { |
| 50 | tasknotesLogger.error(errorMessage, { |
| 51 | category: "provider", |
| 52 | operation: "execute-safe-operation", |
| 53 | error: error, |
| 54 | }); |
| 55 | } |
| 56 | |
| 57 | if (showNotice) { |
| 58 | const message = error instanceof Error ? error.message : String(error); |
| 59 | notifyIfRequested(`${errorMessage}: ${message}`, { |
| 60 | showNotice, |
| 61 | noticeHandler, |
| 62 | }); |
| 63 | } |
| 64 | |
| 65 | return fallback; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Execute an async operation with retry logic |
no test coverage detected