[READ-ONLY] Mirror of https://github.com/probablykasper/ferrum. Music library app for Mac, Linux and Windows ferrum.kasper.space
electron linux macos music music-library music-player napi windows
0

Configure Feed

Select the types of activity you want to include in your feed.

Add safe_call()

Kasper (Oct 4, 2024, 8:00 AM +0200) 342b4908 9025f62b

+26
+26
src/lib/error.ts
··· 28 28 }) 29 29 } 30 30 31 + /** @deprecated */ 31 32 export function call<T, P extends T | Promise<T>>(cb: (addon: typeof window.addon) => P): P { 32 33 try { 33 34 const result = cb(window.addon) ··· 45 46 throw err 46 47 } 47 48 } 49 + 50 + export function safe_call<T>( 51 + cb: (addon: typeof window.addon) => T, 52 + ): T extends Promise<unknown> ? Promise<Awaited<T> | Error> : T | Error { 53 + try { 54 + const result = cb(window.addon) 55 + 56 + // Handle asynchronous result (Promise) 57 + if (result instanceof Promise) { 58 + return result.catch((err) => { 59 + error_popup(err) 60 + return err as Error 61 + }) as T extends Promise<unknown> ? Promise<Awaited<T> | Error> : never 62 + } 63 + 64 + // Handle synchronous result 65 + return result as T extends Promise<unknown> ? never : T | Error 66 + } catch (err) { 67 + // Handle synchronous errors 68 + console.error('errorPopup:', err) 69 + error_popup(err) 70 + // Correct return type for synchronous calls 71 + return err as T extends Promise<unknown> ? never : Error 72 + } 73 + }