Skip to content
Snippets Groups Projects
Commit 82cef990 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

return rejected error for error reporting

parent 6d34f98f
No related branches found
No related tags found
2 merge requests!223reset the pin numbers before search results are fetch (so the results will be...,!199Resolve "[MIN-321] form for reporting errors in minerva"
......@@ -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,
......
......@@ -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 }));
}
},
);
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment