From 0f10aebfd207e2c9f3b9a690053cc9561cf4db33 Mon Sep 17 00:00:00 2001 From: ztimson Date: Mon, 14 Oct 2024 20:46:32 -0400 Subject: [PATCH] bubble up all fetch errors in the http helper --- package.json | 2 +- src/http.ts | 81 ++++++++++++++++++++++++++++------------------------ 2 files changed, 44 insertions(+), 39 deletions(-) diff --git a/package.json b/package.json index 37bf06e..1529c83 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@ztimson/utils", - "version": "0.20.1", + "version": "0.20.2", "description": "Utility library", "author": "Zak Timson", "license": "MIT", diff --git a/src/http.ts b/src/http.ts index a6b5531..230d478 100644 --- a/src/http.ts +++ b/src/http.ts @@ -75,47 +75,52 @@ export class Http { // Send request return new PromiseProgress((res, rej, prog) => { - fetch(url, { - headers, - method: opts.method || (opts.body ? 'POST' : 'GET'), - body: opts.body - }).then(async (resp: any) => { - for(let fn of [...Object.values(Http.interceptors), ...Object.values(this.interceptors)]) { - await new Promise(res => fn(resp, () => res())); - } - - const contentLength = resp.headers.get('Content-Length'); - const total = contentLength ? parseInt(contentLength, 10) : 0; - let loaded = 0; - - const reader = resp.body?.getReader(); - const stream = new ReadableStream({ - start(controller) { - function push() { - reader?.read().then((event: any) => { - if(event.done) return controller.close(); - loaded += event.value.byteLength; - prog(loaded / total); - controller.enqueue(event.value); - push(); - }).catch((error: any) => controller.error(error)); - } - push(); + try { + fetch(url, { + headers, + method: opts.method || (opts.body ? 'POST' : 'GET'), + body: opts.body + }).then(async (resp: any) => { + for(let fn of [...Object.values(Http.interceptors), ...Object.values(this.interceptors)]) { + await new Promise(res => fn(resp, () => res())); } - }); - resp.data = new Response(stream); - if(opts.decode == null || opts.decode) { - const content = resp.headers.get('Content-Type')?.toLowerCase(); - if(content?.includes('form')) resp.data = await resp.data.formData(); - else if(content?.includes('json')) resp.data = await resp.data.json(); - else if(content?.includes('text')) resp.data = await resp.data.text(); - else if(content?.includes('application')) resp.data = await resp.data.blob(); - } + const contentLength = resp.headers.get('Content-Length'); + const total = contentLength ? parseInt(contentLength, 10) : 0; + let loaded = 0; - if(resp.ok) res(resp); - else rej(resp); - }) + const reader = resp.body?.getReader(); + const stream = new ReadableStream({ + start(controller) { + function push() { + reader?.read().then((event: any) => { + if(event.done) return controller.close(); + loaded += event.value.byteLength; + prog(loaded / total); + controller.enqueue(event.value); + push(); + }).catch((error: any) => controller.error(error)); + } + + push(); + } + }); + + resp.data = new Response(stream); + if(opts.decode == null || opts.decode) { + const content = resp.headers.get('Content-Type')?.toLowerCase(); + if(content?.includes('form')) resp.data = await resp.data.formData(); + else if(content?.includes('json')) resp.data = await resp.data.json(); + else if(content?.includes('text')) resp.data = await resp.data.text(); + else if(content?.includes('application')) resp.data = await resp.data.blob(); + } + + if(resp.ok) res(resp); + else rej(resp); + }).catch(err => rej(err)); + } catch(err) { + rej(err); + } }); } }