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:
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user