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

Catch unhandled promise rejections #196

Merged
merged 3 commits into from
Mar 30, 2019
Merged
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
34 changes: 17 additions & 17 deletions lib/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,32 @@ const RollbackReason = function (error) {
};

const start = () => {
return hook => new Promise(resolve => {
return hook => {
if (!hook.service.Model || typeof hook.service.Model.transaction !== 'function') {
return resolve(hook);
return Promise.resolve(hook);
}

if (hook.params.transaction) {
hook.params.transaction.count += 1;
return resolve(hook);
return Promise.resolve(hook);
}

const transaction = {};

transaction.promise = hook.service.Model.transaction(trx => {
transaction.trx = trx;
transaction.id = Date.now();
transaction.count = 0;
return new Promise((resolve, reject) => {
const id = Date.now();

hook.params.transaction = transaction;

debug('started a new transaction %s', transaction.id);
return resolve(hook);
}).catch((error) => {
if (error instanceof RollbackReason) return;
throw error;
debug('started a new transaction %s', id);
hook.service.Model
.transaction(trx => resolve({
id,
trx,
count: 0
}))
.catch(err => reject(err));
}).then(trx => {
hook.params.transaction = trx;
return hook;
});
});
};
};

const end = () => {
Expand Down