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

Call ApiErrorModal using Error Boundary #106

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
30 changes: 30 additions & 0 deletions ui/src/app/error.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use client';

import { useState } from 'react';
import ApiErrorModal from '@/components/modal/api-error-modal';

interface ErrorProps {
reset: () => void;
}

const Error = ({ reset }: ErrorProps) => {
const [open, setOpen] = useState(true);

const handleClose = () => {
setOpen(false);
reset();
};

return (
<div className={'min-h-screen'}>
{open && (
<ApiErrorModal
open={open}
onClose={handleClose}
/>
)}
</div>
);
};

export default Error;
14 changes: 7 additions & 7 deletions ui/src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ const RootLayout = ({
}: Readonly<{
children?: React.ReactNode;
}>) => (
<html lang="en">
<body className={sourceSans3.variable}>
<Navbar />
<main className="h-full 2xl:min-h-[72.3vh]">{children}</main>
<Footer />
</body>
</html>
<html lang="en">
<body className={sourceSans3.variable}>
<Navbar />
<main className="h-full 2xl:min-h-[72.3vh]">{children}</main>
<Footer />
</body>
</html>
);

export default RootLayout;
4 changes: 3 additions & 1 deletion ui/src/components/modal/api-error-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@ import {
import Link from 'next/link';
import { useState } from 'react';

const ApiErrorModal = ({ open }: { open: boolean }) => {
const ApiErrorModal = ({ open, onClose }: { open: boolean }) => {
const [openApiErrorModal, setOpenApiErrorModal] = useState(open);

/**
* Closes the modal.
*/
const close = () => {
setOpenApiErrorModal(false);
onClose();
};

return (
<AlertDialog open={openApiErrorModal}>
<AlertDialogContent>
Expand Down
5 changes: 3 additions & 2 deletions ui/src/lib/http-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,11 @@ export const deleteRequestAsync = async <T>(
*
* @returns The res.json()
*/
const handleFetch = (res: Response, httpMethod: HTTPMethod) => {
const handleFetch = async (res: Response, httpMethod: HTTPMethod) => {
if (!res.ok) {
const error = Error(`${res.status} error from ${httpMethod} ${res.url}`);
sendStackTrace(error.stack as string);
await sendStackTrace(error.stack as string);
throw error;
}
return res.json();
};
Expand Down
3 changes: 2 additions & 1 deletion ui/tests/lib/fetchers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ describe('fetchers', () => {
fetchMock.mockResponse(JSON.stringify(mockError), { status: 500 });
let res = ownedGrouping(groupPaths, page, size, sortString, isAscending);
await jest.advanceTimersByTimeAsync(5000);
expect(await res).toEqual(mockError);
expect(await res).toBeInstanceOf(Error);
expect((await res).message).toMatch(/500 error from POST/);

fetchMock.mockReject(() => Promise.reject(mockError));
res = ownedGrouping(groupPaths, page, size, sortString, isAscending);
Expand Down