Ky: Error not thrown without the json method

Created on 1 Feb 2019  路  1Comment  路  Source: sindresorhus/ky

Hi,

I was just trying to catch errors in the front-end of my react/redux app when I spotted a really odd behavior.

  • When doing a DELETE request on my API without ".json()" appended to my response, http errors are not thrown, even with throwHttpErrors forced true in the options.
try {
    await api.delete(`roles/${id}`);
    dispatch(actions.deleteRole(id));
  } catch (error) {
    dispatch({
      type: types.DELETE_ROLE_ERROR,
      payload: {
        error,
      },
    });
  }
  • When doing the same DELETE request with ".json()" appended to the response, errors are thrown but the request is fired twice so the second one always returns a 404.
try {
    await api.delete(`roles/${id}`).json();
    dispatch(actions.deleteRole(id));
  } catch (error) {
    dispatch({
      type: types.DELETE_ROLE_ERROR,
      payload: {
        error,
      },
    });
  }

Any idea why this would happen?

bug help wanted

Most helpful comment

Hi @Hinosxz! Thanks for the report and yes this is a bug. We'll definitely get it fixed, but in the mean time, I strongly recommend using one of the Body shortcuts:

await ky().json()
await ky().text()
await ky().formData()
await ky().arrayBuffer()
await ky().blob()

Those should work well, whereas plain await ky() without a Body shortcut is currently missing some features, including retries, HTTP status throws, and afterResponse hooks. It's not intended. PR very much welcome!

Note to all: This has the same underlying cause as #94. See https://github.com/sindresorhus/ky/issues/94#issuecomment-459489020 for a rough idea of what needs to be done.

>All comments

Hi @Hinosxz! Thanks for the report and yes this is a bug. We'll definitely get it fixed, but in the mean time, I strongly recommend using one of the Body shortcuts:

await ky().json()
await ky().text()
await ky().formData()
await ky().arrayBuffer()
await ky().blob()

Those should work well, whereas plain await ky() without a Body shortcut is currently missing some features, including retries, HTTP status throws, and afterResponse hooks. It's not intended. PR very much welcome!

Note to all: This has the same underlying cause as #94. See https://github.com/sindresorhus/ky/issues/94#issuecomment-459489020 for a rough idea of what needs to be done.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sindresorhus picture sindresorhus  路  7Comments

davalapar picture davalapar  路  6Comments

ShivamJoker picture ShivamJoker  路  5Comments

Splact picture Splact  路  7Comments

kirillgroshkov picture kirillgroshkov  路  3Comments