Skip to content

Commit

Permalink
Merge pull request #1117 from InseeFr/1116-module-operation-gérer-pou…
Browse files Browse the repository at this point in the history
…r-les-famille

feat: display errors when back return 500 for Operations/Families
  • Loading branch information
PierreVasseur authored Jan 10, 2025
2 parents b3a7309 + fe13ce5 commit 300a23b
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 3 deletions.
77 changes: 77 additions & 0 deletions src/packages/components/errors-bloc/errors-bloc.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { render, screen } from '@testing-library/react';
import { describe, it, expect } from 'vitest';

import { ClientSideError, GlobalClientSideErrorBloc, ErrorBloc } from './index';

const mockD = {
errors: {
GlobalClientSideErrorBloc: 'A global client-side error occurred.',
SOME_ERROR_CODE: () => 'Error related to SOME_ERROR_CODE.',
},
};

describe('ClientSideError', () => {
it('renders error message when error is provided', () => {
render(
<ClientSideError error="<strong>Error occurred</strong>" id="error1" />,
);
screen.getByText('Error occurred');
});

it('does not render anything when error is not provided', () => {
render(<ClientSideError id="error1" />);
screen.queryByText('Error occurred');
});
});

describe('GlobalClientSideErrorBloc', () => {
it('renders global error message when clientSideErrors are provided', () => {
render(
<GlobalClientSideErrorBloc clientSideErrors={['error1']} D={mockD} />,
);
const errorElement = screen.getByRole('alert');
expect(errorElement).toHaveTextContent(
'A global client-side error occurred.',
);
});

it('does not render anything when clientSideErrors is undefined', () => {
render(<GlobalClientSideErrorBloc D={mockD} />);
const errorElement = screen.queryByRole('alert');
expect(errorElement).toBeNull();
});

it('does not render anything when clientSideErrors is an empty array', () => {
render(<GlobalClientSideErrorBloc clientSideErrors={[]} D={mockD} />);
const errorElement = screen.queryByRole('alert');
expect(errorElement).toBeNull();
});
});

describe('ErrorBloc', () => {
it('renders formatted errors for an array of error messages', () => {
const errors = [
JSON.stringify({ code: 'SOME_ERROR_CODE' }),
JSON.stringify({ status: 500 }),
'Plain error message',
];
render(<ErrorBloc error={errors} D={mockD} />);

screen.getByText('Error related to SOME_ERROR_CODE.');
screen.getByText(
'An error has occurred. Please contact the RMéS administration team and provide them with the following message: {"status":500}',
);
screen.getByText('Plain error message');
});

it('renders a single error message when error is a string', () => {
render(<ErrorBloc error="Plain error message" D={mockD} />);
screen.getByText('Plain error message');
});

it('renders fallback message when JSON parsing fails', () => {
const invalidError = 'Invalid JSON';
render(<ErrorBloc error={[invalidError]} D={mockD} />);
screen.getByText('Invalid JSON');
});
});
4 changes: 3 additions & 1 deletion src/packages/components/errors-bloc/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import NewDictionnary from '../../i18n';
import './errors-bloc.css';

export const ClientSideError = ({
Expand Down Expand Up @@ -55,6 +56,8 @@ export const ErrorBloc = ({
errorMsg = D.errors[parsedError.code](parsedError);
} else if (parsedError.message && D.errors[parsedError.message]) {
errorMsg = D.errors[parsedError.message](parsedError);
} else if (parsedError.status === 500) {
errorMsg = NewDictionnary.errors.serversideErrors['500'](e);
} else {
errorMsg = parsedError.message;
}
Expand All @@ -63,7 +66,6 @@ export const ErrorBloc = ({
}
return errorMsg;
});

return (
<>
{formattedErrors.map((e, index) => (
Expand Down
8 changes: 8 additions & 0 deletions src/packages/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ const dictionary = {
},
},
errors: {
serversideErrors: {
500: {
fr: (error: string) =>
`Une erreur s'est produite. Veuillez contacter l'équipe d'administration RMéS en lui communiquant le message suivant: ${error}`,
en: (error: string) =>
`An error has occurred. Please contact the RMéS administration team and provide them with the following message: ${error}`,
},
},
mandatoryProperty: {
fr: (propertyName: string) =>
`La propriété <strong>${propertyName}</strong> est obligatoire.`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useParams } from 'react-router-dom';

import { CheckSecondLang } from '@components/check-second-lang';
import { ErrorBloc } from '@components/errors-bloc';
import { Loading } from '@components/loading';
import { Loading, Publishing } from '@components/loading';
import { PageTitleBlock } from '@components/page-title-block';

import { OperationsApi } from '@sdk/operations-api';
Expand Down Expand Up @@ -39,7 +39,7 @@ export const Component = () => {
}, [family, id]);

if (!family) return <Loading />;
if (publishing) return <Loading text="publishing" />;
if (publishing) return <Publishing />;

return (
<div className="container">
Expand Down

0 comments on commit 300a23b

Please sign in to comment.