Skip to content
Snippets Groups Projects
Commit 26750ed5 authored by Johanna Szepanski's avatar Johanna Szepanski
Browse files

added error handling and a common ApiAlert component

parent 2d0ba833
No related branches found
No related tags found
2 merge requests!542Prepare 4.1.0,!532CE-2537: Use rtk query
import { Alert } from "@mui/material";
import { ApiError, ErrorMessage } from "../../../types/common";
import { FetchBaseQueryError } from "@reduxjs/toolkit/query";
import { SerializedError } from "@reduxjs/toolkit";
interface ApiAlertErrorProps {
error: ApiError;
}
export const isFetchBaseQueryError = (
error: unknown
): error is FetchBaseQueryError => {
return typeof error === "object" && error != null && "status" in error;
};
export const isErrorWithMessage = (
error: unknown
): error is { message: string } => {
return (
typeof error === "object" &&
error != null &&
"message" in error &&
typeof (error as SerializedError).message === "string"
);
};
export const getErrorMessage = (
error: FetchBaseQueryError | SerializedError | unknown
) => {
if (error) {
if (isFetchBaseQueryError(error)) {
const customError = error.data as ErrorMessage;
if (customError) {
return customError.description
? customError.description
: customError.error;
}
return "Unknown error";
} else if (isErrorWithMessage(error)) {
return error.message;
}
}
return "Unknown error";
};
export const ApiAlertError = ({ error }: ApiAlertErrorProps) => (
<Alert severity="error">{getErrorMessage(error)}</Alert>
);
...@@ -12,6 +12,7 @@ import { ...@@ -12,6 +12,7 @@ import {
} from "../../components/common/Helper"; } from "../../components/common/Helper";
import { GlobalAppBarContext, OnPageParams } from "../../types/common"; import { GlobalAppBarContext, OnPageParams } from "../../types/common";
import { SearchBar } from "../../components/common/SearchBar/SearchBar"; import { SearchBar } from "../../components/common/SearchBar/SearchBar";
import { ApiAlertError } from "../../components/common/Alerts/ApiAlertError";
import useUrlState from "@ahooksjs/use-url-state"; import useUrlState from "@ahooksjs/use-url-state";
import { import {
serialize, serialize,
...@@ -23,7 +24,7 @@ import IOCTable from "../../components/IOC/IOCTable"; ...@@ -23,7 +24,7 @@ import IOCTable from "../../components/IOC/IOCTable";
export const IOCListView = () => { export const IOCListView = () => {
const { setTitle }: GlobalAppBarContext = useGlobalAppBarContext(); const { setTitle }: GlobalAppBarContext = useGlobalAppBarContext();
useEffect(() => setTitle(applicationTitle("IOCs")), [setTitle]); useEffect(() => setTitle(applicationTitle("IOCs")), [setTitle]);
const [listIocs, { data: iocs, isLoading }] = useLazyListIocsQuery(); const [listIocs, { data: iocs, isLoading, error }] = useLazyListIocsQuery();
const [urlState, setUrlState] = useUrlState( const [urlState, setUrlState] = useUrlState(
{ {
...@@ -167,13 +168,17 @@ export const IOCListView = () => { ...@@ -167,13 +168,17 @@ export const IOCListView = () => {
loading={isLoading} loading={isLoading}
placeholder="Search" placeholder="Search"
> >
<IOCTable {error ? (
iocs={iocs?.iocList} <ApiAlertError error={error} />
loading={isLoading} ) : (
rowType="explore" <IOCTable
pagination={pagination} iocs={iocs?.iocList}
onPage={onPage} loading={isLoading}
/> rowType="explore"
pagination={pagination}
onPage={onPage}
/>
)}
</SearchBar> </SearchBar>
</Grid> </Grid>
</Grid> </Grid>
......
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