mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-12-01 20:36:21 -05:00
app: api helpers report failure instead of returning an empty value (#736)
Six helpers turned a non-OK response into an empty value, so their
consumers rendered a server error as "nothing here":
* getConfigBackups -> {dates: []}: "No backups available for this
config." A thrown request also left "Loading backups..." forever.
* getInventoryBackups -> []: "No backups yet."
* getDownloaders -> {downloaders: []} on a throw, the error body on a
non-OK response (which then crashed the .map): the RSS form's
Downloader select sat empty.
* searchMap -> {results: []}: "No map locations found".
* getMapSearchStatus -> {indexed: [], missing: []}: "No maps are
installed".
* flasherSearch -> [null, null], then `|| []`: "No .bin firmware
found".
Invariants now enforced and tested:
* On a non-OK response each helper returns undefined; an empty value
only ever comes from the server.
* Each consumer shows an error for a failed or thrown fetch and keeps
its "none" message for a confirmed empty result. The Downloader
select is disabled and says it is loading or could not fetch. A
failed map status is unknown, never "no maps are installed".
* The map search tab loads from the first render, and a query shorter
than the two characters the request needs now says so instead of
"No map locations found".
Archive browsers and recent tags are left as they are: a failed fetch
there hides an optional picker or suggestion list without claiming
anything.
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
20541d7184
commit
63ddad69e8
13 changed files with 375 additions and 44 deletions
43
app/src/components/admin/ConfigBackups.test.js
Normal file
43
app/src/components/admin/ConfigBackups.test.js
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
import React from 'react';
|
||||
import {render, screen} from '../../test-utils';
|
||||
import {BackupsModal} from './Configs';
|
||||
import {getConfigBackups} from '../../api';
|
||||
|
||||
// The config backups modal says "No backups available" only once the server confirmed there are
|
||||
// none; a failed fetch is an error, and a thrown one must not leave the loader up forever.
|
||||
|
||||
jest.mock('../../api', () => ({
|
||||
...jest.requireActual('../../api'),
|
||||
getConfigBackups: jest.fn(),
|
||||
postConfigBackupPreview: jest.fn(() => new Promise(() => {
|
||||
})),
|
||||
}));
|
||||
|
||||
const NONE = /No backups available/;
|
||||
const FAILED = /Could not fetch backups/;
|
||||
|
||||
const renderModal = () => render(
|
||||
<BackupsModal open fileName='wrolpi.yaml' onClose={jest.fn()} fetchConfigs={jest.fn()}/>,
|
||||
);
|
||||
|
||||
beforeEach(() => getConfigBackups.mockReset());
|
||||
|
||||
test('empty: says there are no backups', async () => {
|
||||
getConfigBackups.mockResolvedValue({dates: []});
|
||||
renderModal();
|
||||
expect(await screen.findByText(NONE)).toBeInTheDocument();
|
||||
});
|
||||
|
||||
test('failed: shows an error, not "No backups available"', async () => {
|
||||
getConfigBackups.mockResolvedValue(undefined);
|
||||
renderModal();
|
||||
expect(await screen.findByText(FAILED)).toBeInTheDocument();
|
||||
expect(screen.queryByText(NONE)).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
test('thrown: shows an error, not a loader forever', async () => {
|
||||
getConfigBackups.mockRejectedValue(new Error('down'));
|
||||
renderModal();
|
||||
expect(await screen.findByText(FAILED)).toBeInTheDocument();
|
||||
expect(screen.queryByText(/Loading backups/)).not.toBeInTheDocument();
|
||||
});
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
import {APIButton} from "../Common";
|
||||
import {APIButton, ErrorMessage} from "../Common";
|
||||
import {Button, Header, Icon, IconButton, Loader, Loading, Modal, Placeholder, Table, Tooltip} from "../ui";
|
||||
import React, {useEffect, useState} from "react";
|
||||
import {getConfigText, getConfigBackups, postConfigBackupImport, postConfigBackupPreview} from "../../api";
|
||||
|
|
@ -117,9 +117,11 @@ function BackupDateRow({date, previews, previewsLoaded, onSelect}) {
|
|||
</Table.Row>;
|
||||
}
|
||||
|
||||
function BackupsModal({open, onClose, fileName, fetchConfigs}) {
|
||||
export function BackupsModal({open, onClose, fileName, fetchConfigs}) {
|
||||
const [dates, setDates] = useState([]);
|
||||
const [datesLoading, setDatesLoading] = useState(true);
|
||||
// The backups could not be fetched; distinct from the server confirming there are none.
|
||||
const [datesFailed, setDatesFailed] = useState(false);
|
||||
const [previews, setPreviews] = useState({});
|
||||
const [previewsLoaded, setPreviewsLoaded] = useState({});
|
||||
const [selectedAction, setSelectedAction] = useState(null); // {date, mode, preview}
|
||||
|
|
@ -130,17 +132,30 @@ function BackupsModal({open, onClose, fileName, fetchConfigs}) {
|
|||
if (!open) return;
|
||||
let cancelled = false;
|
||||
setDatesLoading(true);
|
||||
setDatesFailed(false);
|
||||
setDates([]);
|
||||
setPreviews({});
|
||||
setPreviewsLoaded({});
|
||||
setSelectedAction(null);
|
||||
setApplying(false);
|
||||
|
||||
getConfigBackups(fileName).then(result => {
|
||||
if (cancelled) return;
|
||||
setDates(result?.dates || []);
|
||||
setDatesLoading(false);
|
||||
});
|
||||
getConfigBackups(fileName)
|
||||
.then(result => {
|
||||
if (cancelled) return;
|
||||
// The api helper returns undefined on a non-OK response.
|
||||
if (result) {
|
||||
setDates(result.dates || []);
|
||||
} else {
|
||||
setDatesFailed(true);
|
||||
}
|
||||
})
|
||||
.catch(e => {
|
||||
console.error(e);
|
||||
if (!cancelled) setDatesFailed(true);
|
||||
})
|
||||
.finally(() => {
|
||||
if (!cancelled) setDatesLoading(false);
|
||||
});
|
||||
|
||||
return () => { cancelled = true; };
|
||||
}, [open, fileName]);
|
||||
|
|
@ -253,6 +268,8 @@ function BackupsModal({open, onClose, fileName, fetchConfigs}) {
|
|||
<Modal.Content>
|
||||
{datesLoading
|
||||
? <Loading>Loading backups...</Loading>
|
||||
: datesFailed
|
||||
? <ErrorMessage>Could not fetch backups for this config.</ErrorMessage>
|
||||
: dates.length === 0
|
||||
? <p>No backups available for this config.</p>
|
||||
: <div style={{maxHeight: '400px', overflowY: 'auto'}}>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue