acacode / acacode/swagger-typescript-api

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

Offen
#618 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
Vorherrschende Sprache
TypeScript
Sterne
4.1k
Forks
436
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

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

Beitragsleitfaden

Für dieses Repository ist kein Beitragsleitfaden indexiert

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.