Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

improved error handling

Steven Teyf9f15bce540d
diff --git a/components/app/links-container.tsx b/components/app/links-container.tsx
index 675e55c863..cb7c4021ac 100644
--- a/components/app/links-container.tsx
+++ b/components/app/links-container.tsx
@@ -27,7 +27,7 @@ export default function LinksContainer({
return (
<MaxWidthWrapper>
- <div className="my-10 grid grid-cols-1 gap-3">
+ <div className="py-10 grid grid-cols-1 gap-3">
{links ? (
links.length > 0 ? (
links.map((props) => (
diff --git a/lib/utils.ts b/lib/utils.ts
index 11da83db70..1cf3d419f8 100644
--- a/lib/utils.ts
+++ b/lib/utils.ts
@@ -1,6 +1,10 @@
import ms from "ms";
import { ccTLDs } from "./constants";
+interface SWRError extends Error {
+ status: number;
+}
+
export async function fetcher<JSON = any>(
input: RequestInfo,
init?: RequestInit
@@ -8,14 +12,11 @@ export async function fetcher<JSON = any>(
const res = await fetch(input, init);
if (!res.ok) {
- if (res.status === 401) {
- throw new Error("Unauthorized");
- } else if (res.status === 403) {
- throw new Error("Forbidden");
- } else if (res.status === 404) {
- throw new Error("Not Found");
- } else if (res.status === 500) {
- throw new Error("Internal Server Error");
+ const json = await res.json();
+ if (json.error) {
+ const error = new Error(json.error) as SWRError;
+ error.status = res.status;
+ throw error;
} else {
throw new Error("An unexpected error occurred");
}
diff --git a/pages/app/[slug]/index.tsx b/pages/app/[slug]/index.tsx
index e5175c4ba4..c0474d2896 100644
--- a/pages/app/[slug]/index.tsx
+++ b/pages/app/[slug]/index.tsx
@@ -7,6 +7,7 @@ import { ProjectProps } from "@/lib/types";
import MaxWidthWrapper from "@/components/shared/max-width-wrapper";
import { useAddLinkModal } from "@/components/app/add-link-modal";
import LinksContainer from "@/components/app/links-container";
+import ErrorPage from "next/error";
export default function ProjectLinks() {
const router = useRouter();
@@ -19,12 +20,6 @@ export default function ProjectLinks() {
fetcher
);
- useEffect(() => {
- if (error) {
- router.push("/404");
- }
- }, [error]);
-
const { data: usage } = useSWR(
router.isReady &&
project &&
@@ -45,6 +40,11 @@ export default function ProjectLinks() {
domain: project?.domain,
});
+ // handle error page
+ if (error && error.status === 404) {
+ return <ErrorPage statusCode={404} />;
+ }
+
return (
<AppLayout>
{project && <AddLinkModal />}
@@ -56,11 +56,13 @@ export default function ProjectLinks() {
</div>
</MaxWidthWrapper>
</div>
- <LinksContainer
- exceededUsage={exceededUsage}
- AddLinkButton={AddLinkButton}
- domain={project?.domain}
- />
+ {project && (
+ <LinksContainer
+ exceededUsage={exceededUsage}
+ AddLinkButton={AddLinkButton}
+ domain={project?.domain}
+ />
+ )}
</AppLayout>
);
}
diff --git a/pages/app/[slug]/settings.tsx b/pages/app/[slug]/settings.tsx
index ff8261cec0..ae74ffea22 100644
--- a/pages/app/[slug]/settings.tsx
+++ b/pages/app/[slug]/settings.tsx
@@ -2,11 +2,11 @@ import AppLayout from "components/layout/app";
import { useRouter } from "next/router";
import useSWR from "swr";
import { fetcher } from "@/lib/utils";
-import { useEffect } from "react";
import { ProjectProps } from "@/lib/types";
import MaxWidthWrapper from "@/components/shared/max-width-wrapper";
import CustomDomain from "@/components/app/custom-domain";
import PlanUsage from "@/components/app/plan-usage";
+import ErrorPage from "next/error";
export default function ProjectLinks() {
const router = useRouter();
@@ -19,11 +19,10 @@ export default function ProjectLinks() {
fetcher
);
- useEffect(() => {
- if (error) {
- router.push("/404");
- }
- }, [error]);
+ // handle error page
+ if (error && error.status === 404) {
+ return <ErrorPage statusCode={404} />;
+ }
return (
<AppLayout>