Skip to content

Commit

Permalink
feat: improved error messages for resumable uploads (#1708)
Browse files Browse the repository at this point in the history
* feat: improved error messages for resumable uploads

* 馃 Updates from OwlBot

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
  • Loading branch information
shaffeeullah and gcf-owl-bot[bot] committed Nov 9, 2021
1 parent 7d61bff commit 50cdbb6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
13 changes: 12 additions & 1 deletion src/file.ts
Expand Up @@ -282,6 +282,7 @@ export enum ActionToHTTPMethod {
*/
class ResumableUploadError extends Error {
name = 'ResumableUploadError';
additionalInfo?: string;
}

/**
Expand Down Expand Up @@ -1914,14 +1915,24 @@ class File extends ServiceObject<File> {
if (options.resumable) {
// The user wanted a resumable upload, but we couldn't create a
// configuration directory, which means gcs-resumable-upload will fail.

// Determine if the issue is that the directory does not exist or
// if the directory exists, but is not writable.
const error = new ResumableUploadError(
[
'A resumable upload could not be performed. The directory,',
`${configDir}, is not writable. You may try another upload,`,
'this time setting `options.resumable` to `false`.',
].join(' ')
);
stream.destroy(error);
fs.access(configDir, fs.constants.R_OK, noReadErr => {
if (noReadErr) {
error.additionalInfo = 'The directory does not exist.';
} else {
error.additionalInfo = 'The directory is read-only.';
}
stream.destroy(error);
});
} else {
// The user didn't care, resumable or not. Fall back to simple upload.
this.startSimpleUpload_(fileWriteStream, options);
Expand Down
10 changes: 9 additions & 1 deletion test/file.ts
Expand Up @@ -65,6 +65,10 @@ class HTTPError extends Error {
}
}

class ResumableUploadError extends Error {
additionalInfo?: string;
}

let promisified = false;
let makeWritableStreamOverride: Function | null;
let handleRespOverride: Function | null;
Expand Down Expand Up @@ -2131,7 +2135,7 @@ describe('File', () => {

const writable = file.createWriteStream({resumable: true});

writable.on('error', (err: Error) => {
writable.on('error', (err: ResumableUploadError) => {
assert.strictEqual(err.name, 'ResumableUploadError');
assert.strictEqual(
err.message,
Expand All @@ -2141,6 +2145,10 @@ describe('File', () => {
'this time setting `options.resumable` to `false`.',
].join(' ')
);
assert.strictEqual(
err.additionalInfo,
'The directory does not exist.'
);

done();
});
Expand Down

0 comments on commit 50cdbb6

Please sign in to comment.