From 662c1e39e60573c31f59ce5f7b9cda2dd5b530e7 Mon Sep 17 00:00:00 2001
From: Piotr Gawron <p.gawron@atcomp.pl>
Date: Wed, 15 May 2024 13:58:59 +0200
Subject: [PATCH] return error using new interface

---
 src/redux/statistics/statistics.reducers.test.ts | 8 +++++---
 src/redux/statistics/statistics.thunks.ts        | 7 +++----
 2 files changed, 8 insertions(+), 7 deletions(-)

diff --git a/src/redux/statistics/statistics.reducers.test.ts b/src/redux/statistics/statistics.reducers.test.ts
index 6d620db1..45a0b843 100644
--- a/src/redux/statistics/statistics.reducers.test.ts
+++ b/src/redux/statistics/statistics.reducers.test.ts
@@ -7,6 +7,7 @@ import { mockNetworkResponse } from '@/utils/mockNetworkResponse';
 import { HttpStatusCode } from 'axios';
 import { waitFor } from '@testing-library/react';
 import { statisticsFixture } from '@/models/fixtures/statisticsFixture';
+import { unwrapResult } from '@reduxjs/toolkit';
 import { StatisticsState } from './statistics.types';
 import statisticsReducer from './statistics.slice';
 import { apiPath } from '../apiPath';
@@ -56,11 +57,12 @@ describe('statistics reducer', () => {
       .onGet(apiPath.getStatisticsById(PROJECT_ID))
       .reply(HttpStatusCode.NotFound, undefined);
 
-    const { type, payload } = await store.dispatch(getStatisticsById(PROJECT_ID));
+    const action = await store.dispatch(getStatisticsById(PROJECT_ID));
+
     const { loading } = store.getState().statistics;
 
-    expect(type).toBe('statistics/getStatisticsById/rejected');
-    expect(payload).toBe(
+    expect(action.type).toBe('statistics/getStatisticsById/rejected');
+    expect(() => unwrapResult(action)).toThrow(
       "Failed to fetch statistics: The page you're looking for doesn't exist. Please verify the URL and try again.",
     );
 
diff --git a/src/redux/statistics/statistics.thunks.ts b/src/redux/statistics/statistics.thunks.ts
index 1a683a4e..dcaf0100 100644
--- a/src/redux/statistics/statistics.thunks.ts
+++ b/src/redux/statistics/statistics.thunks.ts
@@ -3,14 +3,14 @@ import { Statistics } from '@/types/models';
 import { validateDataUsingZodSchema } from '@/utils/validateDataUsingZodSchema';
 import { createAsyncThunk } from '@reduxjs/toolkit';
 import { statisticsSchema } from '@/models/statisticsSchema';
-import { getErrorMessage } from '@/utils/getErrorMessage';
 import { ThunkConfig } from '@/types/store';
+import { getError } from '@/utils/error-report/getError';
 import { apiPath } from '../apiPath';
 import { STATISTICS_FETCHING_ERROR_PREFIX } from './statistics.constants';
 
 export const getStatisticsById = createAsyncThunk<Statistics | undefined, string, ThunkConfig>(
   'statistics/getStatisticsById',
-  async (id, { rejectWithValue }) => {
+  async id => {
     try {
       const response = await axiosInstance.get<Statistics>(apiPath.getStatisticsById(id));
 
@@ -18,8 +18,7 @@ export const getStatisticsById = createAsyncThunk<Statistics | undefined, string
 
       return isDataValid ? response.data : undefined;
     } catch (error) {
-      const errorMessage = getErrorMessage({ error, prefix: STATISTICS_FETCHING_ERROR_PREFIX });
-      return rejectWithValue(errorMessage);
+      return Promise.reject(getError({ error, prefix: STATISTICS_FETCHING_ERROR_PREFIX }));
     }
   },
 );
-- 
GitLab