Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/web-api-upload-stream-error.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@slack/web-api": patch
---

fix: reject `files.uploadV2` with a `WebAPIFileUploadReadFileDataError` when the `file` stream errors, instead of an uncaught exception
53 changes: 52 additions & 1 deletion packages/web-api/src/file-upload.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import assert from 'node:assert/strict';
import { createReadStream, statSync, unlinkSync, writeFileSync } from 'node:fs';
import { Readable } from 'node:stream';
import { afterEach, beforeEach, describe, it } from 'node:test';
import sinon from 'sinon';
import { ErrorCode, SlackError, WebAPIFileUploadInvalidArgumentsError } from './errors';
import {
ErrorCode,
SlackError,
WebAPIFileUploadInvalidArgumentsError,
WebAPIFileUploadReadFileDataError,
} from './errors';
import {
buildChannelsWarning,
buildLegacyFileTypeWarning,
Expand Down Expand Up @@ -274,6 +280,18 @@ describe('file-upload', () => {
// should return file data from the stream
assert.strictEqual(Buffer.isBuffer(res), true);
});
it('rejects when the file ReadStream cannot be read', async () => {
const fileUpload = {
file: createReadStream('./test/fixtures/does-not-exist.txt'),
filename: 'Test File',
};
await assert.rejects(getFileData(fileUpload), (err: unknown) => {
assert.ok(err instanceof WebAPIFileUploadReadFileDataError);
assert.strictEqual(err.code, ErrorCode.FileUploadReadFileDataError);
assert.strictEqual((err.cause as NodeJS.ErrnoException).code, 'ENOENT');
return true;
});
});
it('handles content as string', async () => {
const validFileUpload = {
content: 'Happiness',
Expand Down Expand Up @@ -307,6 +325,39 @@ describe('file-upload', () => {
assert.strictEqual(err.message, 'No data in supplied file');
}
});
it('rejects if the stream errors before any data is read', async () => {
await assert.rejects(
getFileDataAsStream(createReadStream('./test/fixtures/does-not-exist.txt')),
(err: unknown) => {
assert.ok(err instanceof WebAPIFileUploadReadFileDataError);
assert.ok(err instanceof SlackError);
assert.strictEqual(err.code, ErrorCode.FileUploadReadFileDataError);
assert.match(err.message, /^Unable to read file data from the supplied stream: ENOENT/);
assert.strictEqual((err.cause as NodeJS.ErrnoException).code, 'ENOENT');
return true;
},
);
});
it('rejects if the stream errors after some data is read', async () => {
const failure = new Error('connection reset');
let reads = 0;
const readable = new Readable({
read() {
reads += 1;
if (reads === 1) {
this.push('partial data');
} else {
this.destroy(failure);
}
},
});
await assert.rejects(getFileDataAsStream(readable), (err: unknown) => {
assert.ok(err instanceof WebAPIFileUploadReadFileDataError);
assert.strictEqual(err.message, 'Unable to read file data from the supplied stream: connection reset');
assert.strictEqual(err.cause, failure);
return true;
});
});
it('ensures complete file is uploaded', async () => {
try {
// create a large file
Expand Down
8 changes: 8 additions & 0 deletions packages/web-api/src/file-upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,14 @@ export async function getFileDataAsStream(readable: Readable): Promise<Buffer> {
reject(Error('No data in supplied file'));
}
});
// without this listener a failing stream is an uncaught exception and the promise never settles
readable.on('error', (err: Error) => {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

praise: Thanks for catching this! Keeping the stream error as cause makes the ENOENT easy to find for anyone debugging an upload.

reject(
new WebAPIFileUploadReadFileDataError(`Unable to read file data from the supplied stream: ${err.message}`, {
cause: err,
}),
);
});
});
}

Expand Down