Skip to content

Commit

Permalink
fix(try-catch): throw on the first error, when there is not enough space
Browse files Browse the repository at this point in the history
  • Loading branch information
ido-pluto committed May 10, 2024
1 parent 722bc9c commit dfbe6bf
Showing 1 changed file with 17 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const DEFAULT_OPTIONS: DownloadEngineWriteStreamOptionsNodeJS = {
mode: "r+"
};

const NOT_ENOUGH_SPACE_ERROR_CODE = "ENOSPC";

export default class DownloadEngineWriteStreamNodejs extends BaseDownloadEngineWriteStream {
private _fd: FileHandle | null = null;
private _fileWriteFinished = false;
Expand All @@ -39,9 +41,23 @@ export default class DownloadEngineWriteStreamNodejs extends BaseDownloadEngineW
}

async write(cursor: number, buffer: Uint8Array) {
let throwError: Error | false = false;

await retry(async () => {
return await this._writeWithoutRetry(cursor, buffer);
try {
return await this._writeWithoutRetry(cursor, buffer);
} catch (error: any) {
if (error?.code === NOT_ENOUGH_SPACE_ERROR_CODE) {
throwError = error;
return;
}
throw error;
}
}, this.options.retry);

if (throwError) {
throw throwError;
}
}

async ftruncate(size = this.fileSize) {
Expand Down

0 comments on commit dfbe6bf

Please sign in to comment.