Skip to content

Commit

Permalink
fix: move a type error throw into MaybeResult
Browse files Browse the repository at this point in the history
  • Loading branch information
hugomrdias committed Jan 16, 2025
1 parent 29f229b commit cc66a47
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
5 changes: 3 additions & 2 deletions packages/iso-web/src/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
})
Expand Down
13 changes: 12 additions & 1 deletion packages/iso-web/test/request.test.js
Original file line number Diff line number Diff line change
@@ -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')
Expand Down Expand Up @@ -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')

Expand Down

0 comments on commit cc66a47

Please sign in to comment.