From f31ba657cc3727bff0ff8a74a1448b586a3432ad Mon Sep 17 00:00:00 2001 From: Oliver Date: Tue, 24 Dec 2024 07:18:56 +1100 Subject: [PATCH] API error handling (#8739) * API error handling - Add error handlers to various API calls * Fix return type --- .../src/components/buttons/SSOButton.tsx | 8 ++++ .../dashboard/widgets/NewsWidget.tsx | 3 +- .../widgets/QueryCountDashboardWidget.tsx | 3 +- .../components/forms/AuthenticationForm.tsx | 38 +++++++++++-------- .../components/plugins/RemoteComponent.tsx | 28 ++++++++------ src/frontend/src/tables/InvenTreeTable.tsx | 10 +++++ 6 files changed, 61 insertions(+), 29 deletions(-) diff --git a/src/frontend/src/components/buttons/SSOButton.tsx b/src/frontend/src/components/buttons/SSOButton.tsx index 4454c200e8..fc136768f7 100644 --- a/src/frontend/src/components/buttons/SSOButton.tsx +++ b/src/frontend/src/components/buttons/SSOButton.tsx @@ -15,6 +15,7 @@ import { } from '@tabler/icons-react'; import { t } from '@lingui/macro'; +import { showNotification } from '@mantine/notifications'; import { api } from '../../App'; import { ApiEndpoints } from '../../enums/ApiEndpoints'; import { apiUrl } from '../../states/ApiState'; @@ -46,6 +47,13 @@ export function SsoButton({ provider }: Readonly<{ provider: Provider }>) { .then(() => { // redirect to login window.location.href = provider.login; + }) + .catch(() => { + showNotification({ + title: t`Error`, + message: t`Sign in redirect failed.`, + color: 'red' + }); }); } diff --git a/src/frontend/src/components/dashboard/widgets/NewsWidget.tsx b/src/frontend/src/components/dashboard/widgets/NewsWidget.tsx index 183ad90179..1ac003ff2e 100644 --- a/src/frontend/src/components/dashboard/widgets/NewsWidget.tsx +++ b/src/frontend/src/components/dashboard/widgets/NewsWidget.tsx @@ -99,7 +99,8 @@ export default function NewsWidget() { }) .then(() => { newsItems.refetch(); - }); + }) + .catch(() => {}); }, [newsItems] ); diff --git a/src/frontend/src/components/dashboard/widgets/QueryCountDashboardWidget.tsx b/src/frontend/src/components/dashboard/widgets/QueryCountDashboardWidget.tsx index 0ec30de04a..66e70d8c5c 100644 --- a/src/frontend/src/components/dashboard/widgets/QueryCountDashboardWidget.tsx +++ b/src/frontend/src/components/dashboard/widgets/QueryCountDashboardWidget.tsx @@ -48,7 +48,8 @@ function QueryCountWidget({ limit: 1 } }) - .then((res) => res.data); + .then((res) => res.data) + .catch(() => {}); } }); diff --git a/src/frontend/src/components/forms/AuthenticationForm.tsx b/src/frontend/src/components/forms/AuthenticationForm.tsx index 5ae0e5f0db..3a65ff57f9 100644 --- a/src/frontend/src/components/forms/AuthenticationForm.tsx +++ b/src/frontend/src/components/forms/AuthenticationForm.tsx @@ -15,6 +15,7 @@ import { useDisclosure } from '@mantine/hooks'; import { useState } from 'react'; import { useLocation, useNavigate } from 'react-router-dom'; +import { showNotification } from '@mantine/notifications'; import { api } from '../../App'; import { ApiEndpoints } from '../../enums/ApiEndpoints'; import { @@ -44,26 +45,31 @@ export function AuthenticationForm() { setIsLoggingIn(true); if (classicLoginMode === true) { - doBasicLogin( - classicForm.values.username, - classicForm.values.password - ).then(() => { - setIsLoggingIn(false); + doBasicLogin(classicForm.values.username, classicForm.values.password) + .then(() => { + setIsLoggingIn(false); - if (isLoggedIn()) { - showLoginNotification({ - title: t`Login successful`, - message: t`Logged in successfully` - }); - followRedirect(navigate, location?.state); - } else { - showLoginNotification({ + if (isLoggedIn()) { + showLoginNotification({ + title: t`Login successful`, + message: t`Logged in successfully` + }); + followRedirect(navigate, location?.state); + } else { + showLoginNotification({ + title: t`Login failed`, + message: t`Check your input and try again.`, + success: false + }); + } + }) + .catch(() => { + showNotification({ title: t`Login failed`, message: t`Check your input and try again.`, - success: false + color: 'red' }); - } - }); + }); } else { doSimpleLogin(simpleForm.values.email).then((ret) => { setIsLoggingIn(false); diff --git a/src/frontend/src/components/plugins/RemoteComponent.tsx b/src/frontend/src/components/plugins/RemoteComponent.tsx index d5834bba7b..16dadedf4d 100644 --- a/src/frontend/src/components/plugins/RemoteComponent.tsx +++ b/src/frontend/src/components/plugins/RemoteComponent.tsx @@ -53,18 +53,24 @@ export default function RemoteComponent({ } if (sourceFile && functionName) { - findExternalPluginFunction(sourceFile, functionName).then((func) => { - if (func) { - try { - func(componentRef.current, context); - setRenderingError(''); - } catch (error) { - setRenderingError(`${error}`); + findExternalPluginFunction(sourceFile, functionName) + .then((func) => { + if (func) { + try { + func(componentRef.current, context); + setRenderingError(''); + } catch (error) { + setRenderingError(`${error}`); + } + } else { + setRenderingError(`${sourceFile}:${functionName}`); } - } else { - setRenderingError(`${sourceFile}:${functionName}`); - } - }); + }) + .catch((_error) => { + console.error( + `ERR: Failed to load remove plugin function: ${sourceFile}:${functionName}` + ); + }); } else { setRenderingError( `${t`Invalid source or function name`} - ${sourceFile}:${functionName}` diff --git a/src/frontend/src/tables/InvenTreeTable.tsx b/src/frontend/src/tables/InvenTreeTable.tsx index bceb9a9d2f..e9469aaaa7 100644 --- a/src/frontend/src/tables/InvenTreeTable.tsx +++ b/src/frontend/src/tables/InvenTreeTable.tsx @@ -12,6 +12,7 @@ import type React from 'react'; import { useCallback, useEffect, useMemo, useState } from 'react'; import { useNavigate } from 'react-router-dom'; +import { showNotification } from '@mantine/notifications'; import { api } from '../App'; import { Boundary } from '../components/Boundary'; import type { ApiFormFieldSet } from '../components/forms/fields/ApiFormField'; @@ -198,6 +199,15 @@ export function InvenTreeTable>({ setTableColumnNames(cacheKey)(names); } + return null; + }) + .catch(() => { + showNotification({ + title: t`API Error`, + message: t`Failed to load table options`, + color: 'red' + }); + return null; }); }