All files / src/shared/lib/store createThunkWithErrorHandling.ts

71.42% Statements 5/7
0% Branches 0/2
66.66% Functions 2/3
71.42% Lines 5/7

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 19 20 21      34x     34x       224x     14x 14x            
import { createAsyncThunk } from '@reduxjs/toolkit';
import { PROJECT_MESSAGES } from '@/shared/constants';
 
const handleError = (e: unknown): string =>
	e instanceof Error ? e.message : PROJECT_MESSAGES.ERROR_ACTION;
 
export const createThunkWithErrorHandling = <Returned, ThunkArg>(
	action: string,
	payload: (arg: ThunkArg) => Promise<Returned>,
) =>
	createAsyncThunk<Returned, ThunkArg, { rejectValue: string }>(
		action,
		async (arg, { rejectWithValue }) => {
			try {
				return await payload(arg);
			} catch (e) {
				return rejectWithValue(handleError(e));
			}
		},
	);