Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Propagate errors instead of crashing #35

Merged
merged 2 commits into from
Jul 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ const tmp = require("tmp");

// file
module.exports.fileSync = tmp.fileSync;
const fileWithOptions = promisify(
(options, cb) => tmp.file(
options,
(err, path, fd, cleanup) => cb(err, {path, fd, cleanup: promisify(cleanup)})
const fileWithOptions = promisify((options, cb) =>
tmp.file(options, (err, path, fd, cleanup) =>
err ? cb(err) : cb(undefined, { path, fd, cleanup: promisify(cleanup) })
)
);
module.exports.file = async (options) => fileWithOptions(options);
Expand All @@ -23,10 +22,9 @@ module.exports.withFile = async function withFile(fn, options) {

// directory
module.exports.dirSync = tmp.dirSync;
const dirWithOptions = promisify(
(options, cb) => tmp.dir(
options,
(err, path, cleanup) => cb(err, {path, cleanup: promisify(cleanup)})
const dirWithOptions = promisify((options, cb) =>
tmp.dir(options, (err, path, cleanup) =>
err ? cb(err) : cb(undefined, { path, cleanup: promisify(cleanup) })
)
);
module.exports.dir = async (options) => dirWithOptions(options);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tmp-promise",
"version": "2.0.1",
"version": "2.0.2",
"description": "The tmp package with promises support and disposers.",
"main": "index.js",
"types": "index.d.ts",
Expand Down
18 changes: 18 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ describe('file()', function()
assert.ok(result.path.includes(prefix))
})
})

it('propagates errors', async function() {
try {
await tmp.file({ dir: 'nonexistent-path' });
throw Error('Expected to throw');
} catch (e) {
assert.ok(e.message.startsWith('ENOENT: no such file or directory'));
}
});
})

async function checkDirResult(result) {
Expand Down Expand Up @@ -84,6 +93,15 @@ describe('dir()', function()
assert.ok(result.path.includes(prefix))
})
})

it('propagates errors', async function() {
try {
await tmp.dir({ dir: 'nonexistent-path' });
throw Error('Expected to throw');
} catch (e) {
assert.ok(e.message.startsWith('ENOENT: no such file or directory'));
}
});
})

describe('withFile()', function()
Expand Down