From 8c399e1bbb07a179f26f545edab48ac5b6a2fa23 Mon Sep 17 00:00:00 2001 From: Muhammad Rehan Butt Date: Tue, 24 Feb 2026 13:38:29 +0500 Subject: [PATCH 1/3] Improve error handling for GitHub API requests Refactor error handling for GitHub API calls to provide more specific responses for 404 and 403 errors. --- src/app/api/generate/route.ts | 39 +++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/src/app/api/generate/route.ts b/src/app/api/generate/route.ts index 3c7f56f..cd66e25 100644 --- a/src/app/api/generate/route.ts +++ b/src/app/api/generate/route.ts @@ -56,11 +56,38 @@ export async function POST(req: Request) { { status: 400 }, ); } - - const [repoInfo, repoContents] = await Promise.all([ - getRepoData(owner, repo), - getRepoContents(owner, repo), - ]); + let repoInfo, repoContents; + try { + [repoInfo, repoContents] = await Promise.all([ + getRepoData(owner, repo), + getRepoContents(owner, repo), + ]); + } catch (error: any) { + console.error("GitHub API Error:", error.message); + + // Handle 404 (Not Found / Typos) + if (error.status === 404) { + return NextResponse.json( + { error: "Repository not found. Please check the URL for typos or ensure the repo is public." }, + { status: 404 } + ); + } + + // Handle 403 (Rate limits or Private repos) + if (error.status === 403) { + return NextResponse.json( + { error: "Access denied or GitHub API rate limit reached. Try again later." }, + { status: 403 } + ); + } + + // Generic fallback for other API issues + return NextResponse.json( + { error: "Unable to reach GitHub. Please verify the repository exists and is accessible." }, + { status: 400 } + ); + } + const files = Array.isArray(repoContents) ? repoContents.map((f: { name: string }) => f.name) @@ -154,7 +181,7 @@ export async function POST(req: Request) { console.error("README Generation Failed:", message); return NextResponse.json( - { error: "Failed to generate README. Check your URL and try again." }, + { error: "error: AI generation failed. Please try again in a moment." }, { status: 500 }, ); } From 605d5418a8aabee4a240f76f7e84d8d1b3385dde Mon Sep 17 00:00:00 2001 From: Muhammad Rehan Butt Date: Tue, 24 Feb 2026 13:46:23 +0500 Subject: [PATCH 2/3] Update src/app/api/generate/route.ts Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/app/api/generate/route.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app/api/generate/route.ts b/src/app/api/generate/route.ts index cd66e25..d471ffc 100644 --- a/src/app/api/generate/route.ts +++ b/src/app/api/generate/route.ts @@ -181,7 +181,7 @@ export async function POST(req: Request) { console.error("README Generation Failed:", message); return NextResponse.json( - { error: "error: AI generation failed. Please try again in a moment." }, + { error: "AI generation failed. Please try again in a moment." }, { status: 500 }, ); } From 126e0a97410daaa1172ee9aa6bdf73adc35d8f75 Mon Sep 17 00:00:00 2001 From: Muhammad Rehan Butt Date: Tue, 24 Feb 2026 13:46:33 +0500 Subject: [PATCH 3/3] Update src/app/api/generate/route.ts Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/app/api/generate/route.ts | 41 +++++++++++------------------------ 1 file changed, 13 insertions(+), 28 deletions(-) diff --git a/src/app/api/generate/route.ts b/src/app/api/generate/route.ts index d471ffc..c0afdca 100644 --- a/src/app/api/generate/route.ts +++ b/src/app/api/generate/route.ts @@ -57,37 +57,22 @@ export async function POST(req: Request) { ); } let repoInfo, repoContents; - try { - [repoInfo, repoContents] = await Promise.all([ - getRepoData(owner, repo), - getRepoContents(owner, repo), - ]); - } catch (error: any) { - console.error("GitHub API Error:", error.message); - - // Handle 404 (Not Found / Typos) - if (error.status === 404) { - return NextResponse.json( - { error: "Repository not found. Please check the URL for typos or ensure the repo is public." }, - { status: 404 } - ); - } - - // Handle 403 (Rate limits or Private repos) - if (error.status === 403) { - return NextResponse.json( - { error: "Access denied or GitHub API rate limit reached. Try again later." }, - { status: 403 } - ); - } - - // Generic fallback for other API issues + [repoInfo, repoContents] = await Promise.all([ + getRepoData(owner, repo), + getRepoContents(owner, repo), + ]); + + // If the Octokit helpers swallow errors and return null/empty results, + // handle that case explicitly here instead of relying on try/catch. + if (!repoInfo) { return NextResponse.json( - { error: "Unable to reach GitHub. Please verify the repository exists and is accessible." }, - { status: 400 } + { + error: + "Unable to fetch repository from GitHub. Please verify the repository exists, is public, and the URL is correct.", + }, + { status: 400 }, ); } - const files = Array.isArray(repoContents) ? repoContents.map((f: { name: string }) => f.name)