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

Automatically clean up after an error is thrown #31

Merged
merged 4 commits into from
Mar 17, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 9 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,15 @@ const writeStream = async (filePath, data) => pipeline(data, fs.createWriteStrea
const createTask = (tempyFunction, {extraArguments = 0} = {}) => async (...arguments_) => {
const [callback, options] = arguments_.slice(extraArguments);
const result = await tempyFunction(...arguments_.slice(0, extraArguments), options);
const returnValue = await callback(result);

let returnValue;
try {
returnValue = await callback(result);
} catch (error) {
await del(result, {force: true});
Richienb marked this conversation as resolved.
Show resolved Hide resolved
throw error;
}

await del(result, {force: true});
return returnValue;
};
Expand Down
13 changes: 13 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,19 @@ test('.file.task()', async t => {
t.false(await pathExists(temporaryFilePath));
});

test('.task() error', async t => {
Richienb marked this conversation as resolved.
Show resolved Hide resolved
let temporaryDirectoryPath;
await t.throwsAsync(tempy.directory.task(async temporaryDirectory => {
temporaryDirectoryPath = temporaryDirectory;
throw new Error('Catch me if you can!');
}), {
instanceOf: Error,
message: 'Catch me if you can!'
});

t.false(await pathExists(temporaryDirectoryPath));
});

test('.directory()', t => {
const prefix = 'name_';

Expand Down