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 { ...@@ -7,6 +7,7 @@ import {
} from '@/utils/createStoreInstanceUsingSliceReducer'; } from '@/utils/createStoreInstanceUsingSliceReducer';
import { mockNetworkNewAPIResponse } from '@/utils/mockNetworkResponse'; import { mockNetworkNewAPIResponse } from '@/utils/mockNetworkResponse';
import { HttpStatusCode } from 'axios'; import { HttpStatusCode } from 'axios';
import { unwrapResult } from '@reduxjs/toolkit';
import drugsReducer from './drugs.slice'; import drugsReducer from './drugs.slice';
import { getDrugs } from './drugs.thunks'; import { getDrugs } from './drugs.thunks';
import { DrugsState } from './drugs.types'; import { DrugsState } from './drugs.types';
...@@ -56,16 +57,16 @@ describe('drugs reducer', () => { ...@@ -56,16 +57,16 @@ describe('drugs reducer', () => {
.onGet(apiPath.getDrugsStringWithQuery(SEARCH_QUERY)) .onGet(apiPath.getDrugsStringWithQuery(SEARCH_QUERY))
.reply(HttpStatusCode.NotFound, []); .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 { data } = store.getState().drugs;
const drugsWithSearchElement = data.find( const drugsWithSearchElement = data.find(
bioEntity => bioEntity.searchQueryElement === SEARCH_QUERY, 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.", "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({ expect(drugsWithSearchElement).toEqual({
searchQueryElement: SEARCH_QUERY, searchQueryElement: SEARCH_QUERY,
data: undefined, data: undefined,
......
...@@ -3,16 +3,16 @@ import { apiPath } from '@/redux/apiPath'; ...@@ -3,16 +3,16 @@ import { apiPath } from '@/redux/apiPath';
import { axiosInstanceNewAPI } from '@/services/api/utils/axiosInstance'; import { axiosInstanceNewAPI } from '@/services/api/utils/axiosInstance';
import { Drug } from '@/types/models'; import { Drug } from '@/types/models';
import { ThunkConfig } from '@/types/store'; import { ThunkConfig } from '@/types/store';
import { getErrorMessage } from '@/utils/getErrorMessage';
import { validateDataUsingZodSchema } from '@/utils/validateDataUsingZodSchema'; import { validateDataUsingZodSchema } from '@/utils/validateDataUsingZodSchema';
import { createAsyncThunk } from '@reduxjs/toolkit'; import { createAsyncThunk } from '@reduxjs/toolkit';
import { z } from 'zod'; import { z } from 'zod';
import { getError } from '@/utils/error-report/getError';
import { addNumbersToEntityNumberData } from '../entityNumber/entityNumber.slice'; import { addNumbersToEntityNumberData } from '../entityNumber/entityNumber.slice';
import { DRUGS_FETCHING_ERROR_PREFIX, MULTI_DRUGS_FETCHING_ERROR_PREFIX } from './drugs.constants'; import { DRUGS_FETCHING_ERROR_PREFIX, MULTI_DRUGS_FETCHING_ERROR_PREFIX } from './drugs.constants';
export const getDrugs = createAsyncThunk<Drug[] | undefined, string, ThunkConfig>( export const getDrugs = createAsyncThunk<Drug[] | undefined, string, ThunkConfig>(
'project/getDrugs', 'project/getDrugs',
async (searchQuery: string, { rejectWithValue }) => { async (searchQuery: string) => {
try { try {
const response = await axiosInstanceNewAPI.get<Drug[]>( const response = await axiosInstanceNewAPI.get<Drug[]>(
apiPath.getDrugsStringWithQuery(searchQuery), apiPath.getDrugsStringWithQuery(searchQuery),
...@@ -22,8 +22,7 @@ export const getDrugs = createAsyncThunk<Drug[] | undefined, string, ThunkConfig ...@@ -22,8 +22,7 @@ export const getDrugs = createAsyncThunk<Drug[] | undefined, string, ThunkConfig
return isDataValid ? response.data : undefined; return isDataValid ? response.data : undefined;
} catch (error) { } catch (error) {
const errorMessage = getErrorMessage({ error, prefix: DRUGS_FETCHING_ERROR_PREFIX }); return Promise.reject(getError({ error, prefix: DRUGS_FETCHING_ERROR_PREFIX }));
return rejectWithValue(errorMessage);
} }
}, },
); );
...@@ -31,7 +30,7 @@ export const getDrugs = createAsyncThunk<Drug[] | undefined, string, ThunkConfig ...@@ -31,7 +30,7 @@ export const getDrugs = createAsyncThunk<Drug[] | undefined, string, ThunkConfig
export const getMultiDrugs = createAsyncThunk<void, string[], ThunkConfig>( export const getMultiDrugs = createAsyncThunk<void, string[], ThunkConfig>(
'project/getMultiDrugs', 'project/getMultiDrugs',
// eslint-disable-next-line consistent-return // eslint-disable-next-line consistent-return
async (searchQueries, { dispatch, rejectWithValue }) => { async (searchQueries, { dispatch }) => {
try { try {
const asyncGetDrugsFunctions = searchQueries.map(searchQuery => const asyncGetDrugsFunctions = searchQueries.map(searchQuery =>
dispatch(getDrugs(searchQuery)), dispatch(getDrugs(searchQuery)),
...@@ -52,9 +51,7 @@ export const getMultiDrugs = createAsyncThunk<void, string[], ThunkConfig>( ...@@ -52,9 +51,7 @@ export const getMultiDrugs = createAsyncThunk<void, string[], ThunkConfig>(
const drugsIds = drugsTargetsData.map(d => d.elementId); const drugsIds = drugsTargetsData.map(d => d.elementId);
dispatch(addNumbersToEntityNumberData(drugsIds)); dispatch(addNumbersToEntityNumberData(drugsIds));
} catch (error) { } catch (error) {
const errorMessage = getErrorMessage({ error, prefix: MULTI_DRUGS_FETCHING_ERROR_PREFIX }); return Promise.reject(getError({ error, prefix: MULTI_DRUGS_FETCHING_ERROR_PREFIX }));
return rejectWithValue(errorMessage);
} }
}, },
); );
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