fix(cowork-service): preserve budget policy auth errors

This commit is contained in:
Saravana Achu Mac 2026-04-03 14:09:52 -07:00
parent 6c2e9a67ca
commit 6936c007c7
2 changed files with 40 additions and 6 deletions

View File

@ -133,6 +133,22 @@ describe('usage proxy routes', () => {
'Bearer test-token' 'Bearer test-token'
); );
}); });
it('forwards budget-policy authorization failures', async () => {
mockFetch.mockResolvedValue({
ok: false,
status: 403,
json: async () => ({ error: 'Admin access required' }),
});
const res = await app.inject({
method: 'GET',
url: '/api/usage/budget-policy',
});
expect(res.statusCode).toBe(403);
expect(JSON.parse(res.payload).error).toBe('Admin access required');
});
}); });
describe('PUT /api/usage/budget-policy', () => { describe('PUT /api/usage/budget-policy', () => {

View File

@ -54,7 +54,10 @@ async function listBudgetPolicies(
headers: buildProxyHeaders(req), headers: buildProxyHeaders(req),
}); });
if (!res.ok) { if (!res.ok) {
throw new Error(`Platform returned ${res.status}`); const body = (await res.json().catch(() => null)) as { error?: string } | null;
const error = new Error(body?.error ?? `Platform returned ${res.status}`);
(error as Error & { statusCode?: number }).statusCode = res.status;
throw error;
} }
return (await res.json()) as PlatformBudgetPolicy[]; return (await res.json()) as PlatformBudgetPolicy[];
} }
@ -89,7 +92,10 @@ async function upsertPolicy(
body: JSON.stringify(body), body: JSON.stringify(body),
}); });
if (!res.ok) { if (!res.ok) {
throw new Error(`Platform returned ${res.status}`); const body = (await res.json().catch(() => null)) as { error?: string } | null;
const error = new Error(body?.error ?? `Platform returned ${res.status}`);
(error as Error & { statusCode?: number }).statusCode = res.status;
throw error;
} }
return (await res.json()) as PlatformBudgetPolicy; return (await res.json()) as PlatformBudgetPolicy;
} }
@ -170,8 +176,14 @@ export async function usageRoutes(app: FastifyInstance) {
}; };
} catch (err) { } catch (err) {
req.log.warn({ err }, 'Failed to load budget policy from platform-service'); req.log.warn({ err }, 'Failed to load budget policy from platform-service');
reply.code(502); const statusCode =
return { error: 'Platform-service unavailable' }; err instanceof Error && 'statusCode' in err && typeof err.statusCode === 'number'
? err.statusCode
: 502;
reply.code(statusCode);
return {
error: err instanceof Error ? err.message : 'Platform-service unavailable',
};
} }
}); });
@ -207,8 +219,14 @@ export async function usageRoutes(app: FastifyInstance) {
}; };
} catch (err) { } catch (err) {
req.log.warn({ err }, 'Failed to save budget policy to platform-service'); req.log.warn({ err }, 'Failed to save budget policy to platform-service');
reply.code(502); const statusCode =
return { error: 'Platform-service unavailable' }; err instanceof Error && 'statusCode' in err && typeof err.statusCode === 'number'
? err.statusCode
: 502;
reply.code(statusCode);
return {
error: err instanceof Error ? err.message : 'Platform-service unavailable',
};
} }
}); });
} }