|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +// Test that async fs callback errors include stack traces. |
| 4 | +// Regression test for https://github.com/nodejs/node/issues/30944 |
| 5 | +// Prior to this fix, async fs callback errors had no stack frames |
| 6 | +// (only the error message), making debugging very difficult. |
| 7 | + |
| 8 | +const common = require('../common'); |
| 9 | +const assert = require('assert'); |
| 10 | +const fs = require('fs'); |
| 11 | + |
| 12 | +const nonexistentFile = '/tmp/.node-test-nonexistent-' + process.pid; |
| 13 | + |
| 14 | +// Helper to check that an error has stack frames |
| 15 | +function assertHasStackFrames(err, operation) { |
| 16 | + assert.ok(err instanceof Error, `${operation}: expected Error instance`); |
| 17 | + assert.ok(typeof err.stack === 'string', `${operation}: expected string stack`); |
| 18 | + assert.ok( |
| 19 | + err.stack.includes('\n at '), |
| 20 | + `${operation}: error should have stack frames but got:\n${err.stack}` |
| 21 | + ); |
| 22 | +} |
| 23 | + |
| 24 | +// Test: fs.stat with nonexistent file |
| 25 | +fs.stat(nonexistentFile, common.mustCall((err) => { |
| 26 | + assertHasStackFrames(err, 'stat'); |
| 27 | +})); |
| 28 | + |
| 29 | +// Test: fs.lstat with nonexistent file |
| 30 | +fs.lstat(nonexistentFile, common.mustCall((err) => { |
| 31 | + assertHasStackFrames(err, 'lstat'); |
| 32 | +})); |
| 33 | + |
| 34 | +// Test: fs.access with nonexistent file |
| 35 | +fs.access(nonexistentFile, common.mustCall((err) => { |
| 36 | + assertHasStackFrames(err, 'access'); |
| 37 | +})); |
| 38 | + |
| 39 | +// Test: fs.open with nonexistent file |
| 40 | +fs.open(nonexistentFile, 'r', common.mustCall((err) => { |
| 41 | + assertHasStackFrames(err, 'open'); |
| 42 | +})); |
| 43 | + |
| 44 | +// Test: fs.readdir with nonexistent directory |
| 45 | +fs.readdir(nonexistentFile, common.mustCall((err) => { |
| 46 | + assertHasStackFrames(err, 'readdir'); |
| 47 | +})); |
| 48 | + |
| 49 | +// Test: fs.rename with nonexistent source |
| 50 | +fs.rename(nonexistentFile, nonexistentFile + '.bak', common.mustCall((err) => { |
| 51 | + assertHasStackFrames(err, 'rename'); |
| 52 | +})); |
| 53 | + |
| 54 | +// Test: fs.unlink with nonexistent file |
| 55 | +fs.unlink(nonexistentFile, common.mustCall((err) => { |
| 56 | + assertHasStackFrames(err, 'unlink'); |
| 57 | +})); |
| 58 | + |
| 59 | +// Test: fs.rmdir with nonexistent directory |
| 60 | +fs.rmdir(nonexistentFile, common.mustCall((err) => { |
| 61 | + assertHasStackFrames(err, 'rmdir'); |
| 62 | +})); |
| 63 | + |
| 64 | +// Test: fs.chmod with nonexistent file |
| 65 | +fs.chmod(nonexistentFile, 0o644, common.mustCall((err) => { |
| 66 | + assertHasStackFrames(err, 'chmod'); |
| 67 | +})); |
| 68 | + |
| 69 | +// Test: fs.readlink with nonexistent file |
| 70 | +fs.readlink(nonexistentFile, common.mustCall((err) => { |
| 71 | + assertHasStackFrames(err, 'readlink'); |
| 72 | +})); |
| 73 | + |
| 74 | +// Test: fs.realpath with nonexistent file |
| 75 | +fs.realpath(nonexistentFile, common.mustCall((err) => { |
| 76 | + assertHasStackFrames(err, 'realpath'); |
| 77 | +})); |
| 78 | + |
| 79 | +// Test: fs.mkdir with nonexistent parent |
| 80 | +fs.mkdir(nonexistentFile + '/sub/dir', common.mustCall((err) => { |
| 81 | + assertHasStackFrames(err, 'mkdir'); |
| 82 | +})); |
| 83 | + |
| 84 | +// Test: fs.copyFile with nonexistent source |
| 85 | +fs.copyFile(nonexistentFile, nonexistentFile + '.copy', common.mustCall((err) => { |
| 86 | + assertHasStackFrames(err, 'copyFile'); |
| 87 | +})); |
| 88 | + |
| 89 | +// Test: fs.readFile with nonexistent file |
| 90 | +fs.readFile(nonexistentFile, common.mustCall((err) => { |
| 91 | + assertHasStackFrames(err, 'readFile'); |
| 92 | +})); |
| 93 | + |
| 94 | +// Test: Verify that errors that ALREADY have stack frames are not modified |
| 95 | +{ |
| 96 | + const originalErr = new Error('test error'); |
| 97 | + const originalStack = originalErr.stack; |
| 98 | + assert.ok(originalStack.includes('\n at '), |
| 99 | + 'Sanity check: JS errors should have stack frames'); |
| 100 | +} |
| 101 | + |
| 102 | +// Test: Verify sync errors still work (not affected by our change) |
| 103 | +assert.throws( |
| 104 | + () => fs.readFileSync(nonexistentFile), |
| 105 | + (err) => { |
| 106 | + assertHasStackFrames(err, 'readFileSync'); |
| 107 | + return true; |
| 108 | + } |
| 109 | +); |
0 commit comments