Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

Response body read errors bypass the SDK's typed error handling

Aperta
#757 0 commenti 0 reazioni 1 assegnatario Vedi su GitHub

@radenkovic ci sta già lavorando.

Dal 28/7/2026.

Valutazione

Questa issue non è ancora stata valutata.

Descrizione

Describe the bug

When the HTTP connection drops while the SDK is reading a response body, the error that reaches my code is a raw TypeError: terminated from Node's fetch stack, not one of the SDK's own error types (NylasApiError, NylasOAuthError, NylasSdkTimeoutError). My catch block checks instanceof NylasApiError, so it falls through and I lose the request context I normally rely on (the requestId and flow id).

Looking at src/apiClient.ts, the two response-body reads are outside any try/catch:

  • requestWithResponse reads the body at line 317: const text = await response.text();. The try block starts on the next line and only wraps JSON.parse(text), so a failure in response.text() itself is not caught here.
  • requestRaw reads the body at line 348: const arrayBuffer = await response.arrayBuffer();, with no try/catch in the function.

This looks inconsistent with sendRequest, which already wraps fetch() and converts failures into SDK errors (for example AbortError becomes NylasSdkTimeoutError, and a non-2xx status becomes NylasApiError). A connection that fails before the first byte surfaces a typed error, but the same connection failing mid-body surfaces a raw undici error. The requestRaw path is the one I hit most, since it backs Attachments.download() and large downloads over a flaky network are the most likely to be cut off partway.

To Reproduce

Point the client at a local server that returns a success status and then closes the socket mid-body, so the body read rejects:

const http = require('http');
const Nylas = require('nylas').default;

// Server that sends headers + a partial body, then destroys the socket
const server = http.createServer((req, res) => {
  res.writeHead(200, { 'Content-Type': 'application/json' });
  res.write('{"request_id":"abc","data":');   // intentionally incomplete
  res.socket.destroy();                        // drop the connection mid-body
});

server.listen(0, async () => {
  const nylas = new Nylas({ apiKey: 'test', apiUri: `http://localhost:${server.address().port}` });

  try {
    await nylas.messages.list({ identifier: 'me' });
  } catch (e) {
    console.log('name:', e.name);                 // TypeError
    console.log('message:', e.message);           // terminated
    console.log('is NylasApiError:', e instanceof Nylas.NylasApiError); // false
  } finally {
    server.close();
  }
});

The same happens through nylas.attachments.download(...), which reads the body with response.arrayBuffer().

Expected behavior

A failure while reading the response body should be surfaced the same way as other transport failures: as an SDK error type, carrying the flow id / request id when available, so that a catch (e) { if (e instanceof NylasApiError) ... } block behaves consistently regardless of when in the request lifecycle the connection dropped. At minimum the raw error should be wrapped in a descriptive Error rather than leaking terminated.

SDK Version:

8.4.0

Additional context

The two spots are requestWithResponse (src/apiClient.ts:317) and requestRaw (src/apiClient.ts:348). Wrapping each body read in a try/catch and rethrowing an SDK-typed error, matching the pattern already used in sendRequest and in the existing JSON.parse catch, would keep the error surface consistent. Happy to open a PR with that change if it would be useful.

Lingua principale
TypeScript
Stelle
180
Fork
126
Merge medio
4g 6h
PR unite (30g)
3

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di nylas/nylas-nodejs

Tutte le issue di nylas/nylas-nodejs

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.