receipts/js/ajaxUtil.ts

17 lines
491 B
TypeScript

export async function getResponseError(r: Response): Promise<string> {
let ct = r.headers.get("Content-Type");
if (ct && ct.indexOf("json") > -1) {
const json = await r.json();
if (json.error) {
return json.error;
}
}
const html = await r.text();
if (html) {
const doc = new DOMParser().parseFromString(html, "text/html");
return doc.documentElement.textContent ?? "";
} else {
return r.statusText;
}
}