mswjs / source

Generate MSW request handlers from various sources (HAR files, OpenAPI documents, etc).
https://source.mswjs.io
131 stars 5 forks source link

Traffic: [Status 0] - The status provided (0) is outside the range [200, 599] #53

Closed JeanDamien closed 5 days ago

JeanDamien commented 1 month ago

Description

If the user uses a .har file that contained a resource blocked by the browser (or adblocker) with a 0 status , this package will throw an exception:

Uncaught (in promise) RangeError: Failed to construct 'Response': The status provided (0) is outside the range [200, 599].
    at toResponse (@mswjs_source_traffic.js)
    at fromTraffic (@mswjs_source_traffic.js)

https://github.com/mswjs/source/blob/main/src/traffic/utils/har-utils.ts#L40

Reproduction steps

image

HAR file

test/traffic/fixtures/archives/entry-with-status-0.har

Test template

  // Happen when the resource is blocked by the browser
  it(
    'does not throw an exception given an HAR file with a 0 status entry',
    () => {
      const traffic = readArchive(
        'test/traffic/fixtures/archives/entry-with-status-0.har',
      )
      expect(() => fromTraffic(traffic)).not.toThrow()
    },
  )

Expected behavior

It would be ideal if the library didn't throw an exception, or if we could add an entry in the documentation advising users to filter those entries in HAR before using it.

Workaround

fromTraffic(traffic, (entry) => {
  if (entry.response.status === 0) {
    return
  }
  return entry
})
weyert commented 1 month ago

Ah interesting. Never thought of this. The error appears to come from the Response-class itself, we probably want to add your fix into the code base.

kettanaito commented 5 days ago

Released: v0.3.1 🎉

This has been released in v0.3.1!

Make sure to always update to the latest version (npm i @mswjs/source@latest) to get the newest features and bug fixes.


Predictable release automation by @ossjs/release.