fix(capacity): Fix availability load/save error handling

- Clone response before reading to prevent body consumption issues
- Add better error logging in API client
- Surface backend error messages in capacity page
- Import ApiError type for proper error handling

Test Results:
- Backend: 15 capacity tests passed 
- Frontend Unit: 10 passed 
- E2E: 130 passed, 24 skipped 

Refs: openspec/changes/headroom-foundation
This commit is contained in:
2026-02-19 19:30:57 -05:00
parent d6b7215f93
commit 2f8ef8f2b3
2 changed files with 25 additions and 6 deletions

View File

@@ -221,13 +221,21 @@ export async function apiRequest<T>(endpoint: string, options: ApiRequestOptions
// Handle API response
async function handleResponse(response: Response): Promise<Response> {
const contentType = response.headers?.get?.('content-type') || response.headers?.get?.('Content-Type');
const contentType =
response.headers?.get?.('content-type') || response.headers?.get?.('Content-Type');
const isJson = contentType && contentType.includes('application/json');
if (!response.ok) {
const data = isJson ? await response.json() : await response.text();
const payloadResponse = response.clone();
const data = isJson ? await payloadResponse.json() : await payloadResponse.text();
const errorData = typeof data === 'object' ? data : { message: data };
const message = (errorData as { message?: string }).message || 'API request failed';
console.error('API error', {
url: response.url,
status: response.status,
message,
data: errorData
});
throw new ApiError(message, response.status, errorData);
}