Skip to content

Commit

Permalink
Propagate errors instead of crashing
Browse files Browse the repository at this point in the history
Fix #34
  • Loading branch information
paulmelnikow committed Jul 3, 2019
1 parent 6c54ec6 commit 104f457
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
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
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

0 comments on commit 104f457

Please sign in to comment.