Skip to content

fix: anchor fetchThrowsOn regexes to avoid false positives on 2xx/3xx#673

Open
andreahlert wants to merge 1 commit intobigskysoftware:devfrom
andreahlert:fix/fetch-throws-on-non-2xx-anchor
Open

fix: anchor fetchThrowsOn regexes to avoid false positives on 2xx/3xx#673
andreahlert wants to merge 1 commit intobigskysoftware:devfrom
andreahlert:fix/fetch-throws-on-non-2xx-anchor

Conversation

@andreahlert
Copy link
Copy Markdown
Contributor

Problem

Default fetchThrowsOn patterns are unanchored:

fetchThrowsOn: [/4.*/, /5.*/],

/4.*/.test("204") returns true because "4" appears at index 2. Same issue affects 304, 250-259, etc. Any 2xx/3xx response whose status string contains a 4 or 5 digit is incorrectly treated as an error and throws fetch failed: <status> ....

Fixes #670.

Repro

<body _="on keypress fetch `/kbd/${event.key}` with method:'POST'">

Server returns 204 No Content. Console:

fetch failed: 204 No Content (/kbd/d)

Docs say: "fetch throws on non-2xx responses (404, 500, etc.)" — current behavior contradicts that.

Fix

Anchor the regexes:

fetchThrowsOn: [/^4/, /^5/],
> /4.*/.test("204")   // true  (bug)
> /^4/.test("204")    // false (fixed)
> /^4/.test("404")    // true  (still throws as expected)

Tests

Added two cases in test/commands/fetch.js:

  • does not throw on 204 No Content
  • does not throw on 304 Not Modified

All 25 fetch tests pass.

The default `fetchThrowsOn` patterns `/4.*/` and `/5.*/` are unanchored,
so any status code containing the digit 4 or 5 matches and throws.
This incorrectly fails 204, 304, 250-259, etc.

Anchor to `/^4/` and `/^5/` so only 4xx and 5xx throw, matching the
documented behavior ("fetch throws on non-2xx responses").

Fixes bigskysoftware#670
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant