From 8df82eb4e36ba2f264003df4c332776311bad98e Mon Sep 17 00:00:00 2001 From: Brendan Gibson Date: Thu, 27 Jul 2023 10:26:13 -0600 Subject: [PATCH] Better error handling --- src/components/MFCard/CardIframe.tsx | 5 +++-- src/components/MFCard/useTaskCards.ts | 14 +++++++++----- src/pages/Task/index.tsx | 15 ++++++++++++++- src/translations/en.ts | 1 + 4 files changed, 27 insertions(+), 8 deletions(-) diff --git a/src/components/MFCard/CardIframe.tsx b/src/components/MFCard/CardIframe.tsx index 5fa3cce6..5d2736b7 100644 --- a/src/components/MFCard/CardIframe.tsx +++ b/src/components/MFCard/CardIframe.tsx @@ -1,4 +1,4 @@ -import React, { useEffect, useRef, useState } from 'react'; +import React, { ReactEventHandler, useEffect, useRef, useState } from 'react'; import styled from 'styled-components'; import { MESSAGE_NAME } from '../Plugins/PluginManager'; import { apiHttp } from '../../constants'; @@ -76,7 +76,8 @@ const CardIframe: React.FC = ({ path }) => { setLoading(false); }; - const handleIframeError = () => { + const handleIframeError: ReactEventHandler = (e) => { + console.error('Error loading card', e); setError(true); }; diff --git a/src/components/MFCard/useTaskCards.ts b/src/components/MFCard/useTaskCards.ts index 8830825a..7a9b206c 100644 --- a/src/components/MFCard/useTaskCards.ts +++ b/src/components/MFCard/useTaskCards.ts @@ -72,7 +72,7 @@ export default function useTaskCards(task: Task | null, decorators: Decorator[]) // We want to invalidate cache when polling since cache would return old results. // First request will be without invalidate and if that returns us all expected cards // we don't need to poll and invalidate. - return fetch(`${apiHttp(path)}${invalidate ? '?invalidate=true' : ''}`) + fetch(`${apiHttp(path)}${invalidate ? '?invalidate=true' : ''}`) .then((result) => result.json()) .then((result: DataModel) => { if (result.status === 200) { @@ -87,7 +87,11 @@ export default function useTaskCards(task: Task | null, decorators: Decorator[]) } }) .catch((e) => { - console.warn('Cards request failed for ', apiHttp(path), e); + console.error('Cards request failed for ', apiHttp(path), e); + setTaskCards((prev) => ({ + ...prev, + [path]: { cards: prev[path]?.cards ?? [], status: 'error' }, + })); }) .finally(() => { setPoll(true); @@ -109,7 +113,7 @@ export default function useTaskCards(task: Task | null, decorators: Decorator[]) if (taskCards[url]?.status !== 'success') { setTaskCards((prev) => ({ ...prev, - [url]: { ...prev[url], status: 'success' }, + [url]: { cards: prev[url]?.cards ?? [], status: 'success' }, })); } // If the timeout has been reached @@ -118,7 +122,7 @@ export default function useTaskCards(task: Task | null, decorators: Decorator[]) if (taskCards[url]?.status !== 'timeout') { setTaskCards((prev) => ({ ...prev, - [url]: { ...prev[url], status: 'timeout' }, + [url]: { cards: prev[url]?.cards ?? [], status: 'timeout' }, })); } } else { @@ -126,7 +130,7 @@ export default function useTaskCards(task: Task | null, decorators: Decorator[]) if (taskCards[url]?.status !== 'loading') { setTaskCards((prev) => ({ ...prev, - [url]: { ...prev[url], status: 'loading' }, + [url]: { cards: prev[url]?.cards ?? [], status: 'loading' }, })); } t = window.setTimeout(() => { diff --git a/src/pages/Task/index.tsx b/src/pages/Task/index.tsx index 8a9cf3fd..6f03a5e0 100755 --- a/src/pages/Task/index.tsx +++ b/src/pages/Task/index.tsx @@ -401,7 +401,7 @@ const Task: React.FC = ({ ] : []), // Render cards at the end of sections if enabled by feature flags. - ...(showCards + ...(showCards && cardsResult?.cards?.length ? cardsResult?.cards?.map((def) => ({ key: def.hash, order: 99, @@ -466,6 +466,19 @@ const Task: React.FC = ({ ] : [] : []), + // Show error if cards were not fetched before timeout + ...(showCards + ? cardsResult?.status === 'error' + ? [ + { + key: 'card_error', + order: 99, + label: t('card.card_error'), + component: <>, + }, + ] + : [] + : []), ].sort((a, b) => a.order - b.order)} /> diff --git a/src/translations/en.ts b/src/translations/en.ts index 7d2f5b56..0291e77d 100644 --- a/src/translations/en.ts +++ b/src/translations/en.ts @@ -330,6 +330,7 @@ const en = { download_card: 'Download card HTML file', link_card: 'Link to card', card_timeout: 'Timeout: loading cards', + card_error: 'Error: loading cards', card_loading: 'Loading cards', }, },