-
Notifications
You must be signed in to change notification settings - Fork 1
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
Changes from 3 commits
daec78e
327e22d
bda0e54
254430a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { | ||
type Middleware, | ||
isRejected, | ||
isRejectedWithValue, | ||
} from '@reduxjs/toolkit'; | ||
import { type ServerValidationErrorResponse } from 'shared'; | ||
|
||
import { toastService } from '../services/services.js'; | ||
|
||
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 !== '' && !toastService.isActive(toastId)) { | ||
toastService.error(message, toastId, 'An error occurred.'); | ||
} | ||
|
||
return next(action); | ||
}; | ||
}; | ||
|
||
export { errorMiddleware }; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { errorMiddleware } from './error-handling.middleware.js'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { type ToastService, toastService } from './toast/toast.js'; |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. rename it to notification.service.ts |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { type createStandaloneToast } from '@chakra-ui/react'; | ||
|
||
type Constructor = { | ||
toast: ReturnType<typeof createStandaloneToast>['toast']; | ||
}; | ||
|
||
class ToastService { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
private toast: ReturnType<typeof createStandaloneToast>['toast']; | ||
|
||
public constructor({ toast }: Constructor) { | ||
this.toast = toast; | ||
} | ||
|
||
public warn = (message: string, toastId: string, title: string): void => { | ||
this.toast({ | ||
id: toastId, | ||
title: title, | ||
description: message, | ||
status: 'warning', | ||
duration: 7000, | ||
isClosable: true, | ||
position: 'top-right', | ||
variant: 'solid', | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are using the same properties for every method except for status. Create a separate function for calling toast and just pass status as argument There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
}; | ||
|
||
public loading = ( | ||
message: string, | ||
toastId: string, | ||
title: string, | ||
): void => { | ||
this.toast({ | ||
id: toastId, | ||
title: title, | ||
description: message, | ||
status: 'loading', | ||
duration: 7000, | ||
isClosable: true, | ||
position: 'top-right', | ||
variant: 'solid', | ||
}); | ||
}; | ||
|
||
public error = (message: string, toastId: string, title: string): void => { | ||
this.toast({ | ||
id: toastId, | ||
title: title, | ||
description: message, | ||
status: 'error', | ||
duration: 7000, | ||
isClosable: true, | ||
position: 'top-right', | ||
variant: 'solid', | ||
}); | ||
}; | ||
|
||
public success = ( | ||
message: string, | ||
toastId: string, | ||
title: string, | ||
): void => { | ||
this.toast({ | ||
id: toastId, | ||
title: title, | ||
description: message, | ||
status: 'success', | ||
duration: 7000, | ||
isClosable: true, | ||
position: 'top-right', | ||
variant: 'solid', | ||
}); | ||
}; | ||
|
||
public info = (message: string, toastId: string, title: string): void => { | ||
this.toast({ | ||
id: toastId, | ||
title: title, | ||
description: message, | ||
status: 'info', | ||
duration: 7000, | ||
isClosable: true, | ||
position: 'top-right', | ||
variant: 'solid', | ||
}); | ||
}; | ||
|
||
public isActive = (toastId: string): boolean => { | ||
return this.toast.isActive(toastId); | ||
}; | ||
} | ||
|
||
export { ToastService }; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { createStandaloneToast } from '@chakra-ui/react'; | ||
|
||
import { theme } from '~/framework/theme/theme.js'; | ||
|
||
import { ToastService } from './toast.service.js'; | ||
|
||
const { toast } = createStandaloneToast({ theme: theme }); | ||
|
||
const toastService = new ToastService({ toast }); | ||
|
||
export { ToastService } from './toast.service.js'; | ||
export { toastService }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it possible to combine these 2 exports? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if I try to do so, I receive the next error: Use |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done