diff --git a/packages/iso-web/src/http.js b/packages/iso-web/src/http.js index e935e2ec..af01c978 100644 --- a/packages/iso-web/src/http.js +++ b/packages/iso-web/src/http.js @@ -221,7 +221,9 @@ export async function request(resource, options = {}) { // validate resource type if (typeof resource !== 'string' && !(resource instanceof URL)) { - throw new TypeError('`resource` must be a string or URL object') + return { + error: new RequestError('`resource` must be a string or URL object'), + } } const timeoutSignal = AbortSignal.timeout(timeout) @@ -417,7 +419,6 @@ request.trace = function trace(resource, options = {}) { request.json = async function json(resource, options = {}) { const { error, result } = await request(resource, { ...options, - // eslint-disable-next-line unicorn/no-null body: null, json: options.body, }) diff --git a/packages/iso-web/test/request.test.js b/packages/iso-web/test/request.test.js index bcd1f733..1b3edcb6 100644 --- a/packages/iso-web/test/request.test.js +++ b/packages/iso-web/test/request.test.js @@ -1,6 +1,6 @@ import { http, HttpResponse, delay } from 'msw' import { assert, suite } from 'playwright-test/taps' -import { HttpError, JsonError, request } from '../src/http.js' +import { HttpError, JsonError, RequestError, request } from '../src/http.js' import { setup } from '../src/msw/msw.js' const test = suite('request') @@ -91,6 +91,17 @@ test('should request 500', async () => { } }) +test('should fail with bad resource', async () => { + // @ts-expect-error - tests bad resource + const { error } = await request(1000) + + if (RequestError.is(error)) { + assert.equal(error.message, '`resource` must be a string or URL object') + } else { + assert.fail('should fail') + } +}) + test('should request 501', async () => { const { error } = await request('https://local.dev/error?status=501')