Improve API fallback robustness and avoid throwing to enable demo fallback silently
cgen-b98e0911f28f4d11add2635b34ab43b3
This commit is contained in:
parent
6fe9b6aded
commit
011ad98ee0
1 changed files with 12 additions and 3 deletions
|
|
@ -263,14 +263,23 @@ export const communityService = {
|
||||||
try {
|
try {
|
||||||
const resp = await fetch(`/api/posts?limit=${encodeURIComponent(String(limit))}`);
|
const resp = await fetch(`/api/posts?limit=${encodeURIComponent(String(limit))}`);
|
||||||
if (resp.ok) {
|
if (resp.ok) {
|
||||||
const payload = await resp.json();
|
const ct = resp.headers.get("content-type") || "";
|
||||||
return (Array.isArray(payload) ? payload : []) as CommunityPost[];
|
if (ct.includes("application/json") || ct.includes("json")) {
|
||||||
|
const payload = await resp.json();
|
||||||
|
return (Array.isArray(payload) ? payload : []) as CommunityPost[];
|
||||||
|
} else {
|
||||||
|
const text = await resp.text();
|
||||||
|
console.warn("API fallback returned non-JSON content-type:", ct, text.slice(0, 120));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
console.warn("API fallback /api/posts not ok:", resp.status, resp.statusText);
|
||||||
}
|
}
|
||||||
} catch (apiErr) {
|
} catch (apiErr) {
|
||||||
console.error("API fallback for getPosts failed:", (apiErr as any)?.message || apiErr);
|
console.error("API fallback for getPosts failed:", (apiErr as any)?.message || apiErr);
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new Error("Unable to load posts");
|
// As a last resort, return an empty array so callers can apply their own demo fallback without surfacing an error toast
|
||||||
|
return [] as CommunityPost[];
|
||||||
},
|
},
|
||||||
|
|
||||||
async createPost(
|
async createPost(
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue