export class SharedErrorService { public static catchAsyncError< T, E extends new (message?: string | null) => Error, >( promise: Promise, errorsToCatch?: E[], ): Promise<[null, T] | [InstanceType, null]> { return promise .then((data) => { return [null, data] as [null, T] // Success case: [null, T] }) .catch((error: unknown) => { if (errorsToCatch == null) { return [error as InstanceType, null] // Error case without specific errors to catch } if (errorsToCatch.some((e) => error instanceof e)) { return [error as InstanceType, null] // Error case with specific errors to catch } throw error // Rethrow if the error is not to be caught }) } }