Skip to content

Commit

Permalink
fix: Handle unhandled error in startResumableUpload_ (#2495)
Browse files Browse the repository at this point in the history
* Added error handling using `stream.destroy`
  • Loading branch information
cuyl authored Jul 9, 2024
1 parent 013a5a4 commit d5257ba
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4141,8 +4141,7 @@ class File extends ServiceObject<File, FileMetadata> {
) {
retryOptions.autoRetry = false;
}

const uploadStream = resumableUpload.upload({
const cfg = {
authClient: this.storage.authClient,
apiEndpoint: this.storage.apiEndpoint,
bucket: this.bucket.name,
Expand All @@ -4168,7 +4167,17 @@ class File extends ServiceObject<File, FileMetadata> {
highWaterMark: options?.highWaterMark,
universeDomain: this.bucket.storage.universeDomain,
[GCCL_GCS_CMD_KEY]: options[GCCL_GCS_CMD_KEY],
});
};

let uploadStream: resumableUpload.Upload;

try {
uploadStream = resumableUpload.upload(cfg);
} catch (error) {
dup.destroy(error as Error);
this.storage.retryOptions.autoRetry = this.instanceRetryValue;
return;
}

uploadStream
.on('response', resp => {
Expand Down
19 changes: 19 additions & 0 deletions test/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1991,6 +1991,25 @@ describe('File', () => {
writable.write('data');
});

it('should emit RangeError', done => {
const error = new RangeError(
'Cannot provide an `offset` without providing a `uri`'
);

const opitons = {
offset: 1,
isPartialUpload: true,
};
const writable = file.createWriteStream(opitons);

writable.on('error', (err: RangeError) => {
assert.deepEqual(err, error);
done();
});

writable.write('data');
});

it('should emit progress via resumable upload', done => {
const progress = {};

Expand Down

0 comments on commit d5257ba

Please sign in to comment.