Skip to content

Commit a36e630

Browse files
authored
node: suppress stream errors when uploading (#346)
* node: suppress stream errors when uploading * node: move wrapReadableSuppressErrors to a private class method
1 parent 47ff386 commit a36e630

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

packages/node/src/BacktraceNodeRequestHandler.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
import FormData from 'form-data';
1111
import http, { ClientRequest, IncomingMessage } from 'http';
1212
import https from 'https';
13-
import { Readable } from 'stream';
13+
import { PassThrough, Readable } from 'stream';
1414

1515
export interface BacktraceNodeRequestHandlerOptions {
1616
readonly timeout?: number;
@@ -238,13 +238,39 @@ export class BacktraceNodeRequestHandler implements BacktraceRequestHandler {
238238
}
239239

240240
for (const attachment of attachments) {
241-
const data = attachment.get();
241+
let data = attachment.get();
242242
if (!data) {
243243
continue;
244244
}
245+
246+
if (data instanceof Readable) {
247+
data = this.wrapReadableSuppressErrors(data);
248+
}
249+
245250
formData.append(`attachment_${attachment.name}`, data, attachment.name);
246251
}
247252

248253
return formData;
249254
}
255+
256+
/**
257+
* When inputStream emits an error, it will be suppressed, and the stream will be closed.
258+
*/
259+
private wrapReadableSuppressErrors(inputStream: Readable) {
260+
const safeStream = new PassThrough();
261+
262+
inputStream.on('data', (chunk) => {
263+
safeStream.write(chunk);
264+
});
265+
266+
inputStream.on('end', () => {
267+
safeStream.end();
268+
});
269+
270+
inputStream.on('error', () => {
271+
safeStream.end();
272+
});
273+
274+
return safeStream;
275+
}
250276
}

0 commit comments

Comments
 (0)