From 4b300b34f1a403047c9a7ae7f09bd27b1554cba1 Mon Sep 17 00:00:00 2001 From: Pavlo Strunkin Date: Sat, 24 Oct 2026 16:26:47 +0300 Subject: [PATCH] refactor: address SonarCloud findings on #438 - handleResponse: a successful response that is not JSON goes through the one rejection path instead of a second Promise.reject - applying an ignore area to the whole build is one flat promise chain - zip entry numbering uses a while loop over the name it tests --- src/_helpers/service.helpers.ts | 12 +++++------ .../TestDetailsDialog/TestDetailsModal.tsx | 21 ++++++++++--------- src/services/static.service.ts | 4 +++- 3 files changed, 20 insertions(+), 17 deletions(-) diff --git a/src/_helpers/service.helpers.ts b/src/_helpers/service.helpers.ts index c95b5789..0bd14d8e 100644 --- a/src/_helpers/service.helpers.ts +++ b/src/_helpers/service.helpers.ts @@ -31,7 +31,7 @@ export function handleResponse(response: Response) { isJson = false; } - if (!response.ok) { + if (!response.ok || !isJson) { if (response.status === 401) { // auto logout if 401 response returned from api usersService.logout(); @@ -41,15 +41,15 @@ export function handleResponse(response: Response) { } } - const error = data?.message || response.statusText; + // a successful status with a body that is not JSON is no answer either + const error = + data?.message || + (response.ok ? "Unexpected response from the server" : "") || + response.statusText; return Promise.reject(error); } - if (!isJson) { - return Promise.reject("Unexpected response from the server"); - } - return data; }); } diff --git a/src/components/TestDetailsDialog/TestDetailsModal.tsx b/src/components/TestDetailsDialog/TestDetailsModal.tsx index 8c77fcbf..5b00d2e4 100644 --- a/src/components/TestDetailsDialog/TestDetailsModal.tsx +++ b/src/components/TestDetailsDialog/TestDetailsModal.tsx @@ -352,16 +352,17 @@ const TestDetailsModal: React.FunctionComponent = ({ // returned, so a failure reaches the catch below: the progress bar // stayed up for good and nobody heard about the error - return testRunService.addIgnoreAreas(data).then(() => { - setProcessing(false); - setSelectedRectId(undefined); - enqueueSnackbar( - "Ignore areas are updated in all images in this build.", - { - variant: "success", - }, - ); - }); + return testRunService.addIgnoreAreas(data); + }) + .then(() => { + setProcessing(false); + setSelectedRectId(undefined); + enqueueSnackbar( + "Ignore areas are updated in all images in this build.", + { + variant: "success", + }, + ); }) .catch((error) => { enqueueSnackbar(`There was an error : ${error}`, { diff --git a/src/services/static.service.ts b/src/services/static.service.ts index 92b315c2..70c23903 100644 --- a/src/services/static.service.ts +++ b/src/services/static.service.ts @@ -55,7 +55,9 @@ async function downloadAsZip( const taken = new Set(); const entryNames = items.map((item) => { let entryName = `${item.filename}.png`; - for (let copy = 2; taken.has(entryName); copy++) { + let copy = 1; + while (taken.has(entryName)) { + copy += 1; entryName = `${item.filename} (${copy}).png`; } taken.add(entryName);