Skip to content

Commit 04aab31

Browse files
authored
Merge pull request #35 from paulmelnikow/errors
Propagate errors instead of crashing
2 parents 6c54ec6 + 1b06db8 commit 04aab31

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

index.js

+6-8
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ const tmp = require("tmp");
33

44
// file
55
module.exports.fileSync = tmp.fileSync;
6-
const fileWithOptions = promisify(
7-
(options, cb) => tmp.file(
8-
options,
9-
(err, path, fd, cleanup) => cb(err, {path, fd, cleanup: promisify(cleanup)})
6+
const fileWithOptions = promisify((options, cb) =>
7+
tmp.file(options, (err, path, fd, cleanup) =>
8+
err ? cb(err) : cb(undefined, { path, fd, cleanup: promisify(cleanup) })
109
)
1110
);
1211
module.exports.file = async (options) => fileWithOptions(options);
@@ -23,10 +22,9 @@ module.exports.withFile = async function withFile(fn, options) {
2322

2423
// directory
2524
module.exports.dirSync = tmp.dirSync;
26-
const dirWithOptions = promisify(
27-
(options, cb) => tmp.dir(
28-
options,
29-
(err, path, cleanup) => cb(err, {path, cleanup: promisify(cleanup)})
25+
const dirWithOptions = promisify((options, cb) =>
26+
tmp.dir(options, (err, path, cleanup) =>
27+
err ? cb(err) : cb(undefined, { path, cleanup: promisify(cleanup) })
3028
)
3129
);
3230
module.exports.dir = async (options) => dirWithOptions(options);

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tmp-promise",
3-
"version": "2.0.1",
3+
"version": "2.0.2",
44
"description": "The tmp package with promises support and disposers.",
55
"main": "index.js",
66
"types": "index.d.ts",

test.js

+18
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,15 @@ describe('file()', function()
4848
assert.ok(result.path.includes(prefix))
4949
})
5050
})
51+
52+
it('propagates errors', async function() {
53+
try {
54+
await tmp.file({ dir: 'nonexistent-path' });
55+
throw Error('Expected to throw');
56+
} catch (e) {
57+
assert.ok(e.message.startsWith('ENOENT: no such file or directory'));
58+
}
59+
});
5160
})
5261

5362
async function checkDirResult(result) {
@@ -84,6 +93,15 @@ describe('dir()', function()
8493
assert.ok(result.path.includes(prefix))
8594
})
8695
})
96+
97+
it('propagates errors', async function() {
98+
try {
99+
await tmp.dir({ dir: 'nonexistent-path' });
100+
throw Error('Expected to throw');
101+
} catch (e) {
102+
assert.ok(e.message.startsWith('ENOENT: no such file or directory'));
103+
}
104+
});
87105
})
88106

89107
describe('withFile()', function()

0 commit comments

Comments
 (0)