diff --git a/src/generic/alert-error/AlertError.test.tsx b/src/generic/alert-error/AlertError.test.tsx index 6cfee70db..2f193223d 100644 --- a/src/generic/alert-error/AlertError.test.tsx +++ b/src/generic/alert-error/AlertError.test.tsx @@ -35,6 +35,6 @@ describe('', () => { const { getByText } = render(); screen.logTestingPlaygroundURL(); expect(getByText(/this is an error message/i)).toBeInTheDocument(); - expect(getByText(/\{"message":"this is a response body"\}/i)).toBeInTheDocument(); + expect(getByText(/\{ "message": "this is a response body" \}/i)).toBeInTheDocument(); }); }); diff --git a/src/generic/alert-error/index.tsx b/src/generic/alert-error/index.tsx index a0612fc47..c8c060d52 100644 --- a/src/generic/alert-error/index.tsx +++ b/src/generic/alert-error/index.tsx @@ -1,14 +1,45 @@ -import React from 'react'; +import { useIntl } from '@edx/frontend-platform/i18n'; import { Alert, } from '@openedx/paragon'; +import messages from './messages'; -const AlertError: React.FC<{ error: unknown }> = ({ error }) => ( - - {error instanceof Object && 'message' in error ? error.message : String(error)} -
- {error instanceof Object && (error as any).response?.data && JSON.stringify((error as any).response?.data)} -
-); +export interface AlertErrorProps { + error: unknown; + title?: string; + onDismiss?: () => void; +} + +/* eslint-disable react/prop-types */ +const AlertError: React.FC = ({ error, title, onDismiss }) => { + const intl = useIntl(); + let errorDetails: string | undefined; + if (error instanceof Object && (error as any).response?.data) { + if (typeof (error as any).response?.data === 'string') { + errorDetails = (error as any).response?.data; + } else { + errorDetails = JSON.stringify((error as any).response?.data, null, 2); + } + } + + return ( + + {title && {title}} + {error instanceof Object && 'message' in error ? error.message : String(error)} +
+ {errorDetails && ( +
+          {errorDetails}
+        
+ )} +
+ ); +}; export default AlertError; diff --git a/src/generic/alert-error/messages.ts b/src/generic/alert-error/messages.ts new file mode 100644 index 000000000..2734da349 --- /dev/null +++ b/src/generic/alert-error/messages.ts @@ -0,0 +1,11 @@ +import { defineMessages } from '@edx/frontend-platform/i18n'; + +const messages = defineMessages({ + dismissLabel: { + id: 'authoring.alert-error-alert.dismiss', + defaultMessage: 'Dismiss', + description: 'The label for the dismiss button on the alert error component.', + }, +}); + +export default messages; diff --git a/src/library-authoring/collections/LibraryCollectionPage.test.tsx b/src/library-authoring/collections/LibraryCollectionPage.test.tsx index ed6f57d3e..2397cbb75 100644 --- a/src/library-authoring/collections/LibraryCollectionPage.test.tsx +++ b/src/library-authoring/collections/LibraryCollectionPage.test.tsx @@ -110,7 +110,8 @@ describe('', () => { it('shows an error component if no collection returned', async () => { // This mock will simulate incorrect collection id await renderLibraryCollectionPage(mockCollection.collectionEmpty); - expect(await screen.findByText(/Mocked request failed with status code 404./)).toBeInTheDocument(); + const errorMessage = 'Mocked request failed with status code 404{ "detail": "Not found." }'; + expect(await screen.findByRole('alert')).toHaveTextContent(errorMessage); }); it('shows collection data', async () => { diff --git a/src/library-authoring/create-library/CreateLibrary.test.tsx b/src/library-authoring/create-library/CreateLibrary.test.tsx index d129cfed1..5b93674b3 100644 --- a/src/library-authoring/create-library/CreateLibrary.test.tsx +++ b/src/library-authoring/create-library/CreateLibrary.test.tsx @@ -176,8 +176,8 @@ describe('', () => { '{"description":"","title":"Test Library Name","org":"org1","slug":"test_library_slug"}', ); expect(mockNavigate).not.toHaveBeenCalled(); - expect(await screen.findByRole('alert')).toHaveTextContent('Request failed with status code 400'); - expect(await screen.findByRole('alert')).toHaveTextContent('{"field":"Error message"}'); + const errorMessage = 'Request failed with status code 400{ "field": "Error message" }'; + expect(await screen.findByRole('alert')).toHaveTextContent(errorMessage); }); }); diff --git a/src/taxonomy/TaxonomyLayout.test.tsx b/src/taxonomy/TaxonomyLayout.test.tsx index 09f239888..8e89226da 100644 --- a/src/taxonomy/TaxonomyLayout.test.tsx +++ b/src/taxonomy/TaxonomyLayout.test.tsx @@ -1,6 +1,6 @@ import React, { useContext } from 'react'; -import { initializeMocks, render } from '../testUtils'; +import { initializeMocks, render, screen } from '../testUtils'; import { TaxonomyContext } from './common/context'; import { TaxonomyLayout } from './TaxonomyLayout'; @@ -9,7 +9,7 @@ const alertErrorTitle = 'Error title'; const alertErrorDescription = 'Error description'; const MockChildComponent = () => { - const { setToastMessage, setAlertProps } = useContext(TaxonomyContext); + const { setToastMessage, setAlertError } = useContext(TaxonomyContext); return (
@@ -22,7 +22,7 @@ const MockChildComponent = () => {