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

change reporting to new handler

parent 7272c2da
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"
......@@ -3,6 +3,9 @@ import type { AppProps } from 'next/app';
import '@/styles/index.css';
import { useEffect } from 'react';
import { store } from '@/redux/store';
import { initializeErrorReporting } from '@/utils/error-report/errorReporting';
initializeErrorReporting();
const MyApp = ({ Component, pageProps }: AppProps): JSX.Element => {
const project = store.getState().project.data;
......
import { showToast } from '@/utils/showToast';
import { handleError } from '@/utils/error-report/errorReporting';
import { errorMiddlewareListener } from './error.middleware';
jest.mock('../../utils/showToast', () => ({
showToast: jest.fn(),
jest.mock('../../utils/error-report/errorReporting', () => ({
handleError: jest.fn(),
}));
describe('errorMiddlewareListener', () => {
// eslint-disable-next-line no-console
beforeEach(() => {
jest.clearAllMocks();
});
......@@ -19,9 +21,12 @@ describe('errorMiddlewareListener', () => {
rejectedWithValue: true,
requestStatus: 'rejected',
},
error: {
code: 'Error 2',
},
};
await errorMiddlewareListener(action);
expect(showToast).toHaveBeenCalledWith({ type: 'error', message: 'Error message' });
expect(handleError).toHaveBeenCalledWith({ code: 'Error 2' });
});
it('should show toast with unknown error when action is rejected without value', async () => {
......@@ -33,12 +38,12 @@ describe('errorMiddlewareListener', () => {
rejectedWithValue: true,
requestStatus: 'rejected',
},
error: {
code: 'Error 3',
},
};
await errorMiddlewareListener(action);
expect(showToast).toHaveBeenCalledWith({
type: 'error',
message: 'An unknown error occurred. Please try again later.',
});
expect(handleError).toHaveBeenCalledWith({ code: 'Error 3' });
});
it('should not show toast when action is not rejected', async () => {
......@@ -51,7 +56,7 @@ describe('errorMiddlewareListener', () => {
},
};
await errorMiddlewareListener(action);
expect(showToast).not.toHaveBeenCalled();
expect(handleError).not.toHaveBeenCalled();
});
it('should show toast with unknown error when action payload is not a string', async () => {
......@@ -63,12 +68,13 @@ describe('errorMiddlewareListener', () => {
rejectedWithValue: true,
requestStatus: 'rejected',
},
error: {
code: 'ERROR',
message: 'Error message',
},
};
await errorMiddlewareListener(action);
expect(showToast).toHaveBeenCalledWith({
type: 'error',
message: 'An unknown error occurred. Please try again later.',
});
expect(handleError).toHaveBeenCalledWith({ code: 'ERROR', message: 'Error message' });
});
it('should show toast with custom message when action payload is a string', async () => {
......@@ -80,8 +86,11 @@ describe('errorMiddlewareListener', () => {
rejectedWithValue: true,
requestStatus: 'rejected',
},
error: {
code: 'ERROR',
},
};
await errorMiddlewareListener(action);
expect(showToast).toHaveBeenCalledWith({ type: 'error', message: 'Failed to fetch' });
expect(handleError).toHaveBeenCalledWith({ code: 'ERROR' });
});
});
import type { AppStartListening } from '@/redux/store';
import { UNKNOWN_ERROR } from '@/utils/getErrorMessage/getErrorMessage.constants';
import { showToast } from '@/utils/showToast';
import {
Action,
createListenerMiddleware,
isRejected,
isRejectedWithValue,
} from '@reduxjs/toolkit';
import { Action, createListenerMiddleware, isRejected } from '@reduxjs/toolkit';
import { handleError } from '@/utils/error-report/errorReporting';
export const errorListenerMiddleware = createListenerMiddleware();
const startListening = errorListenerMiddleware.startListening as AppStartListening;
export const errorMiddlewareListener = async (action: Action): Promise<void> => {
if (isRejectedWithValue(action)) {
let message: string;
if (typeof action.payload === 'string') {
message = action.payload;
} else {
message = UNKNOWN_ERROR;
}
showToast({
type: 'error',
message,
});
if (isRejected(action)) {
handleError(action.error);
}
};
......
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