Skip to content

Commit

Permalink
Add error boundary to docs page
Browse files Browse the repository at this point in the history
  • Loading branch information
grubersjoe committed Jan 17, 2024
1 parent 7a109d3 commit c5de1b5
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 12 deletions.
1 change: 1 addition & 0 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"preview": "vite preview"
},
"dependencies": {
"react-error-boundary": "^4.0.12",
"react-github-btn": "^1.4.0",
"react-github-calendar": "link:..",
"react-router-dom": "^6.21.2",
Expand Down
12 changes: 12 additions & 0 deletions example/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 17 additions & 10 deletions example/src/components/Docs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
useEffect,
useState,
} from 'react';
import { ErrorBoundary } from 'react-error-boundary';
import GitHubButton from 'react-github-btn';
import GitHubCalendar, { Props } from 'react-github-calendar';
import { useSearchParams } from 'react-router-dom';
Expand All @@ -12,6 +13,8 @@ import '../styles.scss';
import pkg from '../../package.json';

import CodeBlock from './CodeBlock.tsx';

import { errorRenderer } from './Error.tsx';
import ForkMe from './ForkMe.tsx';

const defaultUsername = 'grubersjoe';
Expand Down Expand Up @@ -73,7 +76,9 @@ const Docs: FunctionComponent = () => {
on GitHub
</h4>

<GitHubCalendar username={username} fontSize={16} />
<ErrorBoundary fallbackRender={errorRenderer} key={username}>
<GitHubCalendar username={username} fontSize={16} />
</ErrorBoundary>

<p style={{ marginBottom: '1.25rem' }}>
Made with love by <a href="https://jogruber.de">@grubersjoe</a>,
Expand Down Expand Up @@ -436,15 +441,17 @@ function transformData(data: Array<Activity>): Array<Activity>;`}

<br />

<GitHubCalendar
username={username}
transformData={selectLastHalfYear}
hideColorLegend
fontSize={16}
labels={{
totalCount: '{{count}} contributions in the last half year',
}}
/>
<ErrorBoundary fallbackRender={errorRenderer} key={username}>
<GitHubCalendar
username={username}
transformData={selectLastHalfYear}
hideColorLegend
fontSize={16}
labels={{
totalCount: '{{count}} contributions in the last half year',
}}
/>
</ErrorBoundary>

<p>
The total count will be recalculated based on the transformed data.
Expand Down
9 changes: 9 additions & 0 deletions example/src/components/Error.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { FallbackProps } from 'react-error-boundary';

export const errorRenderer = ({ error }: FallbackProps) => (
<div className="error">
<b>Error</b>
<br />
{error.message}
</div>
);
10 changes: 10 additions & 0 deletions example/src/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,16 @@ button {
}
}

.error {
display: inline-block;
margin: 0 0 1rem;
padding: 0.5rem 0.75rem;
background-color: #f8d7da;
border: 1px solid #f1aeb5;
border-radius: 4px;
font-size: 90%;
}

.react-activity-calendar {
margin: 0 0 1.5rem;
}
Expand Down
4 changes: 2 additions & 2 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import React, { useCallback, useEffect, useState } from 'react';
import Calendar, {
Activity,
Skeleton,
type Props as CalendarProps,
Skeleton,
} from 'react-activity-calendar';

import { API_URL, DEFAULT_THEME } from './constants';
Expand All @@ -27,7 +27,7 @@ async function fetchCalendarData(

if (!response.ok) {
throw Error(
`Unable to fetch GitHub contribution data for ${username}: HTTP ${response.status}}`,
`Fetching contribution data for '${username}' failed: ${(data as ApiErrorResponse).error}`,
);
}

Expand Down

0 comments on commit c5de1b5

Please sign in to comment.