Start a new topic

PostFetch hook broken

I randomly started getting



{"error":"BLViolationError","description":"TheBusinessLogicscriptviolatedaconstraint.Seedebugmessagefordetails.","debug":"Blocked,reason:response.complete()orresponse.continue()wascalledmultipletimes,triggeredbythepostscollection'sonPostFetchhook,remainingtime:2515"}



when I remove the hook it's fine.



when I change the hook to



function onPostFetch(request, response, modules) {

var context = modules.backendContext;

debug(context.getSecurityContext());

response.complete();

}



it still happens...



so it seems like post fetch hooks are just dead for me

Can you post your complete code? This code will throw 'debug is undefined'.



This error happens when your asynchronous code paths are not properly isolated..



if (err) {

response.complete()

}

//continue with processing

response.complete(); //WRONG



must be :



if (err) {

response.complete()

} else {

//continue with processing

response.complete();

}
yeah that was a copy/paste bad on my side...



function onPostFetch(request, response, modules) {

response.complete();

}



throws the same error
currently the full code is:



```

function onPostFetch(request, response, modules) {

var logger = modules.logger,

error = modules.logger.error,

debug = modules.logger.info,

collectionAccess = modules.collectionAccess,

Users = collectionAccess.collection('user'),

username = request.username,

context = modules.backendContext;



if(context.getSecurityContext() == 'master') {

response.complete();

}



Users.findOne({ username: username }, function(err, doc) {

if(err) error(err);

if(!doc) {

response.complete();

} else {

if(doc.likes) {

var likes = doc.likes;



if(response.body.length > 0) {

response.body.forEach(function (element, index, array) {

response.body[index].i_like = (likes.indexOf(element._id) >= 0);

}, this);

response.complete();

} else {

response.body.i_like = (likes.indexOf(response.body._id) >= 0);

response.complete();

}

} else {

response.complete();

}

}

});



}

```
although that code has never worked, never got a chance to verify it.



last working version was



```

function onPostFetch(request, response, modules) {

var logger = modules.logger,

error = modules.logger.error,

debug = modules.logger.info,

collectionAccess = modules.collectionAccess,

Users = collectionAccess.collection('user'),

username = request.username,

context = modules.backendContext;



Users.findOne({ username: username }, function(err, doc) {

if(err) error(err);

if(!doc) {

response.complete();

} else {

if(doc.likes) {

var likes = doc.likes;



if(response.body.length > 0) {

response.body.forEach(function (element, index, array) {

response.body[index].i_like = (likes.indexOf(element._id) >= 0);

}, this);

} else {

response.body.i_like = (likes.indexOf(response.body._id) >= 0);

}

response.complete();

} else {

response.complete();

}

}

});



}

```
Right, so your first 'if' needs an else.
the non-threading of this is annoying btw...



I only added the context stuff later on... it broke when that wasn't there.



and



```

function onPostFetch(request, response, modules) {

response.complete();

}

```

failed with the same business logic error



and even the wrong code (I tried it)



```

function onPostFetch(request, response, modules) {

var context = modules.backendContext;

debug(context.getSecurityContext());

response.complete();

}

```

wasn't throwing 'debug undefined' it was throwing the same constraint issue



Now, it has just started working again... what did you change?
There are two issues here. One is that your code was faulty. It seems like the cause of that was your missing else statement.

The second issue is that because you were calling the faulty code multiple times, your code was blacklisted. If you look up your error message on the forum, you will find http://support.kinvey.com/hc/communities/public/questions/200315157-All-BL-failing-with-Violation-Error. This is why it started working "all of a sudden" - the remaining time expired (notice remainingtime:2515 in your debug msg). Before the time expired, it was erroring out even with the no-op code.



>the non-threading of this is annoying btw...

Yes, sorry. We had higher expectations from Zendesk, looking to migrate to something else.



Okay but you're missing something though (which I caused by not filtering all my pasted code and not explaining it all properly).



```

function onPostFetch(request, response, modules) {

var logger = modules.logger,

error = modules.logger.error,

debug = modules.logger.info,

collectionAccess = modules.collectionAccess,

Users = collectionAccess.collection('user'),

username = request.username;



Users.findOne({ username: username }, function(err, doc) {

if(err) error(err);

if(!doc) {

response.complete();

} else {

if(doc.likes) {

var likes = doc.likes;



if(response.body.length > 0) {

response.body.forEach(function (element, index, array) {

response.body[index].i_like = (likes.indexOf(element._id) >= 0);

}, this);

} else {

response.body.i_like = (likes.indexOf(response.body._id) >= 0);

}

response.complete();

} else {

response.complete();



}

}



});



}

```

was working perfectly fine for the past couple weeks. It was even working today up until about 15 minutes before I posted here. Then it started throwing that error.



I was returning 100 records, which isn't much. I tried dropping it to 10 record, still happened. Then I tried other things, some of which would have broken such as

```

function onPostFetch(request, response, modules) {

var context = modules.backendContext;

debug(context.getSecurityContext());

response.complete();

}

```

which didn't return "undefined" it returned the blacklist thing. etc etc



why did the first chunk of code work for so long, then just stop working today?



and why would doing

```

function onPostFetch(request, response, modules) {

response.complete();

}

```

throw the same error, even though there's no possible way for that to cause the error? Does it blacklist ALL calls? How long does the blacklist last and how do we get off it if it occurs?



I get that I can write bad code :) what bothers me is when I don't change anything and it just fails for no reason.
I don't mean to belabor this point but this call is on the core functionality of the app...

```

function onPostFetch(request, response, modules) {

var logger = modules.logger,

error = modules.logger.error,

debug = modules.logger.info,

collectionAccess = modules.collectionAccess,

Users = collectionAccess.collection('user'),

username = request.username;



Users.findOne({ username: username }, function(err, doc) {

if(err) error(err);

if(!doc) {

response.complete();

} else {

if(doc.likes) {

var likes = doc.likes;



if(response.body.length > 0) {

response.body.forEach(function (element, index, array) {

response.body[index].i_like = (likes.indexOf(element._id) >= 0);

}, this);

} else {

response.body.i_like = (likes.indexOf(response.body._id) >= 0);

}

response.complete();

} else {

response.complete();

}

}



});



}

```

worked for a couple weeks, and then just stopped working with the Business Logic Violation error yesterday.



What caused it to fail?
> Does it blacklist ALL calls?

Yes.



>How long does the blacklist last and how do we get off it if it occurs?

It lasts 1 hour. There is currently no way to stop it except for waiting the period - the best way to avoid it is to make sure your code is correct.



>when I don't change anything and it just fails for no reason

So, it is not for no reason. l looked up the code version at the time the event occurred. You had:



if(doc.likes) {

//some logic

response.complete();

}

response.complete();



This causes response.complete to be called twice if doc.likes is defined. Later you added an else.

Keep in mind that BL code is not something that runs by itself - it acts on inputs and your data, and depending on that different code paths can be triggered. This may create the perception of "nothing changed", but what actually did was the data or the input.





Okay, so you were looking at v326?



Totally my fault for missing the else there. The only mystery I guess is why that worked for awhile, and then started failing. That's the logic behind my "no reason" for it to happen.



Blacklisting for an hour right off the bat seems harsh, especially if the logic gets complex, and no way to test it locally.

Login or Signup to post a comment