UNPKG

@keycloakify/keycloak-account-ui

Version:
36 lines 1.3 kB
import { getNetworkErrorMessage, getNetworkErrorDescription, } from "../ui-shared"; import { CONTENT_TYPE_HEADER, CONTENT_TYPE_JSON } from "../api/constants"; export class ApiError extends Error { constructor(message, description) { super(message); this.description = description; } } export async function parseResponse(response) { const contentType = response.headers.get(CONTENT_TYPE_HEADER); const isJSON = contentType ? contentType.includes(CONTENT_TYPE_JSON) : false; if (!isJSON) { throw new Error(`Expected response to have a JSON content type, got '${contentType}' instead.`); } const data = await parseJSON(response); if (!response.ok) { const message = getNetworkErrorMessage(data); const description = getNetworkErrorDescription(data); if (!message) { throw new Error("Unable to retrieve error message from response, no matching key found."); } throw new ApiError(message, description); } return data; } async function parseJSON(response) { try { return await response.json(); } catch (error) { throw new Error("Unable to parse response as valid JSON.", { cause: error, }); } } //# sourceMappingURL=parse-response.js.map