- From: Justin Fagnani <notifications@github.com>
- Date: Wed, 01 Nov 2023 12:55:52 -0700
- To: whatwg/fetch <fetch@noreply.github.com>
- Cc: Subscribed <subscribed@noreply.github.com>
Received on Wednesday, 1 November 2023 19:55:58 UTC
@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