(response)
| 4 | |
| 5 | // Helper to handle common fetch logic and errors |
| 6 | async function handleApiResponse(response) { |
| 7 | if (!response.ok) { |
| 8 | let errorMsg = `API Error: ${response.status} ${response.statusText}`; |
| 9 | let errorData = null; |
| 10 | try { |
| 11 | // Attempt to parse error detail from backend |
| 12 | errorData = await response.json(); |
| 13 | errorMsg = errorData.detail || JSON.stringify(errorData) || errorMsg; |
| 14 | } catch (e) { |
| 15 | // If parsing fails, try reading as text |
| 16 | try { |
| 17 | errorMsg = await response.text() || errorMsg; |
| 18 | } catch (textError) { /* Ignore text reading error */ } |
| 19 | } |
| 20 | console.error('API Error Data:', errorData); |
| 21 | const error = new Error(errorMsg); |
| 22 | error.status = response.status; |
| 23 | error.data = errorData; |
| 24 | throw error; |
| 25 | } |
| 26 | // If response has content, parse as JSON, otherwise return the response object |
| 27 | const contentType = response.headers.get("content-type"); |
| 28 | if (contentType && contentType.indexOf("application/json") !== -1) { |
| 29 | return response.json(); |
| 30 | } |
| 31 | if (contentType && contentType.indexOf("text/") !== -1) { |
| 32 | return response.text(); // Handle text responses like config content |
| 33 | } |
| 34 | // For downloads or other types, return the raw response |
| 35 | return response; |
| 36 | } |
| 37 | |
| 38 | // --- Config API --- |
| 39 |
no outgoing calls
no test coverage detected