Changed XHR error handling
This commit is contained in:
parent
07606cd996
commit
6f60f8bd94
4
package-lock.json
generated
4
package-lock.json
generated
@ -1,12 +1,12 @@
|
||||
{
|
||||
"name": "@ztimson/utils",
|
||||
"version": "0.10.2",
|
||||
"version": "0.10.4",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "@ztimson/utils",
|
||||
"version": "0.10.2",
|
||||
"version": "0.10.4",
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"@types/jest": "^29.5.12",
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ztimson/utils",
|
||||
"version": "0.10.3",
|
||||
"version": "0.10.4",
|
||||
"description": "Utility library",
|
||||
"author": "Zak Timson",
|
||||
"license": "MIT",
|
||||
|
@ -75,10 +75,16 @@ export class XHR {
|
||||
await new Promise<void>(res => fn(resp, () => res()));
|
||||
}
|
||||
|
||||
if(!resp.ok) throw new Error(resp.statusText);
|
||||
const decode = async () => {
|
||||
if(!opts.skipConverting && resp.headers.get('Content-Type')?.startsWith('application/json')) return await resp.json();
|
||||
if(!opts.skipConverting && resp.headers.get('Content-Type')?.startsWith('text/plain')) return await <any>resp.text();
|
||||
return resp;
|
||||
}
|
||||
|
||||
const payload = await decode();
|
||||
if(resp.ok) return payload;
|
||||
const text = resp.statusText || (typeof payload == 'string' ? payload : null);
|
||||
throw (text ? new Error(text) : payload);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user