Skip to content

Commit bc5c41a

Browse files
authored
handle user quotas errors (#449)
Signed-off-by: Abdelsalem <[email protected]>
1 parent 9793026 commit bc5c41a

File tree

8 files changed

+48
-2
lines changed

8 files changed

+48
-2
lines changed

src/components/dialogs/create-case-dialog/create-case-dialog.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import {
3434
keyGenerator,
3535
useSnackMessage,
3636
} from '@gridsuite/commons-ui';
37+
import { handleMaxElementsExceededError } from '../../utils/rest-errors';
3738

3839
interface IFormData {
3940
[FieldConstants.CASE_NAME]: string;
@@ -92,7 +93,9 @@ const CreateCaseDialog: React.FunctionComponent<CreateCaseDialogProps> = ({
9293
})
9394
.then(onClose)
9495
.catch((err) => {
95-
if (err?.status === HTTP_UNPROCESSABLE_ENTITY_STATUS) {
96+
if (handleMaxElementsExceededError(err, snackError)) {
97+
return;
98+
} else if (err?.status === HTTP_UNPROCESSABLE_ENTITY_STATUS) {
9699
snackError({
97100
messageId: 'invalidFormatOrName',
98101
headerId: 'caseCreationError',

src/components/dialogs/create-study-dialog/create-study-dialog.jsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import {
4343
} from './create-study-dialog-utils';
4444
import { yupResolver } from '@hookform/resolvers/yup/dist/yup';
4545
import PrefilledNameInput from '../commons/prefilled-name-input';
46+
import { handleMaxElementsExceededError } from '../../utils/rest-errors';
4647

4748
const STRING_LIST = 'STRING_LIST';
4849

@@ -218,6 +219,9 @@ const CreateStudyDialog = ({ open, onClose, providedExistingCase }) => {
218219
onClose();
219220
})
220221
.catch((error) => {
222+
if (handleMaxElementsExceededError(error, snackError)) {
223+
return;
224+
}
221225
snackError({
222226
messageTxt: error.message,
223227
headerId: 'studyCreationError',

src/components/menus/content-contextual-menu.jsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ import ExportCaseDialog from '../dialogs/export-case-dialog';
5858
import { setSelectionForCopy } from '../../redux/actions';
5959
import { useParameterState } from '../dialogs/parameters-dialog';
6060
import { PARAM_LANGUAGE } from '../../utils/config-params';
61+
import { handleMaxElementsExceededError } from '../utils/rest-errors';
6162

6263
const ContentContextualMenu = (props) => {
6364
const {
@@ -226,6 +227,9 @@ const ContentContextualMenu = (props) => {
226227
undefined,
227228
activeElement.type
228229
).catch((error) => {
230+
if (handleMaxElementsExceededError(error, snackError)) {
231+
return;
232+
}
229233
handleDuplicateError(error.message);
230234
});
231235
break;

src/components/menus/directory-tree-contextual-menu.jsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import ContingencyListCreationDialog from '../dialogs/contingency-list/creation/
4343
import CreateCaseDialog from '../dialogs/create-case-dialog/create-case-dialog';
4444
import { useParameterState } from '../dialogs/parameters-dialog';
4545
import { PARAM_LANGUAGE } from '../../utils/config-params';
46+
import { handleMaxElementsExceededError } from '../utils/rest-errors';
4647

4748
const DirectoryTreeContextualMenu = (props) => {
4849
const { directory, open, onClose, openDialog, setOpenDialog, ...others } =
@@ -141,6 +142,9 @@ const DirectoryTreeContextualMenu = (props) => {
141142
selectionForCopy.typeItem,
142143
undefined
143144
).catch((error) => {
145+
if (handleMaxElementsExceededError(error, snackError)) {
146+
return;
147+
}
144148
handlePasteError(error);
145149
});
146150
break;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Copyright (c) 2024, RTE (http://www.rte-france.com)
3+
* This Source Code Form is subject to the terms of the Mozilla Public
4+
* License, v. 2.0. If a copy of the MPL was not distributed with this
5+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
6+
*/
7+
import { HTTP_MAX_ELEMENTS_EXCEEDED_MESSAGE } from 'utils/UIconstants';
8+
9+
interface CustomError extends Error {
10+
status?: number;
11+
}
12+
13+
export const handleMaxElementsExceededError = (
14+
error: CustomError,
15+
snackError: Function
16+
): boolean => {
17+
if (
18+
error.status === 403 &&
19+
error.message.includes(HTTP_MAX_ELEMENTS_EXCEEDED_MESSAGE)
20+
) {
21+
let limit = error.message.split(/[: ]+/).pop();
22+
snackError({
23+
messageId: 'maxElementExceededError',
24+
messageValues: { limit: limit },
25+
});
26+
return true;
27+
}
28+
return false;
29+
};

src/translations/en.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"ID": "ID",
2222
"createNewStudy": "Create a study",
2323
"studyCreationError": "Error when creating study: {studyName}",
24+
"maxElementExceededError": "You have reached your user quota for cases and studies, therefore you cannot import further cases or create further studies for now. Please delete old cases or studies in order to go below the limit of {limit} elements",
2425
"directoryUpdateWarning": "Could not update subs (and content) of: {directoryUuid}",
2526
"renameElement": "Rename the element",
2627
"renameElementMsg": "Element new name",

src/translations/fr.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"ID": "ID",
2222
"createNewStudy": "Créer une étude",
2323
"studyCreationError": "Erreur lors de la création de l'étude: {studyName}",
24-
"directoryUpdateWarning": "Impossible de mettre à jour les sous-dossiers (et le contenu) de: {directoryUuid}",
24+
"maxElementExceededError": "Vous avez atteint votre quota utilisateur en termes de situations et d'études, vous ne pouvez donc plus importer des situations ou créer des études pour le moment. Merci de supprimer des situations ou études anciennes afin de redescendre sous le seuil de {limit} éléments.",
2525
"renameElement": "Renommer l'élément",
2626
"renameElementMsg": "Nouveau nom de l'élément",
2727
"renameElementNotAllowedError": "Vous ne pouvez pas renommer un élément dont vous n'êtes pas propriétaire",

src/utils/UIconstants.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,4 @@ export const DialogsId = {
3535
};
3636
export const HTTP_UNPROCESSABLE_ENTITY_STATUS = 422;
3737
export const HTTP_CONNECTION_FAILED_MESSAGE = 'failed: Connection refused';
38+
export const HTTP_MAX_ELEMENTS_EXCEEDED_MESSAGE = 'MAX_ELEMENTS_EXCEEDED';

0 commit comments

Comments
 (0)