diff --git a/src/app/dashboard/[projectId]/page.tsx b/src/app/dashboard/[projectId]/page.tsx index 63a08b4..a63317f 100644 --- a/src/app/dashboard/[projectId]/page.tsx +++ b/src/app/dashboard/[projectId]/page.tsx @@ -67,7 +67,7 @@ export default async function Page({ { @@ -8,15 +9,15 @@ export const getRepositoryDetails = unstable_cache( repo, }) - const { data: lastActivity } = await octokit.rest.repos.getBranch({ + const lastActivity = await getBranchRepository( owner, repo, - branch: repository.default_branch, - }) - + repository.default_branch, + octokit, + ) return { ...repository, - lastCommit: lastActivity.commit, + lastCommit: lastActivity?.commit, } }, undefined, @@ -25,6 +26,25 @@ export const getRepositoryDetails = unstable_cache( }, ) +export const getBranchRepository = unstable_cache( + async (owner: string, repo: string, branch: string, octokit: Octokit) => { + try { + const { data: lastActivity } = await octokit.rest.repos.getBranch({ + owner, + repo, + branch: branch, + }) + return lastActivity + } catch (error) { + if (error instanceof RequestError) { + checkSpecificErrorGithubApi(error) + } else { + throw error + } + } + }, +) + export const getMyntenanceRepository = unstable_cache( async (octokit: Octokit) => { return getRepositoryDetails("Balastrong", "myntenance", octokit) diff --git a/src/lib/handle-error.ts b/src/lib/handle-error.ts index 81833a4..5621e4f 100644 --- a/src/lib/handle-error.ts +++ b/src/lib/handle-error.ts @@ -1,4 +1,5 @@ import { isRedirectError } from "next/dist/client/components/redirect" +import { RequestError } from "octokit" import { toast } from "sonner" import { z } from "zod" @@ -23,3 +24,12 @@ export function showErrorToast(err: unknown) { const errorMessage = getErrorMessage(err) return toast.error(errorMessage) } + +export function checkSpecificErrorGithubApi(error: RequestError) { + const message = error.message.split("-")[0].trim() + switch (message) { + case "Branch not found": + default: + return null + } +}