diff --git a/lib/fs.js b/lib/fs.js index a7b56ee86171e3..a32e8a89e3ff09 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -36,6 +36,7 @@ const { ObjectDefineProperty, Promise, PromisePrototypeThen, + PromiseReject, PromiseResolve, ReflectApply, SafeMap, @@ -574,8 +575,12 @@ function openAsBlob(path, options = kEmptyObject) { // The underlying implementation here returns the Blob synchronously for now. // To give ourselves flexibility to maybe return the Blob asynchronously, // this API returns a Promise. - path = getValidatedPath(path); - return PromiseResolve(createBlobFromFilePath(path, { type })); + try { + path = getValidatedPath(path); + return PromiseResolve(createBlobFromFilePath(path, { type })); + } catch (error) { + return PromiseReject(error); + } } /** diff --git a/test/parallel/test-fs-openasblob-promise-contract.js b/test/parallel/test-fs-openasblob-promise-contract.js new file mode 100644 index 00000000000000..0803725c888f63 --- /dev/null +++ b/test/parallel/test-fs-openasblob-promise-contract.js @@ -0,0 +1,21 @@ +'use strict'; + +const assert = require('assert'); +const fs = require('fs'); + +// Verify invalid paths throw synchronously and return a Promise +(async () => { + let promise; + assert.doesNotThrow(() => { promise = fs.openAsBlob('does-not-exist'); }); + assert.strictEqual(typeof promise?.then, 'function'); + await assert.rejects(promise, { code: 'ERR_INVALID_ARG_VALUE' }); +})(); + + +// Verify that invalid arguments throw synchronously and return a Promise +(async () => { + let promise; + assert.doesNotThrow(() => { promise = fs.openAsBlob(123); }); + assert.strictEqual(typeof promise?.then, 'function'); + await assert.rejects(promise, { code: 'ERR_INVALID_ARG_TYPE' }); +})(); \ No newline at end of file