Skip to content

Commit

Permalink
fix(model): throw NOT_FOUND error when trying to update a Model that …
Browse files Browse the repository at this point in the history
…does not exist (#181)

fix #164
  • Loading branch information
sebelga committed Sep 10, 2019
1 parent 15a713a commit cc11e02
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 39 deletions.
36 changes: 12 additions & 24 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,6 @@ class Model extends Entity {
const _this = this;

let entityUpdated;
let error = {};

const key = this.key(id, ancestors, namespace);
const replace = options && options.replace === true;
Expand All @@ -223,7 +222,8 @@ class Model extends Entity {
*/
if (replace) {
return saveEntity({ key, data })
.then(onEntityUpdated, onUpdateError);
.then(onEntityUpdated)
.catch(onUpdateError);
}

if (typeof transaction === 'undefined' || transaction === null) {
Expand All @@ -232,22 +232,21 @@ class Model extends Entity {
return transaction
.run()
.then(getAndUpdate)
.catch(onTransactionError);
.catch(onUpdateError);
}

if (transaction.constructor.name !== 'Transaction') {
return Promise.reject(new Error('Transaction needs to be a gcloud Transaction'));
}

return getAndUpdate()
.catch(onTransactionError);
return getAndUpdate();

// ---------------------------------------------------------

function getAndUpdate() {
return getEntity()
.then(saveEntity)
.then(onEntityUpdated, onUpdateError);
.then(onEntityUpdated);
}

function getEntity() {
Expand All @@ -257,11 +256,10 @@ class Model extends Entity {
const entity = getData[0];

if (typeof entity === 'undefined') {
error = new GstoreError(
throw (new GstoreError(
errorCodes.ERR_ENTITY_NOT_FOUND,
`Entity { ${id.toString()} } to update not found`
);
throw (error);
));
}

extend(false, entity, data);
Expand All @@ -272,10 +270,6 @@ class Model extends Entity {
};

return result;
})
.catch(err => {
error = err;
return err;
});
}

Expand Down Expand Up @@ -319,14 +313,16 @@ class Model extends Entity {
}

function onUpdateError(err) {
error = err;
const error = Array.isArray(err) ? err[0] : err;
if (internalTransaction) {
// If we created the Transaction instance internally for the update, we rollback it
// otherwise we leave the rollback() call to the transaction creator
return transaction.rollback().then(() => onTransactionError([err]));
return transaction.rollback().then(() => {
throw error;
});
}

return onTransactionError([err]);
throw error;
}

function onTransactionSuccess() {
Expand All @@ -349,14 +345,6 @@ class Model extends Entity {

return entityUpdated;
}

function onTransactionError(transactionError = {}) {
const apiResponse = transactionError && Array.isArray(transactionError)
? transactionError[0]
: transactionError;
extend(apiResponse, transactionError);
throw apiResponse;
}
}

static delete(id, ancestors, namespace, transaction, key, options = {}) {
Expand Down
34 changes: 21 additions & 13 deletions test/integration/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,20 +258,20 @@ describe('Model (Integration Tests)', () => {

describe('update()', () => {
describe('transaction()', () => {
it('should update entity inside a transaction', () => {
const userSchema = new Schema({
name: { joi: Joi.string().required() },
lastname: { joi: Joi.string() },
password: { joi: Joi.string() },
coins: { joi: Joi.number().integer().min(0) },
email: { joi: Joi.string().email() },
createdAt: { joi: Joi.date() },
access_token: { joi: [Joi.string(), Joi.number()] },
birthyear: { joi: Joi.number().integer().min(1900).max(2013) },
}, { joi: true });

const User = gstore.model('ModelTestsTransaction-User', userSchema);
const userSchema = new Schema({
name: { joi: Joi.string().required() },
lastname: { joi: Joi.string() },
password: { joi: Joi.string() },
coins: { joi: Joi.number().integer().min(0) },
email: { joi: Joi.string().email() },
createdAt: { joi: Joi.date() },
access_token: { joi: [Joi.string(), Joi.number()] },
birthyear: { joi: Joi.number().integer().min(1900).max(2013) },
}, { joi: true });

const User = gstore.model('ModelTestsTransaction-User', userSchema);

it('should update entity inside a transaction', () => {
function transferCoins(fromUser, toUser, amount) {
return new Promise((resolve, reject) => {
const transaction = gstore.transaction();
Expand Down Expand Up @@ -319,6 +319,14 @@ describe('Model (Integration Tests)', () => {
return transferCoins(fromUser, toUser, 1000);
});
});

it('should throw a 404 Not found when trying to update a non existing entity', done => {
User.update(randomName(), { name: 'test' })
.catch(err => {
expect(err.code).equal('ERR_ENTITY_NOT_FOUND');
done();
});
});
});
});

Expand Down
5 changes: 3 additions & 2 deletions test/model-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -709,13 +709,14 @@ describe('Model', () => {
});
});

it('should return error if any while saving', () => {
it('should return error if any while saving', done => {
transaction.run.restore();
const error = { code: 500, message: 'Houston wee need you.' };
sinon.stub(transaction, 'run').rejects([error]);

return GstoreModel.update(123).catch(err => {
GstoreModel.update(123).catch(err => {
expect(err).equal(error);
done();
});
});

Expand Down

0 comments on commit cc11e02

Please sign in to comment.