acacode / acacode/swagger-typescript-api

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

Abierto
#618 0 comentarios 0 reacciones 0 asignados Ver en GitHub
Lenguaje dominante
TypeScript
Estrellas
4.1k
Forks
436
Métricas de merge de PR
Sin PR fusionados en 30 d

Descripción

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

Guía de contribución

No hay ninguna guía de contribución indexada para este repositorio

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.