Re: [whatwg/fetch] Consider response.throwIfNotOk() (Issue #1679)

@jakearchibald 

> Totally agree. I tend to write something like this:
> 
> ```js
> async function getData(url) {
>   const response = await fetch(url);
> 
>   try {
>     const data = await response.json();
>     // Use presumably more accurate error data from the response:
>     if (data.error) throw Error(data.error);
>     return data;
>   } catch (error) {
>     // Error seems like a JSON parsing issue:
>     if (response.ok) throw error;
>     // Construct an error from the status:
>     throw Error(`Response error: ${response.status} - ${response.statusText}`);
>   }
> }
> ```
> 
> `throwIfNotOk` doesn't really help here.

Doesn't it a bit though?

```ts
async function getData(url) {
  const response = await fetch(url);
  response.throwIfNotOk();
  const data = await response.json();
  // Use presumably more accurate error data from the response:
  if (data.error) throw Error(data.error);
  return data;
}
```

-- 
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/fetch/issues/1679#issuecomment-1789582353
You are receiving this because you are subscribed to this thread.

Message ID: <whatwg/fetch/issues/1679/1789582353@github.com>

Received on Wednesday, 1 November 2023 19:55:58 UTC