Skip to content
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
4 changes: 2 additions & 2 deletions src/error-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
18 changes: 16 additions & 2 deletions src/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down