All files / Rindu/utils handleAsyncError.ts

55.55% Statements 5/9
0% Branches 0/2
100% Functions 3/3
55.55% Lines 5/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1943x     41x 13x 13x 13x                        
export function handleAsyncError<
  T extends (...args: Parameters<T>) => Promise<void>,
>(asyncFunction: T) {
  return (...args: Parameters<T>): void => {
    (async (): Promise<void> => {
      try {
        await asyncFunction(...args);
      } catch (error) {
        const functionName = asyncFunction.name;
        const isExpectedError = error instanceof Error;
        const errorMessage = `Error in ${functionName}:\n${
          isExpectedError ? error.message : "Unexpected error"
        }`;
        console.error(errorMessage);
      }
    })();
  };
}