bookmarks: don't fabricate a local save when the server rejects it (#121)
All checks were successful
Build + push images (Forgejo registry) / build-push (backend) (push) Successful in 8s
Deploy / deploy (backend) (push) Successful in 8s
secrets-guard / encrypted (push) Successful in 6s
shell-lint / shellcheck (push) Successful in 12s
Build + push images (Forgejo registry) / build-push (frontend) (push) Successful in 41s
Deploy / deploy (frontend) (push) Successful in 53s
PR build (required check) / changes (pull_request) Successful in 6s
secrets-guard / encrypted (pull_request) Successful in 5s
PR build (required check) / build-backend (pull_request) Has been skipped
shell-lint / shellcheck (pull_request) Successful in 6s
PR build (required check) / validate-observability (pull_request) Has been skipped
PR build (required check) / build-frontend (pull_request) Successful in 41s
PR build (required check) / gate (pull_request) Successful in 2s
All checks were successful
Build + push images (Forgejo registry) / build-push (backend) (push) Successful in 8s
Deploy / deploy (backend) (push) Successful in 8s
secrets-guard / encrypted (push) Successful in 6s
shell-lint / shellcheck (push) Successful in 12s
Build + push images (Forgejo registry) / build-push (frontend) (push) Successful in 41s
Deploy / deploy (frontend) (push) Successful in 53s
PR build (required check) / changes (pull_request) Successful in 6s
secrets-guard / encrypted (pull_request) Successful in 5s
PR build (required check) / build-backend (pull_request) Has been skipped
shell-lint / shellcheck (pull_request) Successful in 6s
PR build (required check) / validate-observability (pull_request) Has been skipped
PR build (required check) / build-frontend (pull_request) Successful in 41s
PR build (required check) / gate (pull_request) Successful in 2s
This commit is contained in:
parent
b57e280ad6
commit
3ec72ca75b
2 changed files with 24 additions and 3 deletions
|
|
@ -111,6 +111,10 @@ function buildStar() {
|
||||||
try {
|
try {
|
||||||
if (existing) await bookmarks.remove(existing.id);
|
if (existing) await bookmarks.remove(existing.id);
|
||||||
else await bookmarks.add(loc.lat, loc.lon, currentLabel());
|
else await bookmarks.add(loc.lat, loc.lon, currentLabel());
|
||||||
|
} catch (e) {
|
||||||
|
// A real server rejection (e.g. the per-user cap) — bookmarks.add() no
|
||||||
|
// longer fakes a local save for this, so say why nothing changed.
|
||||||
|
alert(e.message || "Couldn't save this location.");
|
||||||
} finally {
|
} finally {
|
||||||
btn.disabled = false;
|
btn.disabled = false;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -97,17 +97,34 @@ async function syncFromServer(preSyncLocal) {
|
||||||
notify();
|
notify();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Save (or re-save with a new label) the given spot. Upserts either way. */
|
/** Save (or re-save with a new label) the given spot. Upserts either way.
|
||||||
|
*
|
||||||
|
* Throws if signed in and the server explicitly rejects the request (e.g. the
|
||||||
|
* 409 per-user cap, a 422 on bad input) — that is a real failure, not the
|
||||||
|
* "offline" case below, and must not be papered over with a local-only
|
||||||
|
* bookmark that doesn't actually exist server-side and would just vanish,
|
||||||
|
* unexplained, on the next sync. A genuine network failure (no response at
|
||||||
|
* all) still falls through to the local-only save so the star visibly
|
||||||
|
* sticks even offline. */
|
||||||
export async function add(lat, lon, label) {
|
export async function add(lat, lon, label) {
|
||||||
const id = cellId(lat, lon);
|
const id = cellId(lat, lon);
|
||||||
if (signedIn) {
|
if (signedIn) {
|
||||||
|
let res = null;
|
||||||
try {
|
try {
|
||||||
const res = await apiFetch(uv("bookmarks"), { method: "POST", json: { lat, lon, label: label || undefined } });
|
res = await apiFetch(uv("bookmarks"), { method: "POST", json: { lat, lon, label: label || undefined } });
|
||||||
|
} catch (e) {
|
||||||
|
// offline / network failure: fall through to the local-only path below.
|
||||||
|
}
|
||||||
|
if (res) {
|
||||||
if (res.ok) {
|
if (res.ok) {
|
||||||
const rows = await fetchServerList();
|
const rows = await fetchServerList();
|
||||||
if (rows != null) { cache = rows.map(fromServerRow); writeLocal(cache); notify(); return find(lat, lon); }
|
if (rows != null) { cache = rows.map(fromServerRow); writeLocal(cache); notify(); return find(lat, lon); }
|
||||||
|
} else {
|
||||||
|
let msg = `Couldn't save this location (${res.status}).`;
|
||||||
|
try { const d = await res.json(); if (typeof d.detail === "string") msg = d.detail; } catch (e) { /* no body */ }
|
||||||
|
throw new Error(msg);
|
||||||
}
|
}
|
||||||
} catch (e) { /* fall through so the star still visibly sticks, even offline */ }
|
}
|
||||||
}
|
}
|
||||||
let b = cache.find((x) => cellId(x.lat, x.lon) === id);
|
let b = cache.find((x) => cellId(x.lat, x.lon) === id);
|
||||||
if (b) { if (label) b.label = label; }
|
if (b) { if (label) b.label = label; }
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue