_error.tsx 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import clsx from "clsx";
  2. import Head from "next/head";
  3. import Link from "next/link";
  4. import { useMemo } from "react";
  5. import { ErrorProps } from "next/error";
  6. import randomRange from "libs/randomRange";
  7. import styles from "styles/error.module.scss";
  8. const statusCodes = {
  9. 400: "Bad Request",
  10. 404: "This page could not be found",
  11. 405: "Method Not Allowed",
  12. 500: "Internal Server Error",
  13. };
  14. const MyError = (props: ErrorProps) => {
  15. const { statusCode } = props;
  16. const title =
  17. props.title ||
  18. statusCodes[`${statusCode}` as unknown as keyof typeof statusCodes] ||
  19. "An unexpected error has occurred";
  20. const bgNumber = useMemo(() => randomRange(1, 5), []);
  21. return (
  22. <div className={clsx(styles["page"], styles[`bg-${bgNumber}`])}>
  23. <Head>
  24. <title>
  25. {statusCode
  26. ? `${statusCode}: ${title}`
  27. : "Application error: a client-side exception has occurred"}
  28. </title>
  29. </Head>
  30. <div className={clsx(styles["main"])}>
  31. {statusCode ? <h1 className="next-error-h1">{statusCode}</h1> : null}
  32. <h2>
  33. {props.title || statusCode
  34. ? title
  35. : "Application error: a client-side exception has occurred (see the browser console for more information)"}
  36. .
  37. </h2>
  38. <Link className="btn btn-primary" href="/">
  39. Back to Home
  40. </Link>
  41. </div>
  42. </div>
  43. );
  44. };
  45. export default MyError;