acacode / acacode/swagger-typescript-api

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

オープン
#618 コメント 0 件 リアクション 0 件 担当者 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 を短くまとめたダイジェスト。