From 82cef990951c3816f0bdce6472cd65014d612227 Mon Sep 17 00:00:00 2001 From: Piotr Gawron <p.gawron@atcomp.pl> Date: Wed, 15 May 2024 14:36:21 +0200 Subject: [PATCH] return rejected error for error reporting --- src/redux/drugs/drugs.reducers.test.ts | 7 ++++--- src/redux/drugs/drugs.thunks.ts | 13 +++++-------- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/src/redux/drugs/drugs.reducers.test.ts b/src/redux/drugs/drugs.reducers.test.ts index f6e6c671..69d29267 100644 --- a/src/redux/drugs/drugs.reducers.test.ts +++ b/src/redux/drugs/drugs.reducers.test.ts @@ -7,6 +7,7 @@ import { } from '@/utils/createStoreInstanceUsingSliceReducer'; import { mockNetworkNewAPIResponse } from '@/utils/mockNetworkResponse'; import { HttpStatusCode } from 'axios'; +import { unwrapResult } from '@reduxjs/toolkit'; import drugsReducer from './drugs.slice'; import { getDrugs } from './drugs.thunks'; import { DrugsState } from './drugs.types'; @@ -56,16 +57,16 @@ describe('drugs reducer', () => { .onGet(apiPath.getDrugsStringWithQuery(SEARCH_QUERY)) .reply(HttpStatusCode.NotFound, []); - const { type, payload } = await store.dispatch(getDrugs(SEARCH_QUERY)); + const action = await store.dispatch(getDrugs(SEARCH_QUERY)); const { data } = store.getState().drugs; const drugsWithSearchElement = data.find( bioEntity => bioEntity.searchQueryElement === SEARCH_QUERY, ); - expect(payload).toBe( + expect(() => unwrapResult(action)).toThrow( "Failed to fetch drugs: The page you're looking for doesn't exist. Please verify the URL and try again.", ); - expect(type).toBe('project/getDrugs/rejected'); + expect(action.type).toBe('project/getDrugs/rejected'); expect(drugsWithSearchElement).toEqual({ searchQueryElement: SEARCH_QUERY, data: undefined, diff --git a/src/redux/drugs/drugs.thunks.ts b/src/redux/drugs/drugs.thunks.ts index f837f49d..c327e5cd 100644 --- a/src/redux/drugs/drugs.thunks.ts +++ b/src/redux/drugs/drugs.thunks.ts @@ -3,16 +3,16 @@ import { apiPath } from '@/redux/apiPath'; import { axiosInstanceNewAPI } from '@/services/api/utils/axiosInstance'; import { Drug } from '@/types/models'; import { ThunkConfig } from '@/types/store'; -import { getErrorMessage } from '@/utils/getErrorMessage'; import { validateDataUsingZodSchema } from '@/utils/validateDataUsingZodSchema'; import { createAsyncThunk } from '@reduxjs/toolkit'; import { z } from 'zod'; +import { getError } from '@/utils/error-report/getError'; import { addNumbersToEntityNumberData } from '../entityNumber/entityNumber.slice'; import { DRUGS_FETCHING_ERROR_PREFIX, MULTI_DRUGS_FETCHING_ERROR_PREFIX } from './drugs.constants'; export const getDrugs = createAsyncThunk<Drug[] | undefined, string, ThunkConfig>( 'project/getDrugs', - async (searchQuery: string, { rejectWithValue }) => { + async (searchQuery: string) => { try { const response = await axiosInstanceNewAPI.get<Drug[]>( apiPath.getDrugsStringWithQuery(searchQuery), @@ -22,8 +22,7 @@ export const getDrugs = createAsyncThunk<Drug[] | undefined, string, ThunkConfig return isDataValid ? response.data : undefined; } catch (error) { - const errorMessage = getErrorMessage({ error, prefix: DRUGS_FETCHING_ERROR_PREFIX }); - return rejectWithValue(errorMessage); + return Promise.reject(getError({ error, prefix: DRUGS_FETCHING_ERROR_PREFIX })); } }, ); @@ -31,7 +30,7 @@ export const getDrugs = createAsyncThunk<Drug[] | undefined, string, ThunkConfig export const getMultiDrugs = createAsyncThunk<void, string[], ThunkConfig>( 'project/getMultiDrugs', // eslint-disable-next-line consistent-return - async (searchQueries, { dispatch, rejectWithValue }) => { + async (searchQueries, { dispatch }) => { try { const asyncGetDrugsFunctions = searchQueries.map(searchQuery => dispatch(getDrugs(searchQuery)), @@ -52,9 +51,7 @@ export const getMultiDrugs = createAsyncThunk<void, string[], ThunkConfig>( const drugsIds = drugsTargetsData.map(d => d.elementId); dispatch(addNumbersToEntityNumberData(drugsIds)); } catch (error) { - const errorMessage = getErrorMessage({ error, prefix: MULTI_DRUGS_FETCHING_ERROR_PREFIX }); - - return rejectWithValue(errorMessage); + return Promise.reject(getError({ error, prefix: MULTI_DRUGS_FETCHING_ERROR_PREFIX })); } }, ); -- GitLab