Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OV-7: error handling #21

Merged
merged 4 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions frontend/src/bundles/common/helpers/helpers.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { stringToReactNode } from './string-to-react-node.js';
export { configureString } from 'shared';
11 changes: 11 additions & 0 deletions frontend/src/bundles/common/helpers/string-to-react-node.tsx
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

helpers should not contain .tsx files. And also create a folder for file

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I deleted the helper based on the recommendations provided during the daily meeting.

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { type ReactNode } from 'react';

const stringToReactNode = (message: string): ReactNode => {
return message.split('\n').map((line, index) => (
<div key={index} style={{ whiteSpace: 'pre-wrap' }}>
{line}
</div>
));
};

export { stringToReactNode };
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { createStandaloneToast } from '@chakra-ui/react';
import {
type Middleware,
isRejected,
isRejectedWithValue,
} from '@reduxjs/toolkit';
import { type ServerValidationErrorResponse } from 'shared';

import { theme } from '~/framework/theme/theme.js';

import { stringToReactNode } from '../helpers/helpers.js';

const { toast } = createStandaloneToast({ theme: theme });
const toastId = 'redux-store-error';

const errorMiddleware: Middleware = () => {
return (next) => (action) => {
let message: string = '';
if (isRejectedWithValue(action)) {
message += JSON.stringify(action.payload);
} else if (isRejected(action)) {
const error = action.error as ServerValidationErrorResponse;
message += `${error.message}\n`;
if (error.details) {
for (const errorDetail of error.details) {
message += `\t- ${errorDetail.message}\n`;
}
}
}

if (message !== '' && !toast.isActive(toastId)) {
toast({
id: toastId,
title: 'An error occurred.',
description: stringToReactNode(message),
status: 'error',
duration: 7000,
isClosable: true,
position: 'top-right',
});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Create a separate service for Toast that we could call for multiple scenarios: error, warn, success, etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I implemented a toast service for success, warning, error, info, and loading messages.

6

}

return next(action);
};
};

export { errorMiddleware };
1 change: 1 addition & 0 deletions frontend/src/bundles/common/middlewares/middlewares.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { errorMiddleware } from './error-handling.middleware.js';
5 changes: 4 additions & 1 deletion frontend/src/framework/store/store.package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { reducer as usersReducer } from '~/bundles/users/store/users.js';
import { userApi } from '~/bundles/users/users.js';
import { type Config } from '~/framework/config/config.js';

import { errorMiddleware } from '../../bundles/common/middlewares/error-handling.middleware.js';

type RootReducer = {
auth: ReturnType<typeof authReducer>;
users: ReturnType<typeof usersReducer>;
Expand Down Expand Up @@ -39,11 +41,12 @@ class Store {
users: usersReducer,
},
middleware: (getDefaultMiddleware) => {
return getDefaultMiddleware({
const middlewares = getDefaultMiddleware({
thunk: {
extraArgument: this.extraArguments,
},
});
return [...middlewares, errorMiddleware] as Tuple;
},
});
}
Expand Down
Loading