fix(api): a refused write is named, in the reader's language
Deep-review round 5, over round 4's own fixes.
- `notifyActionError` preferred the server's `detail` over the caller's translated fallback, and
`reportFailure` deliberately leaves 403/404/429 to the caller — so for exactly those statuses the
dozen call sites round 4 converted started printing whatever English a route hand-wrote into an
otherwise Hungarian UI ("No user with that email.", "Not subscribed to this channel", "Unknown
job"). A STRUCTURED detail is still preferred: `req` has already run it through `localizeDetail`,
so it is both localized and more specific. A plain string is not.
- The other half of the same defect, on the backend: round 4's `get_or_404(what="channel")` sweep
reached exactly ONE of the five identical sites in channels.py and shipped the HU/EN string with
it, so the Block button said it in Hungarian and the four controls beside it in English. All five
are converted, and the subscription / share-recipient / scheduler-job 404s with them (two new
reasons in both locales).
- `PlexCollectionEditor`'s add/remove, take-over and delete were try/finally with no catch — in the
very file round 4 edited to give `createNew` one. 403 and 404 are the caller's to report, so the
tick simply never appeared and a CONFIRMED "delete collection" did nothing, silently.
- `UploadDialog` is the only `uploadReq` caller and kept an empty catch whose comment still claimed
the global error modal had it. It had not since `reportFailure` became shared: a proxy 502 raised
only the self-clearing connection notice and a 413/403/404 nothing at all, so the dialog snapped
back to its pre-upload state with no error named. The user's own Cancel stays silent — and since
a cancel and a network drop both carry status 0, that difference is now a named `isAborted`
rather than a string literal at each end.
Regression tests for each (5 vitest, 2 pytest, plus two existing assertions corrected), all
mutation-checked: 6 mutations, each red on the assertion it is named after. vitest 1007,
pytest 1127.
This commit is contained in:
@@ -77,6 +77,11 @@ export default function PlexCollectionEditor({
|
||||
return next;
|
||||
});
|
||||
refresh();
|
||||
} catch (e) {
|
||||
// 403/404 are left to the caller by `req` (see reportFailure), so without this the tick
|
||||
// simply never appeared and the user clicked again — a collection deleted on the Plex server
|
||||
// or flipped read-only failed in silence (deep-review round 5).
|
||||
notifyActionError(e, t("plex.collection.editFailed"));
|
||||
} finally {
|
||||
setBusy(null);
|
||||
}
|
||||
@@ -88,6 +93,8 @@ export default function PlexCollectionEditor({
|
||||
try {
|
||||
await api.plexSetCollectionEditable(id, true);
|
||||
refresh();
|
||||
} catch (e) {
|
||||
notifyActionError(e, t("plex.collection.editFailed"));
|
||||
} finally {
|
||||
setBusy(null);
|
||||
}
|
||||
@@ -123,6 +130,10 @@ export default function PlexCollectionEditor({
|
||||
return next;
|
||||
});
|
||||
refresh();
|
||||
} catch (e) {
|
||||
// The worst of the three to lose: the user has just confirmed a destructive dialog, and the
|
||||
// row stayed where it was with nothing said.
|
||||
notifyActionError(e, t("plex.collection.deleteFailed"));
|
||||
} finally {
|
||||
setBusy(null);
|
||||
}
|
||||
|
||||
@@ -5,6 +5,8 @@ import { UploadCloud, File as FileIcon } from "lucide-react";
|
||||
import Modal from "./ui/Modal";
|
||||
import { Button } from "./ui/Button";
|
||||
import { api } from "../lib/api";
|
||||
import { isAborted } from "../lib/api/core";
|
||||
import { notifyActionError } from "../lib/actionErrors";
|
||||
import { invalidateDownloads } from "../lib/downloads";
|
||||
import { notify } from "../lib/notifications";
|
||||
import { navigateTo } from "../lib/nav";
|
||||
@@ -54,9 +56,15 @@ export default function UploadDialog({ onClose }: { onClose: () => void }) {
|
||||
action: { label: t("downloads.dialog.view"), onClick: () => navigateTo("downloads") },
|
||||
});
|
||||
onClose();
|
||||
} catch {
|
||||
// An aborted upload (Cancel) or a server rejection (cap/quota — already surfaced by the global
|
||||
// error modal, like every other req call). Re-arm the form so the user can retry or pick again.
|
||||
} catch (e) {
|
||||
// Everything except the user's own Cancel is said out loud. `uploadReq` shares `reportFailure`
|
||||
// with `req`, which deliberately leaves 403/404/429 to the caller and answers a 502/503/504
|
||||
// with only the transient "connection lost" notice — so this empty catch meant a proxy 502 on
|
||||
// a long upload, or an nginx 413, snapped the dialog back to its pre-upload state with nothing
|
||||
// named and no job in the list. The comment here claimed the global error modal had it; it
|
||||
// had not since the shared `reportFailure` landed (deep-review round 5).
|
||||
if (!isAborted(e)) notifyActionError(e, t("downloads.upload.failed"));
|
||||
// Re-arm the form so the user can retry or pick again.
|
||||
setPct(null);
|
||||
abortRef.current = null;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,146 @@
|
||||
// @vitest-environment jsdom
|
||||
//
|
||||
// A write that fails is named, wherever it fails. `req`/`uploadReq` deliberately leave 403/404/429
|
||||
// to the caller (see reportFailure) and answer a 502/503/504 with only the transient "connection
|
||||
// lost" notice — so a handler with no `catch` reports nothing at all. Round 4 swept a dozen such
|
||||
// call sites and missed these two: the collection editor's three siblings, in the very file it
|
||||
// edited, and the one `uploadReq` caller, whose empty catch still claimed the global error modal had
|
||||
// it (deep-review round 5).
|
||||
import { describe, it, expect, vi, beforeAll, beforeEach, afterEach } from "vitest";
|
||||
import { render, cleanup, screen, fireEvent, waitFor } from "@testing-library/react";
|
||||
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
||||
|
||||
vi.mock("react-i18next", async (importOriginal) => {
|
||||
const actual = await importOriginal<typeof import("react-i18next")>();
|
||||
return {
|
||||
...actual,
|
||||
useTranslation: () => ({ t: (key: string) => key, i18n: { language: "en" } }),
|
||||
};
|
||||
});
|
||||
vi.mock("./ConfirmProvider", () => ({ useConfirm: () => async () => true }));
|
||||
const { notify } = vi.hoisted(() => ({
|
||||
notify: vi.fn((_n: { level: string; message: string }) => 0),
|
||||
}));
|
||||
vi.mock("../lib/notifications", async (importOriginal) => ({
|
||||
...(await importOriginal<typeof import("../lib/notifications")>()),
|
||||
notify,
|
||||
}));
|
||||
|
||||
import PlexCollectionEditor from "./PlexCollectionEditor";
|
||||
import UploadDialog from "./UploadDialog";
|
||||
import { api, HttpError } from "../lib/api";
|
||||
|
||||
beforeAll(() => {
|
||||
class Stub {
|
||||
observe() {}
|
||||
unobserve() {}
|
||||
disconnect() {}
|
||||
takeRecords() {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
globalThis.ResizeObserver ??= Stub as unknown as typeof ResizeObserver;
|
||||
globalThis.IntersectionObserver ??= Stub as unknown as typeof IntersectionObserver;
|
||||
});
|
||||
beforeEach(() => notify.mockClear());
|
||||
afterEach(() => {
|
||||
cleanup();
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
const errors = () => notify.mock.calls.filter(([n]) => n.level === "error").map(([n]) => n.message);
|
||||
const client = () => new QueryClient({ defaultOptions: { queries: { retry: false } } });
|
||||
|
||||
describe("the Plex collection editor", () => {
|
||||
const collections = [
|
||||
{ id: "c1", title: "Marathon", can_edit: true, child_count: 3, smart: false },
|
||||
// Not marked "mine to edit" yet, so it offers the take-over button.
|
||||
{
|
||||
id: "c2",
|
||||
title: "Theirs",
|
||||
can_edit: false,
|
||||
child_count: 9,
|
||||
smart: false,
|
||||
source: "collection",
|
||||
},
|
||||
];
|
||||
const mount = () =>
|
||||
render(
|
||||
<QueryClientProvider client={client()}>
|
||||
<PlexCollectionEditor
|
||||
item={{ id: "m1", title: "A Movie" }}
|
||||
library="1"
|
||||
memberOf={[]}
|
||||
onClose={() => {}}
|
||||
onChanged={() => {}}
|
||||
/>
|
||||
</QueryClientProvider>,
|
||||
);
|
||||
|
||||
it("says so when adding to a collection that is gone, instead of nothing happening", async () => {
|
||||
vi.spyOn(api, "plexCollections").mockResolvedValue({ collections } as never);
|
||||
vi.spyOn(api, "plexCollectionAddItem").mockRejectedValue(
|
||||
new HttpError(404, "Unknown collection"),
|
||||
);
|
||||
mount();
|
||||
fireEvent.click(await screen.findByText("Marathon"));
|
||||
await waitFor(() => expect(errors()).toEqual(["plex.collection.editFailed"]));
|
||||
});
|
||||
|
||||
it("says so when enabling editing is refused", async () => {
|
||||
vi.spyOn(api, "plexCollections").mockResolvedValue({ collections } as never);
|
||||
vi.spyOn(api, "plexSetCollectionEditable").mockRejectedValue(
|
||||
new HttpError(403, "This collection is read-only"),
|
||||
);
|
||||
mount();
|
||||
fireEvent.click(await screen.findByText("plex.collEditor.makeEditable"));
|
||||
await waitFor(() => expect(errors()).toEqual(["plex.collection.editFailed"]));
|
||||
});
|
||||
|
||||
it("says so when a CONFIRMED delete does not happen", async () => {
|
||||
// The worst of the three to lose: the user has just confirmed a destructive dialog.
|
||||
vi.spyOn(api, "plexCollections").mockResolvedValue({ collections } as never);
|
||||
vi.spyOn(api, "plexDeleteCollection").mockRejectedValue(
|
||||
new HttpError(404, "Unknown collection"),
|
||||
);
|
||||
mount();
|
||||
fireEvent.click(await screen.findByText("plex.collEditor.delete"));
|
||||
await waitFor(() => expect(errors()).toEqual(["plex.collection.deleteFailed"]));
|
||||
});
|
||||
});
|
||||
|
||||
describe("the upload dialog", () => {
|
||||
const pick = () => {
|
||||
const input = document.querySelector('input[type="file"]') as HTMLInputElement;
|
||||
const file = new File(["x"], "clip.mp4", { type: "video/mp4" });
|
||||
Object.defineProperty(input, "files", { value: [file], configurable: true });
|
||||
fireEvent.change(input);
|
||||
};
|
||||
const mount = () =>
|
||||
render(
|
||||
<QueryClientProvider client={client()}>
|
||||
<UploadDialog onClose={() => {}} />
|
||||
</QueryClientProvider>,
|
||||
);
|
||||
|
||||
it("names an upload the server refused", async () => {
|
||||
// A 502 from the proxy raises only the self-clearing connection notice; the next successful poll
|
||||
// wipes it, and the dialog snapped back with no error and no job in the list.
|
||||
vi.spyOn(api, "uploadDownload").mockRejectedValue(new HttpError(502, "Bad Gateway"));
|
||||
mount();
|
||||
pick();
|
||||
fireEvent.click(screen.getByText("downloads.upload.submit"));
|
||||
await waitFor(() => expect(errors()).toEqual(["downloads.upload.failed"]));
|
||||
});
|
||||
|
||||
it("says nothing when the user cancelled it themselves", async () => {
|
||||
// A cancel is not a failure — and it rejects with the same status 0 a genuine network drop does,
|
||||
// which is why the difference is a named sentinel (`isAborted`) and not a status check.
|
||||
const upload = vi.spyOn(api, "uploadDownload").mockRejectedValue(new HttpError(0, "aborted"));
|
||||
mount();
|
||||
pick();
|
||||
fireEvent.click(screen.getByText("downloads.upload.submit"));
|
||||
await waitFor(() => expect(upload).toHaveBeenCalled());
|
||||
expect(errors()).toEqual([]);
|
||||
});
|
||||
});
|
||||
@@ -60,10 +60,25 @@ describe("usePlaylistActions — deleting a list", () => {
|
||||
expect(h.notify).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("does say one it was left to handle", async () => {
|
||||
vi.spyOn(api, "deletePlaylist").mockRejectedValue(new HttpError(404, "gone"));
|
||||
it("does say one it was left to handle — in the reader's language", async () => {
|
||||
// A plain-string `detail` is whatever English the route hand-wrote; the caller's own translated
|
||||
// fallback is the one a Hungarian reader can read (deep-review round 5).
|
||||
vi.spyOn(api, "deletePlaylist").mockRejectedValue(new HttpError(404, "Unknown playlist"));
|
||||
const { result } = mount();
|
||||
await act(() => result.current.remove(linked));
|
||||
expect(h.notify).toHaveBeenCalledWith(expect.objectContaining({ message: "gone" }));
|
||||
expect(h.notify).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ message: "playlists.deleteYtFailed" }),
|
||||
);
|
||||
});
|
||||
|
||||
it("prefers a STRUCTURED reason, which the request layer has already localized", async () => {
|
||||
vi.spyOn(api, "deletePlaylist").mockRejectedValue(
|
||||
new HttpError(404, "Ez a lista már nem létezik.", { code: "not_found", reason: "playlist" }),
|
||||
);
|
||||
const { result } = mount();
|
||||
await act(() => result.current.remove(linked));
|
||||
expect(h.notify).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ message: "Ez a lista már nem létezik." }),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user