error.tsx 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. "use client";
  2. import { useEffect } from 'react'
  3. import { useRouter } from 'next/navigation';
  4. import { emitAuthExpired } from "@/utils/auth/auth-events";
  5. //https://nextjs.org/docs/app/api-reference/file-conventions/error
  6. export default function Error({
  7. error,
  8. retry
  9. }: {
  10. retry: () => void;
  11. error: Error & { digest?: string };
  12. }) {
  13. const router = useRouter();
  14. let msg = error.message || "There was an issue with our storefront. This could be a temporary issue, please try your action again.";
  15. if(error.name === 'UnauthorizedError') {
  16. msg = "Authorization Bearer token is required. Will redirect to Login page.";
  17. }
  18. const clickHandler = () => {
  19. if(error.name === 'UnauthorizedError') {
  20. router.push('/customer/login')
  21. } else {
  22. retry();
  23. }
  24. };
  25. useEffect(() => {
  26. if (error.name === "UnauthorizedError") {
  27. emitAuthExpired();
  28. }
  29. // Log the error to an error reporting service
  30. console.error(error)
  31. }, [error])
  32. return (
  33. <div className="mx-auto my-4 flex max-w-xl flex-col rounded-lg border border-neutral-200 bg-white p-8 md:p-12 dark:border-neutral-800 dark:bg-black">
  34. <h2 className="text-xl font-bold">Oh no!</h2>
  35. <p className="my-2">
  36. {msg}
  37. </p>
  38. <button
  39. className="mx-auto mt-4 flex w-full items-center justify-center rounded-full bg-blue-600 p-4 tracking-wide text-white hover:opacity-90"
  40. onClick={clickHandler}
  41. >
  42. {error.name === 'UnauthorizedError' ? "To Login" : "Try Again"}
  43. </button>
  44. </div>
  45. );
  46. }