Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
Address /do:review findings on throwApiError
Bryandero98471a447e2888
diff --git a/client/src/services/apiCore.js b/client/src/services/apiCore.jsindex 93b2e9e6c7..d5fff2453b 100644--- a/client/src/services/apiCore.js+++ b/client/src/services/apiCore.js@@ -27,7 +27,12 @@ export function maybeRedirectToLogin(response, error) {// own (with `silent` support); the streaming/blob callers that bypass request()// to read a raw Response deliberately surface their own errors instead.export async function throwApiError(response) {- const error = await response.json().catch(() => ({ error: `HTTP ${response.status}` }));+ // A valid JSON body that isn't an object (e.g. a bare `null`) parses+ // successfully but has no `.error`/`.code`/`.context` to read — fall back+ // to the same HTTP-status shape used when the body isn't JSON at all.+ const parsedError = await response.json().catch(() => null);+ const error =+ parsedError && typeof parsedError === 'object' ? parsedError : { error: `HTTP ${response.status}` };// Auth gate (server: services/authGate.js) returns 401 with code AUTH_REQUIRED// for any /api request without a valid session. Bounce to /login so the// user can re-authenticate; skip if we're already there.diff --git a/client/src/services/apiCore.test.js b/client/src/services/apiCore.test.jsindex c81deaa9e7..d90b50075a 100644--- a/client/src/services/apiCore.test.js+++ b/client/src/services/apiCore.test.js@@ -50,18 +50,23 @@ describe('throwApiError', () => {it('omits context when the server did not send any', async () => {const response = makeResponse({ status: 500, json: { error: 'boom' } });- try {- await throwApiError(response);- throw new Error('expected throwApiError to throw');- } catch (err) {- expect(err.context).toBeUndefined();- }+ const err = await throwApiError(response).catch((e) => e);+ expect(err).toBeInstanceOf(Error);+ expect(err.context).toBeUndefined();});it('falls back to an HTTP-status message when the body is not JSON', async () => {const response = makeResponse({ status: 502 }); // json:null → makeResponse's json() rejectsawait expect(throwApiError(response)).rejects.toMatchObject({ message: 'HTTP 502' });});++ it('falls back to an HTTP-status message when the body is valid JSON but not an object', async () => {+ // response.json() resolves successfully with `null` here — a real case+ // (an endpoint that responds 500 with a bare `null` body) distinct from+ // "body isn't JSON at all" above, which rejects instead of resolving.+ const response = { status: 500, ok: false, json: async () => null };+ await expect(throwApiError(response)).rejects.toMatchObject({ message: 'HTTP 500' });+ });});describe('request() error path (now delegating to throwApiError)', () => {@@ -74,11 +79,12 @@ describe('request() error path (now delegating to throwApiError)', () => {expect(toast.error).toHaveBeenCalledWith('bad input');});- it('does not toast for PLATFORM_UNAVAILABLE errors (warns instead)', async () => {+ it('warns instead of toasting an error for PLATFORM_UNAVAILABLE', async () => {global.fetch.mockResolvedValue(makeResponse({ status: 503, json: { error: 'offline', code: 'PLATFORM_UNAVAILABLE' } }),);await expect(request('/x')).rejects.toMatchObject({ code: 'PLATFORM_UNAVAILABLE' });+ expect(toast).toHaveBeenCalledWith('offline', { icon: '⚠️' });expect(toast.error).not.toHaveBeenCalled();});diff --git a/client/src/services/apiMedia.test.js b/client/src/services/apiMedia.test.jsindex 34c12a1c2f..069cc25f89 100644--- a/client/src/services/apiMedia.test.js+++ b/client/src/services/apiMedia.test.js@@ -4,31 +4,40 @@ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';// header, which the JSON-oriented request() helper can't surface). saveCleanResult// goes through request(). Mock both layers.const request = vi.fn();-vi.mock('./apiCore.js', () => ({- request: (...a) => request(...a),- API_BASE: '/api',- maybeRedirectToLogin: vi.fn(),- // Mirrors the real apiCore.throwApiError (minus the redirect side effect,- // already covered by the maybeRedirectToLogin mock above) so assertions- // against `.code`/`.status`/`.context`/message keep working unchanged.- throwApiError: vi.fn(async (response) => {+vi.mock('./apiCore.js', () => {+ // Mirrors the real apiCore.throwApiError, including the maybeRedirectToLogin+ // call — routing through the SAME mock the test file asserts against, so a+ // regression in the consumer-side auth-redirect contract actually fails+ // these tests instead of a mock that silently drops the side effect.+ const maybeRedirectToLogin = vi.fn();+ const throwApiError = vi.fn(async (response) => {const error = await response.json().catch(() => ({ error: `HTTP ${response.status}` }));+ maybeRedirectToLogin(response, error);const err = new Error(error.error || `HTTP ${response.status}`);err.code = error?.code;err.status = response.status;if (error?.context) err.context = error.context;throw err;- }),-}));+ });+ return {+ request: (...a) => request(...a),+ API_BASE: '/api',+ maybeRedirectToLogin,+ throwApiError,+ };+});let cleanImage;let fetchCleanResult;let saveCleanResult;+let maybeRedirectToLogin;beforeEach(async () => {vi.resetModules();request.mockReset();({ cleanImage, fetchCleanResult, saveCleanResult } = await import('./apiMedia.js'));+ ({ maybeRedirectToLogin } = await import('./apiCore.js'));+ maybeRedirectToLogin.mockClear();global.fetch = vi.fn();});@@ -77,6 +86,13 @@ describe('cleanImage', () => {global.fetch.mockResolvedValue(makeResponse({ status: 400, ok: false, json: { error: 'No FLUX', code: 'REGEN_BACKEND_UNAVAILABLE' } }));await expect(cleanImage(fakeFile, { diffusion: 'gpu' })).rejects.toMatchObject({ code: 'REGEN_BACKEND_UNAVAILABLE' });});++ it('honors session expiry the same way request() does (401 AUTH_REQUIRED)', async () => {+ const response = makeResponse({ status: 401, ok: false, json: { error: 'auth required', code: 'AUTH_REQUIRED' } });+ global.fetch.mockResolvedValue(response);+ await expect(cleanImage(fakeFile, { diffusion: 'gpu' })).rejects.toMatchObject({ code: 'AUTH_REQUIRED' });+ expect(maybeRedirectToLogin).toHaveBeenCalledWith(response, expect.objectContaining({ code: 'AUTH_REQUIRED' }));+ });});describe('fetchCleanResult', () => {