Skip to content

Commit 90b175c

Browse files
committed
redirect to error page when profile not found
1 parent fb42495 commit 90b175c

2 files changed

Lines changed: 17 additions & 2 deletions

File tree

client/src/app/profile/[userId]/page.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@ import type { UserProfile } from "@durhack/durhack-common/types/user-profile"
44
import { useToast } from "@durhack/web-components/hooks/use-toast"
55
import { Label } from "@durhack/web-components/ui/label"
66
import { UpdateIcon } from "@radix-ui/react-icons"
7+
import { redirect, usePathname } from "next/navigation"
78
import * as React from "react"
89
import useSWR from "swr"
10+
import ModuleError from "module-error"
911

1012
import { ApplicationStatusBadge } from "@/components/dashboard/application-status-indicator"
1113
import { siteConfig } from "@/config/site"
1214
import { isLoaded } from "@/lib/is-loaded"
1315
import { cn } from "@/lib/utils"
16+
import { hasCode } from "@/lib/type-guards"
1417

1518
import { ProfileCheckInButton } from "./check-in-button"
1619
import { CvUploadBadge } from "./cv-upload-badge"
@@ -24,7 +27,7 @@ const profileFetcher = async (url: string): Promise<UserProfile> => {
2427
credentials: "include",
2528
})
2629

27-
if (response.status === 404) throw new Error("Profile not found")
30+
if (response.status === 404) throw new ModuleError("Profile not found", { code: "ERR_NOT_FOUND" })
2831
if (!response.ok) throw new Error("Failed to fetch data")
2932
const payload: unknown = await response.json()
3033
if (typeof payload !== "object" || Array.isArray(payload))
@@ -44,14 +47,20 @@ function UserAttribute({ children, className, ...props }: React.HTMLAttributes<H
4447

4548
export default function ProfilePage(props: { params: Promise<{ userId: string }> }) {
4649
const params = React.use(props.params)
50+
const pathname = usePathname()
4751
const { toast } = useToast()
4852

4953
const {
5054
data: profile,
5155
mutate: mutateProfile,
5256
error: profileError,
5357
isLoading: profileIsLoading,
54-
} = useSWR(`${siteConfig.apiUrl}/profile/${params.userId}`, profileFetcher)
58+
} = useSWR<UserProfile, unknown>(`${siteConfig.apiUrl}/profile/${params.userId}`, profileFetcher)
59+
60+
if (hasCode(profileError) && profileError.code === "ERR_NOT_FOUND") {
61+
const params = new URLSearchParams({ status_code: "404", from: pathname })
62+
redirect(`/error?${params}`)
63+
}
5564

5665
if (!isLoaded(profile, profileIsLoading, profileError))
5766
return (

client/src/lib/type-guards.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,9 @@ export function isObject(value: unknown): value is UnknownObject {
1818
if (Array.isArray(value)) return false
1919
return true
2020
}
21+
22+
export function hasCode(value: unknown): value is { code: unknown } {
23+
if (value == null) return false
24+
if (typeof value !== "object") return false
25+
return Object.hasOwn(value, "code")
26+
}

0 commit comments

Comments
 (0)