Skip to content

Commit 87b79ce

Browse files
montymxbflovilmart
authored andcommitted
Handle possible afterSave exception (#4293)
* capture and log exceptions caused by afterFind * Wording * Consolidated promise chaining * use logger instead of console
1 parent c2fc0f5 commit 87b79ce

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

spec/CloudCode.spec.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,6 +1076,19 @@ describe('Cloud Code', () => {
10761076
});
10771077
});
10781078

1079+
/**
1080+
* Verifies that an afterSave hook throwing an exception
1081+
* will not prevent a successful save response from being returned
1082+
*/
1083+
it('should succeed on afterSave exception', (done) => {
1084+
Parse.Cloud.afterSave("AfterSaveTestClass", function () {
1085+
throw "Exception";
1086+
});
1087+
const AfterSaveTestClass = Parse.Object.extend('AfterSaveTestClass');
1088+
const obj = new AfterSaveTestClass();
1089+
obj.save().then(done, done.fail);
1090+
});
1091+
10791092
describe('cloud jobs', () => {
10801093
it('should define a job', (done) => {
10811094
expect(() => {

src/RestWrite.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ var triggers = require('./triggers');
1313
var ClientSDK = require('./ClientSDK');
1414
import RestQuery from './RestQuery';
1515
import _ from 'lodash';
16+
import logger from './logger';
1617

1718
// query and data are both provided in REST API format. So data
1819
// types are encoded by plain old objects.
@@ -1121,7 +1122,10 @@ RestWrite.prototype.runAfterTrigger = function() {
11211122
this.config.liveQueryController.onAfterSave(updatedObject.className, updatedObject, originalObject);
11221123

11231124
// Run afterSave trigger
1124-
return triggers.maybeRunTrigger(triggers.Types.afterSave, this.auth, updatedObject, originalObject, this.config);
1125+
return triggers.maybeRunTrigger(triggers.Types.afterSave, this.auth, updatedObject, originalObject, this.config)
1126+
.catch(function(err) {
1127+
logger.warn('afterSave caught an error', err);
1128+
})
11251129
};
11261130

11271131
// A helper to figure out what location this operation happens at.

0 commit comments

Comments
 (0)