From c677186d83c1e8e4537290788a3fe219056ab125 Mon Sep 17 00:00:00 2001 From: unewMe Date: Wed, 11 Sep 2024 00:35:08 +0200 Subject: [PATCH] feat: add error page --- src/app/api/data/[facultyId]/route.ts | 5 ++-- src/pages/error-page.tsx | 39 +++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 src/pages/error-page.tsx diff --git a/src/app/api/data/[facultyId]/route.ts b/src/app/api/data/[facultyId]/route.ts index 3935c2f..9f1e967 100644 --- a/src/app/api/data/[facultyId]/route.ts +++ b/src/app/api/data/[facultyId]/route.ts @@ -34,8 +34,9 @@ export async function GET( } catch (error) { // eslint-disable-next-line no-console console.error(error); - // eslint-disable-next-line @typescript-eslint/restrict-template-expressions - return new Response(`Internal Server Error ${error}`, { status: 200 }); + + const errorUrl = new URL(`/error-page?message=${encodeURIComponent(error.message)}`, _request.url); + return NextResponse.redirect(errorUrl, 302); } } diff --git a/src/pages/error-page.tsx b/src/pages/error-page.tsx new file mode 100644 index 0000000..cd8ba6c --- /dev/null +++ b/src/pages/error-page.tsx @@ -0,0 +1,39 @@ +import { useRouter } from 'next/router'; +import { useEffect, useState } from 'react'; + +const ErrorPage = () => { + const router = useRouter(); + const [errorMessage, setErrorMessage] = useState(null); + + useEffect(() => { + // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions + if (router.query.message) { + setErrorMessage(decodeURIComponent(router.query.message as string)); + } + }, [router.query]); + + return ( +
+

Wystąpił błąd

+ {(errorMessage !== null) ? ( +

{errorMessage}

+ ) : ( +

Nie udało się załadować szczegółów błędu.

+ )} + +
+ ); +}; + +export default ErrorPage;