From 3cbebe61d06c3708f09aa67281f59103da5eaae3 Mon Sep 17 00:00:00 2001 From: Danieljeffery695 Date: Mon, 11 Aug 2025 23:19:38 -0700 Subject: [PATCH] add a little error handling behing the getting of data from the api --- CONTRIBUTING.md | 4 +- pages/failed-lang/index.js | 24 +++++++++ pages/search/[lang].js | 97 ++++++++++++------------------------ pages/search/testing-file.js | 90 +++++++++++++++++++++++++++++++++ 4 files changed, 148 insertions(+), 67 deletions(-) create mode 100644 pages/failed-lang/index.js create mode 100644 pages/search/testing-file.js diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 5514aa1..6c2cf90 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -63,7 +63,9 @@ To start contributing, follow these steps: 4. **Set Up GitHub API Token:** - - Visit GitHub Developer Settings to generate **three** personal access tokens. + - Visit GitHub Developer Settings to generate **three fine-grained** personal access tokens. + - Make Sure to Add some useful permissions to access smoothly and Seamless interaction with the api. + - Permissions: [Actions, Commit statues, Contents, Issues, Merge queues, Metadata, Pages, Pull request, and any other Permissions of your choice.] - Create a `.env.local` file in the project root and add your token as follows: ```sh diff --git a/pages/failed-lang/index.js b/pages/failed-lang/index.js new file mode 100644 index 0000000..7860757 --- /dev/null +++ b/pages/failed-lang/index.js @@ -0,0 +1,24 @@ +import IssuesCard from "@/components/IssuesCard"; + +// This page is used when getStaticProps or getStaticPath failed to handle the errors founds in fetching issues. +// It displays a message indicating redirect to home or github dashboard and suggests trying again. +// Default issue object below + + var issueObj = { + issueId: 0, + issueNumber: 0, + issueUrl: '/', + issueTitle: 'Something went wrong in getting your relevant programming language. please try again', + repoTitle: 'dashboard', + createdAt: 'Not_working4', + repo_forks: 0, + repo_stars: 0, + language: 'Not_working5', + repo_title: '/' + } + +export default function Failed_lang() { + return ( + + ); +} \ No newline at end of file diff --git a/pages/search/[lang].js b/pages/search/[lang].js index e702a11..2a48af3 100644 --- a/pages/search/[lang].js +++ b/pages/search/[lang].js @@ -15,6 +15,10 @@ import { FaSort } from "react-icons/fa"; import error_404 from "../../public/404.svg"; import Link from "next/link"; +// Importing Test file start here +import { loadIssues } from "./testing-file"; +// Importing Test file ends here + import { langs } from "@/helper/Languages"; import SeoTags from "@/components/SeoTags"; import moment from "moment"; @@ -230,80 +234,31 @@ export default function Search({ allIssues, lang }) { ); } -async function loadRepo(issueItems) { - var repoObj = {}; - for (const issue of issueItems) { - const repores = await fetch(issue.repository_url, { - headers: { - Authorization: "token " + process.env.NEXT_PUBLIC_TOKEN_SECOND, - Accept: "application/vnd.github.v3+json", - }, - }); - const repojson = await repores.json(); - - repoObj[issue.id] = { - full_name: repojson.full_name, - stargazers_count: repojson.stargazers_count, - forks_count: repojson.forks_count, - }; - } - - return repoObj; -} - -async function loadIssues(url, query_lang) { - const issues_res = await fetch(url, { - headers: { - Authorization: "token " + process.env.NEXT_PUBLIC_TOKEN_FIRST, - Accept: "application/vnd.github.v3+json", - }, - }); - - const issues_json = await issues_res.json(); - const issueItems = issues_json.items; - - var allIssues = []; - - var repo_res = await loadRepo(issueItems); - var mask = ""; - if (url.includes("label")) { - mask = "tag"; - } else { - mask = "language"; - } - issueItems.forEach((issue) => { - var lang = query_lang; - - var issueObj = { - issueId: issue.id, - issueNumber: issue.number, - issueUrl: issue.html_url, - issueTitle: issue.title, - repoTitle: repo_res[issue.id].full_name, - createdAt: issue.created_at, - repo_forks: repo_res[issue.id].forks_count, - repo_stars: repo_res[issue.id].stargazers_count, - - [mask]: query_lang, - }; - - if (issueObj.repo_forks > 0 && issueObj.repo_stars > 0) { - allIssues.push(issueObj); - } - }); - // setIssues(allIssues); - return allIssues; -} export async function getStaticPaths() { + try { + const paths = priority_langs.map((lang) => ({ params: { lang: lang.query }, })); return { paths, fallback: true }; + + } catch { + // Redirect to failed-lang page if there is an error and getStaticProps fail to handle the error. + return { + redirect: { + destination: "/failed-lang", + permanent: false, + } + } + } } export async function getStaticProps({ params }) { + + try { + let url = ""; tags.forEach((tag) => { if (params.lang == "go") { @@ -324,7 +279,7 @@ export async function getStaticProps({ params }) { }); } - let lang_issues = ""; + let lang_issues = []; if (url.length > 0) { lang_issues = await loadIssues(url, params.lang); @@ -337,4 +292,14 @@ export async function getStaticProps({ params }) { }, revalidate: 600, }; -} + + } catch { + // Redirect to failed-lang page if there is an error and getStaticProps fail to handle the error. + return { + redirect: { + destination: "/failed-lang", + permanent: false, + } + } + } +} \ No newline at end of file diff --git a/pages/search/testing-file.js b/pages/search/testing-file.js new file mode 100644 index 0000000..8a6a063 --- /dev/null +++ b/pages/search/testing-file.js @@ -0,0 +1,90 @@ +const errorActive = []; + +async function loadRepo(issueItems) { + try { + var repoObj = {}; + for (const issue of issueItems) { + const repores = await fetch(issue.repository_url, { + headers: { + Authorization: "token " + process.env.NEXT_PUBLIC_TOKEN_SECOND, + Accept: "application/vnd.github.v3+json", + }, + }); + const repojson = await repores.json(); + + repoObj[issue.id] = { + full_name: repojson.full_name, + stargazers_count: repojson.stargazers_count, + forks_count: repojson.forks_count, + }; + } + + return repoObj; + + } catch (error) { + console.error("Error fetching Data:", error); + errorActive.push(true); + return null; // return null and letting getStaticProps handle the error + } +} + +export async function loadIssues(url, query_lang) { + try { + const issues_res = await fetch(url, { + headers: { + Authorization: "token " + process.env.NEXT_PUBLIC_TOKEN_FIRST, + Accept: "application/vnd.github.v3+json", + }, + }); + + const issues_json = await issues_res.json(); + const issueItems = issues_json.items; + + var allIssues = []; + + var repo_res = await loadRepo(issueItems); + var mask = ""; + if (url.includes("label")) { + mask = "tag"; + } else { + mask = "language"; + } + issueItems.forEach((issue) => { + var lang = query_lang; + + var issueObj = { + issueId: issue.id, + issueNumber: issue.number, + issueUrl: issue.html_url, + issueTitle: issue.title, + repoTitle: repo_res[issue.id].full_name, + createdAt: issue.created_at, + repo_forks: repo_res[issue.id].forks_count, + repo_stars: repo_res[issue.id].stargazers_count, + + [mask]: query_lang, + }; + + if (issueObj.repo_forks > 0 && issueObj.repo_stars > 0) { + allIssues.push(issueObj); + } + }); + + // setIssues(allIssues); + return allIssues; + + } catch (error) { + console.error("Error Getting Issues:", error); + errorActive.push(true); + return null; // return null and letting getStaticProps handle the error + } + +} + +export default function TestingError() { + if(errorActive.length > 0) { + return true; + } else { + return false; + } +}