Skip to content

Commit

Permalink
docs(contributors): fetch response error handling (#1053)
Browse files Browse the repository at this point in the history
fix(page.tsx): error handling of Github API requests

fix #1052
  • Loading branch information
paghar authored Oct 14, 2023
1 parent a776d51 commit c1e62b3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
27 changes: 20 additions & 7 deletions app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { HiClipboardCopy, HiOutlineArrowRight } from 'react-icons/hi';
import '~/app/docs.css';
import '~/app/style.css';
import { Button, Flowbite, Footer, Navbar, TextInput, Tooltip } from '~/src';
import { safeResJson } from '~/src/helpers/http';
import { useThemeMode } from '~/src/helpers/use-theme-mode';
import { Banner } from './components/banner';
import { ComponentCard } from './components/component-card';
Expand Down Expand Up @@ -41,22 +42,34 @@ export default function HomePageContent() {

useEffect(() => {
fetch('https://api.github.com/repos/themesberg/flowbite-react/contributors?per_page=21')
.then((res) => res.json())
.then((res) => safeResJson(res))
.then((data) => {
setContributors(data);
})
.catch((err) => {
console.error(err);
});

fetch('https://api.github.com/repos/themesberg/flowbite-react')
.then((res) => res.json())
.then((data) => setStargazers(numberWithCommas(data.stargazers_count)));
.then((res) => safeResJson(res))
.then((data) => setStargazers(numberWithCommas(data.stargazers_count)))
.catch((err) => {
console.error(err);
});

fetch('https://api.npmjs.org/downloads/point/2021-01-01:2100-01-01/flowbite-react')
.then((res) => res.json())
.then((data) => setNpmDownloads(numberWithCommas(data.downloads)));
.then((res) => safeResJson(res))
.then((data) => setNpmDownloads(numberWithCommas(data.downloads)))
.catch((err) => {
console.error(err);
});

fetch('https://discord.com/api/v9/invites/4eeurUVvTy?with_counts=true&with_expiration=true')
.then((res) => res.json())
.then((data) => setDiscordMembers(numberWithCommas(data.approximate_presence_count)));
.then((res) => safeResJson(res))
.then((data) => setDiscordMembers(numberWithCommas(data.approximate_presence_count)))
.catch((err) => {
console.error(err);
});
}, []);

const numberWithCommas = (x: string | number) => {
Expand Down
6 changes: 6 additions & 0 deletions src/helpers/http.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const safeResJson = (res: Response) => {
if (res.ok) {
return res.json();
}
throw new Error('Internal server error!');
};

1 comment on commit c1e62b3

@vercel
Copy link

@vercel vercel bot commented on c1e62b3 Oct 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.