Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Vu Van Dung <joulev.vvd@yahoo.com>
- Loading branch information
Showing
4 changed files
with
81 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import Head from "next/head"; | ||
import Image from "next/image"; | ||
import { Component, ErrorInfo, FC, ReactNode } from "react"; | ||
|
||
import Button from "@client/components/buttons"; | ||
|
||
export const ErrorLayout: FC<{ code?: number }> = ({ code }) => { | ||
const errors = { | ||
404: "The page you requested cannot be found.", | ||
500: "A server error occurred, and we're working on it.", | ||
default: | ||
"An unknown error occurred, likely a client-side error. Please see the console for details.", | ||
}; | ||
return ( | ||
<> | ||
<Head> | ||
<title>Error{code ? ` ${code}` : ""} | ezkomment</title> | ||
</Head> | ||
<main className="h-screen grid place-items-center"> | ||
<div className="flex flex-col items-center gap-6 max-w-md"> | ||
<Image src="/images/logo.svg" alt="ezkomment" width={80} height={80} /> | ||
<h1 className="my-0">{code ? code : "Error"}</h1> | ||
<div className="text-center"> | ||
{code && [404, 500].includes(code) | ||
? errors[code.toString() as "404" | "500"] | ||
: errors.default} | ||
</div> | ||
<Button href="/">Return to home</Button> | ||
</div> | ||
</main> | ||
</> | ||
); | ||
}; | ||
|
||
interface ErrorBoundaryProps { | ||
children: ReactNode; | ||
} | ||
interface ErrorBoundaryState { | ||
hasError: boolean; | ||
} | ||
export class ErrorBoundary extends Component<ErrorBoundaryProps, ErrorBoundaryState> { | ||
constructor(props: ErrorBoundaryProps) { | ||
super(props); | ||
this.state = { hasError: false }; | ||
} | ||
static getDerivedStateFromError(): ErrorBoundaryState { | ||
return { hasError: true }; | ||
} | ||
componentDidCatch(error: Error, errorInfo: ErrorInfo) { | ||
console.error("Uncaught error:", error, errorInfo); | ||
} | ||
render() { | ||
return this.state.hasError ? <ErrorLayout /> : this.props.children; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { ErrorLayout } from "@client/layouts/errors"; | ||
|
||
const Error404 = () => <ErrorLayout code={404} />; | ||
|
||
export default Error404; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { NextPage } from "next"; | ||
|
||
import { ErrorLayout } from "@client/layouts/errors"; | ||
|
||
const Error: NextPage<{ code?: number }> = ({ code }) => <ErrorLayout code={code} />; | ||
|
||
Error.getInitialProps = ({ res, err }) => { | ||
const code = res ? res.statusCode : err ? err.statusCode : 404; | ||
return { code }; | ||
}; | ||
|
||
export default Error; |