diff --git a/src/error-handler.js b/src/error-handler.js index 52babfb..be4ad86 100644 --- a/src/error-handler.js +++ b/src/error-handler.js @@ -73,9 +73,9 @@ export default function errorHandler (error) { if (typeof error.code === 'string' && error.severity && error.routine) { // Omit query information const messages = error.message.split('-'); - error.message = messages[messages.length - 1] + error.message = messages[messages.length - 1]; - switch (error.code.slice(0,2)) { + switch (error.code.slice(0, 2)) { case '22': case '23': feathersError = new errors.BadRequest(error); diff --git a/src/hooks.js b/src/hooks.js index b44d15e..71b5cf2 100644 --- a/src/hooks.js +++ b/src/hooks.js @@ -7,11 +7,17 @@ const start = (options) => { return resolve(hook); } + if (hook.params.transaction) { + hook.params.transaction.count += 1; + return resolve(hook); + } + hook.service.Model.transaction(trx => { const id = Date.now(); hook.params.transaction = { trx, - id + id, + count: 0 }; debug('started a new transaction %s', id); return resolve(hook); @@ -22,7 +28,15 @@ const start = (options) => { const end = (options) => { return (hook) => { if (hook.params.transaction) { - const { trx, id } = hook.params.transaction; + const { trx, id, count } = hook.params.transaction; + + if (count > 0) { + hook.params.transaction.count -= 1; + return Promise.resolve(hook); + } + + hook.params.transaction = undefined; + return trx.commit() .then(() => debug('finished transaction %s with success', id)) .then(hook);