acacode / acacode/swagger-typescript-api

Request.download() failing silently because node-fetch-h2 does not throw on 400/500s

未关闭
#618 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
主要语言
TypeScript
星标
4.1k
派生
436
PR 合并指标
30 天内没有已合并 PR

描述

The Request class in `util/request.js` has a `download()` method. This method uses `node-fetch-h2`. It has a try/catch around the `fetch()` call, however, `node-fetch-h2` differs from `window.fetch` in the browser because it does not throw an error on 400 responses.

Source https://github.com/acacode/swagger-typescript-api/blob/master/src/util/request.js#L47-L54

Instead, you need to check `response.ok` to see if it's true or false. Documented here https://www.npmjs.com/package/node-fetch-h2#handling-client-and-server-errors

The fix could be a simple one-liner that checks `response.ok` and throws an error which the try/catch can handle.

```typescript
try {
const response = await fetch(url, requestOptions);
if (!response.ok) throw Error(response.message)
return await response.text();
} catch (error) {
const message = `error while fetching data from URL "${url}"`;
this.logger.error(message, 'response' in error ? error.response : error);
return message;
}
```

贡献指南

这个仓库没有索引到贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。