diff --git a/resources/assets/src/scripts/net.ts b/resources/assets/src/scripts/net.ts index 57ad8643..470bb579 100644 --- a/resources/assets/src/scripts/net.ts +++ b/resources/assets/src/scripts/net.ts @@ -30,6 +30,7 @@ export async function walkFetch(request: Request): Promise { try { const response = await fetch(request) + const cloned = response.clone() const body = response.headers.get('Content-Type') === 'application/json' ? await response.json() : await response.text() @@ -49,8 +50,7 @@ export async function walkFetch(request: Request): Promise { return } - const res = response.clone() - throw new HTTPError(body.message || body, res) + throw new HTTPError(body.message || body, cloned) } catch (error) { emit('fetchError', error) showAjaxError(error) diff --git a/resources/assets/tests/scripts/net.test.ts b/resources/assets/tests/scripts/net.test.ts index 68064ab5..3873d56b 100644 --- a/resources/assets/tests/scripts/net.test.ts +++ b/resources/assets/tests/scripts/net.test.ts @@ -11,6 +11,7 @@ test('the GET method', async () => { ok: true, json, headers: new Map([['Content-Type', 'application/json']]), + clone: () => ({}), }) const stub = jest.fn() @@ -35,6 +36,7 @@ test('the POST method', async () => { ok: true, json: () => Promise.resolve({}), headers: new Map([['Content-Type', 'application/json']]), + clone: () => ({}), }) const meta = document.createElement('meta') @@ -91,11 +93,13 @@ test('low level fetch', async () => { ok: true, json, headers: new Map([['Content-Type', 'application/json']]), + clone: () => ({}), }) .mockResolvedValueOnce({ ok: true, headers: new Map(), text: () => Promise.resolve('text'), + clone: () => ({}), }) const request: RequestInit = { headers: new Headers() } @@ -134,6 +138,7 @@ test('process backend errors', async () => { errors: { name: ['required'] }, }) }, + clone: () => ({}), }) .mockResolvedValueOnce({ status: 403, @@ -141,6 +146,7 @@ test('process backend errors', async () => { json() { return Promise.resolve({ message: 'forbidden' }) }, + clone: () => ({}), }) const result: {