GeezerVison is showing github.com — fs: restore stack traces of fs/promises readFile and writeFile errors · nodejs/node@2ad4108
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/fs/promises.js +39-19Lines changed: 39 additions & 19 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2226,8 +2226,14 @@ async function writeFile(path, data, options) { | 2226 | 2226 |
| 2227 | 2227 | if (!flush && !isCustomIterable(data) && data.byteLength <= kWriteFileMaxChunkSize) { | 2228 | 2228 | path = getValidatedPath(path); | 2229 | | - await writeFileInOneRoundTrip(path, stringToFlags(flag, 'options.flag'), | 2230 | | - parseFileMode(options.mode, 'mode', 0o666), data); | | 2229 | + const flagsNumber = stringToFlags(flag, 'options.flag'); | | 2230 | + const mode = parseFileMode(options.mode, 'mode', 0o666); | | 2231 | + try { | | 2232 | + await writeFileInOneRoundTrip(path, flagsNumber, mode, data); | | 2233 | + } catch (err) { | | 2234 | + ErrorCaptureStackTrace(err); | | 2235 | + throw err; | | 2236 | + } | 2231 | 2237 | checkAborted(options.signal); // An abort during the write still wins. | 2232 | 2238 | return; | 2233 | 2239 | } | @@ -2244,29 +2250,32 @@ async function writeFile(path, data, options) { | 2244 | 2250 |
| 2245 | 2251 | /** | 2246 | 2252 | * Open + write + close as one thread pool round trip. | | 2253 | + * Throws synchronously if the permission model denies access, while the | | 2254 | + * caller's frames are still on the stack. The caller is responsible for | | 2255 | + * capturing the stack trace of a rejection, as no JS frames are on the stack | | 2256 | + * when the job completes. | 2247 | 2257 | * @param {string|Buffer} path Validated path | 2248 | 2258 | * @param {number} flagsNumber | 2249 | 2259 | * @param {number} mode | 2250 | 2260 | * @param {ArrayBufferView} data | 2251 | 2261 | * @returns {Promise<void>} | 2252 | 2262 | */ | 2253 | 2263 | function writeFileInOneRoundTrip(path, flagsNumber, mode, data) { | 2254 | | - return new Promise((resolve, reject) => { | 2255 | | - const job = new binding.WriteFileJob(path, flagsNumber, mode, data); | | 2264 | + const job = new binding.WriteFileJob(path, flagsNumber, mode, data); | | 2265 | + const promise = new Promise((resolve, reject) => { | 2256 | 2266 | job.ondone = (err) => { | 2257 | 2267 | if (err != null) { | 2258 | | - ErrorCaptureStackTrace(err, writeFileInOneRoundTrip); | 2259 | 2268 | reject(err); | 2260 | 2269 | } else { | 2261 | 2270 | resolve(); | 2262 | 2271 | } | 2263 | 2272 | }; | 2264 | | - const accessError = job.run(path); | 2265 | | - if (accessError !== undefined) { | 2266 | | - ErrorCaptureStackTrace(accessError, writeFileInOneRoundTrip); | 2267 | | - reject(accessError); | 2268 | | - } | 2269 | 2273 | }); | | 2274 | + const accessError = job.run(path); | | 2275 | + if (accessError !== undefined) { | | 2276 | + throw accessError; | | 2277 | + } | | 2278 | + return promise; | 2270 | 2279 | } | 2271 | 2280 |
| 2272 | 2281 | function isCustomIterable(obj) { | @@ -2309,7 +2318,15 @@ async function readFile(path, options) { | 2309 | 2318 | // files of up to one chunk; larger and non-regular files (size 0) come | 2310 | 2319 | // back as an open fd + size and are read by readFileHandle() as before. | 2311 | 2320 | path = getValidatedPath(path); | 2312 | | - const { 0: buffer, 1: fd, 2: size } = await readFileInOneRoundTrip(path, stringToFlags(flag)); | | 2321 | + const flagsNumber = stringToFlags(flag); | | 2322 | + let result; | | 2323 | + try { | | 2324 | + result = await readFileInOneRoundTrip(path, flagsNumber); | | 2325 | + } catch (err) { | | 2326 | + ErrorCaptureStackTrace(err); | | 2327 | + throw err; | | 2328 | + } | | 2329 | + const { 0: buffer, 1: fd, 2: size } = result; | 2313 | 2330 | if (fd === -1) { | 2314 | 2331 | checkAborted(options.signal); // An abort during the read still wins. | 2315 | 2332 | return options.encoding ? buffer.toString(options.encoding) : buffer; | @@ -2323,28 +2340,31 @@ async function readFile(path, options) { | 2323 | 2340 | } | 2324 | 2341 |
| 2325 | 2342 | /** | | 2343 | + * Throws synchronously if the permission model denies access, while the | | 2344 | + * caller's frames are still on the stack. The caller is responsible for | | 2345 | + * capturing the stack trace of a rejection, as no JS frames are on the stack | | 2346 | + * when the job completes. | 2326 | 2347 | * @param {string|Buffer} path Validated path | 2327 | 2348 | * @param {number} flagsNumber | 2328 | 2349 | * @returns {Promise<[Buffer|undefined, number, number|undefined]>} [buffer, -1] or [undefined, fd, size] | 2329 | 2350 | */ | 2330 | 2351 | function readFileInOneRoundTrip(path, flagsNumber) { | 2331 | | - return new Promise((resolve, reject) => { | 2332 | | - const job = new binding.ReadFileJob(path, flagsNumber, kReadFileBufferLength); | | 2352 | + const job = new binding.ReadFileJob(path, flagsNumber, kReadFileBufferLength); | | 2353 | + const promise = new Promise((resolve, reject) => { | 2333 | 2354 | job.ondone = (err, buffer, fd, size, closeErr) => { | 2334 | 2355 | const error = err ?? closeErr; | 2335 | 2356 | if (error != null) { | 2336 | | - ErrorCaptureStackTrace(error, readFileInOneRoundTrip); | 2337 | 2357 | reject(error); | 2338 | 2358 | } else { | 2339 | 2359 | resolve([buffer, fd, size]); | 2340 | 2360 | } | 2341 | 2361 | }; | 2342 | | - const accessError = job.run(path); | 2343 | | - if (accessError !== undefined) { | 2344 | | - ErrorCaptureStackTrace(accessError, readFileInOneRoundTrip); | 2345 | | - reject(accessError); | 2346 | | - } | 2347 | 2362 | }); | | 2363 | + const accessError = job.run(path); | | 2364 | + if (accessError !== undefined) { | | 2365 | + throw accessError; | | 2366 | + } | | 2367 | + return promise; | 2348 | 2368 | } | 2349 | 2369 |
| 2350 | 2370 | async function* _watch(filename, options = kEmptyObject) { | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-fs-promises.js +25Lines changed: 25 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -9,6 +9,7 @@ const fs = require('fs'); | 9 | 9 | const fsPromises = fs.promises; | 10 | 10 | const { | 11 | 11 | access, | | 12 | + appendFile, | 12 | 13 | chmod, | 13 | 14 | chown, | 14 | 15 | copyFile, | @@ -62,6 +63,30 @@ assert.strictEqual( | 62 | 63 | } | 63 | 64 | ).then(common.mustCall()); | 64 | 65 |
| | 66 | + assert.rejects( | | 67 | + readFile('this file does not exist'), | | 68 | + { | | 69 | + code: 'ENOENT', | | 70 | + stack: /at async assert\.rejects/ | | 71 | + } | | 72 | + ).then(common.mustCall()); | | 73 | + | | 74 | + assert.rejects( | | 75 | + writeFile(path.join('this dir does not exist', 'file'), 'data'), | | 76 | + { | | 77 | + code: 'ENOENT', | | 78 | + stack: /at async assert\.rejects/ | | 79 | + } | | 80 | + ).then(common.mustCall()); | | 81 | + | | 82 | + assert.rejects( | | 83 | + appendFile(path.join('this dir does not exist', 'file'), 'data'), | | 84 | + { | | 85 | + code: 'ENOENT', | | 86 | + stack: /at async assert\.rejects/ | | 87 | + } | | 88 | + ).then(common.mustCall()); | | 89 | + | 65 | 90 | assert.rejects( | 66 | 91 | access(__filename, 8), | 67 | 92 | { | |
Collapse file Copy file name to clipboard +42Lines changed: 42 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | + | | 3 | +require('../common'); | | 4 | +const assert = require('assert'); | | 5 | +const { spawnSync } = require('child_process'); | | 6 | +const tmpdir = require('../common/tmpdir'); | | 7 | + | | 8 | +tmpdir.refresh(); | | 9 | + | | 10 | +// The permission model denies access before the first await, so the stack | | 11 | +// still has the frames of a caller that does not await the promise. | | 12 | +const { status, stderr } = spawnSync( | | 13 | + process.execPath, | | 14 | + [ | | 15 | + '--permission', | | 16 | + '-e', | | 17 | + ` | | 18 | + const assert = require('assert'); | | 19 | + const { readFile, writeFile, appendFile } = require('fs/promises'); | | 20 | + const target = ${JSON.stringify(tmpdir.resolve('file'))}; | | 21 | +
| | 22 | + function readCaller() { return readFile(target); } | | 23 | + function writeCaller() { return writeFile(target, 'data'); } | | 24 | + function appendCaller() { return appendFile(target, 'data'); } | | 25 | + async function awaitAppendCaller() { await appendFile(target, 'data'); } | | 26 | +
| | 27 | + const cases = [ | | 28 | + [readCaller, /at readCaller /], | | 29 | + [writeCaller, /at writeCaller /], | | 30 | + [appendCaller, /at appendCaller /], | | 31 | + [awaitAppendCaller, /at awaitAppendCaller /], | | 32 | + ]; | | 33 | + let checked = 0; | | 34 | + for (const [caller, stack] of cases) { | | 35 | + assert.rejects(caller(), { code: 'ERR_ACCESS_DENIED', stack }) | | 36 | + .then(() => checked++); | | 37 | + } | | 38 | + process.on('exit', () => assert.strictEqual(checked, cases.length)); | | 39 | + `, | | 40 | + ], | | 41 | +); | | 42 | +assert.strictEqual(status, 0, stderr.toString()); |
|