Skip to content

Commit cc11e02

Browse files
authored
fix(model): throw NOT_FOUND error when trying to update a Model that does not exist (#181)
fix #164
1 parent 15a713a commit cc11e02

File tree

3 files changed

+36
-39
lines changed

3 files changed

+36
-39
lines changed

lib/model.js

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,6 @@ class Model extends Entity {
210210
const _this = this;
211211

212212
let entityUpdated;
213-
let error = {};
214213

215214
const key = this.key(id, ancestors, namespace);
216215
const replace = options && options.replace === true;
@@ -223,7 +222,8 @@ class Model extends Entity {
223222
*/
224223
if (replace) {
225224
return saveEntity({ key, data })
226-
.then(onEntityUpdated, onUpdateError);
225+
.then(onEntityUpdated)
226+
.catch(onUpdateError);
227227
}
228228

229229
if (typeof transaction === 'undefined' || transaction === null) {
@@ -232,22 +232,21 @@ class Model extends Entity {
232232
return transaction
233233
.run()
234234
.then(getAndUpdate)
235-
.catch(onTransactionError);
235+
.catch(onUpdateError);
236236
}
237237

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

242-
return getAndUpdate()
243-
.catch(onTransactionError);
242+
return getAndUpdate();
244243

245244
// ---------------------------------------------------------
246245

247246
function getAndUpdate() {
248247
return getEntity()
249248
.then(saveEntity)
250-
.then(onEntityUpdated, onUpdateError);
249+
.then(onEntityUpdated);
251250
}
252251

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

259258
if (typeof entity === 'undefined') {
260-
error = new GstoreError(
259+
throw (new GstoreError(
261260
errorCodes.ERR_ENTITY_NOT_FOUND,
262261
`Entity { ${id.toString()} } to update not found`
263-
);
264-
throw (error);
262+
));
265263
}
266264

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

274272
return result;
275-
})
276-
.catch(err => {
277-
error = err;
278-
return err;
279273
});
280274
}
281275

@@ -319,14 +313,16 @@ class Model extends Entity {
319313
}
320314

321315
function onUpdateError(err) {
322-
error = err;
316+
const error = Array.isArray(err) ? err[0] : err;
323317
if (internalTransaction) {
324318
// If we created the Transaction instance internally for the update, we rollback it
325319
// otherwise we leave the rollback() call to the transaction creator
326-
return transaction.rollback().then(() => onTransactionError([err]));
320+
return transaction.rollback().then(() => {
321+
throw error;
322+
});
327323
}
328324

329-
return onTransactionError([err]);
325+
throw error;
330326
}
331327

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

350346
return entityUpdated;
351347
}
352-
353-
function onTransactionError(transactionError = {}) {
354-
const apiResponse = transactionError && Array.isArray(transactionError)
355-
? transactionError[0]
356-
: transactionError;
357-
extend(apiResponse, transactionError);
358-
throw apiResponse;
359-
}
360348
}
361349

362350
static delete(id, ancestors, namespace, transaction, key, options = {}) {

test/integration/model.js

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -258,20 +258,20 @@ describe('Model (Integration Tests)', () => {
258258

259259
describe('update()', () => {
260260
describe('transaction()', () => {
261-
it('should update entity inside a transaction', () => {
262-
const userSchema = new Schema({
263-
name: { joi: Joi.string().required() },
264-
lastname: { joi: Joi.string() },
265-
password: { joi: Joi.string() },
266-
coins: { joi: Joi.number().integer().min(0) },
267-
email: { joi: Joi.string().email() },
268-
createdAt: { joi: Joi.date() },
269-
access_token: { joi: [Joi.string(), Joi.number()] },
270-
birthyear: { joi: Joi.number().integer().min(1900).max(2013) },
271-
}, { joi: true });
272-
273-
const User = gstore.model('ModelTestsTransaction-User', userSchema);
261+
const userSchema = new Schema({
262+
name: { joi: Joi.string().required() },
263+
lastname: { joi: Joi.string() },
264+
password: { joi: Joi.string() },
265+
coins: { joi: Joi.number().integer().min(0) },
266+
email: { joi: Joi.string().email() },
267+
createdAt: { joi: Joi.date() },
268+
access_token: { joi: [Joi.string(), Joi.number()] },
269+
birthyear: { joi: Joi.number().integer().min(1900).max(2013) },
270+
}, { joi: true });
271+
272+
const User = gstore.model('ModelTestsTransaction-User', userSchema);
274273

274+
it('should update entity inside a transaction', () => {
275275
function transferCoins(fromUser, toUser, amount) {
276276
return new Promise((resolve, reject) => {
277277
const transaction = gstore.transaction();
@@ -319,6 +319,14 @@ describe('Model (Integration Tests)', () => {
319319
return transferCoins(fromUser, toUser, 1000);
320320
});
321321
});
322+
323+
it('should throw a 404 Not found when trying to update a non existing entity', done => {
324+
User.update(randomName(), { name: 'test' })
325+
.catch(err => {
326+
expect(err.code).equal('ERR_ENTITY_NOT_FOUND');
327+
done();
328+
});
329+
});
322330
});
323331
});
324332

test/model-test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -709,13 +709,14 @@ describe('Model', () => {
709709
});
710710
});
711711

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

717-
return GstoreModel.update(123).catch(err => {
717+
GstoreModel.update(123).catch(err => {
718718
expect(err).equal(error);
719+
done();
719720
});
720721
});
721722

0 commit comments

Comments
 (0)