Skip to content

714: Show error is we fail to reset defences #927

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions frontend/src/components/MainComponent/MainComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,13 @@ function MainComponent({
configItemId,
currentLevel
);
if (!resetDefence) {
addChatMessage({
message: 'Failed to reset defence configuration. Please try again.',
type: 'ERROR_MSG',
});
return;
}
addConfigUpdateToChat(defenceId, 'reset');
// update state
const newDefences = defences.map((defence) => {
Expand Down
14 changes: 10 additions & 4 deletions frontend/src/service/defenceService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,19 @@ async function resetDefenceConfigItem(
defenceId: DEFENCE_ID,
configItemId: DEFENCE_CONFIG_ITEM_ID,
level: LEVEL_NAMES
): Promise<DefenceResetResponse> {
const response = await post(`${PATH}/resetConfig`, {
): Promise<DefenceResetResponse | null> {
return post(`${PATH}/resetConfig`, {
defenceId,
configItemId,
level,
});
return (await response.json()) as DefenceResetResponse;
}).then(
Copy link
Member

@chriswilty chriswilty Jan 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a mix of promises and async/awaits here. Prefer async/await wherever possible (and here it is possible).

Note that fetch does not throw an error for non-200 status codes, so this will fail when the service (legitimately) returns text/plain error messages, as the body then is not JSON. I appreciate that a) that was already happening, and b) the acceptance criteria specified only what should happen when the server is unavailable (which does indeed result in an error thrown) but we have the opportunity to make this more robust if we check for response.ok ...

As this project is so infrequently updated, may I ask for a little more from you? Firstly, use async/await and try/catch for consistency, and secondly, could you check for response.ok and if false, then console.warn the response text (and then return null)?

async (response) => {
return (await response.json()) as DefenceResetResponse;
},
() => {
return null;
}
);
}

function validatePositiveNumberConfig(value: string) {
Expand Down
Loading