acacode / acacode/swagger-typescript-api

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

Đang mở
#618 0 bình luận 0 reaction 0 người được giao Xem trên GitHub
Ngôn ngữ chính
TypeScript
Star
4.1k
Fork
436
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

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;
}
```

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.