Start a new topic
Answered

Timeout when try to throw custom error inside findAsync

Hello,

I noticed not usual behavior for throwing an error inside then handler of findAsync.
Here is the test code that I used in custom endpoint

function onRequest(request, response, modules) {
var collectionAccess = modules.collectionAccess;

collectionAccess.collection('Collection').findAsync({})
.then(function(entities) {
if(entities.length > 0) {
return response.error('Count is more than 0');
}
return response.complete(200)
});
}

The problem is that I have a timeout exception here. My 'Collection' isn't empty. It's only for promise-based functions. 
For non promise-based function I received an error as expected

function onRequest(request, response, modules) {
var collectionAccess = modules.collectionAccess;

collectionAccess.collection('Collection').find({},function(entities) {
if(entities.length > 0) {
return response.error('Count is more than 0');
}
return response.complete(200)
});
}


And now I'm afraid to use promise-based collection functions because of possible timeouts.

Regards, Yegor


Best Answer

The issue here is that the response.error call needs to be wrapped with setImmediate. For example:

}).catch(function(err) {
    logger.info("err: " + err.stack);
    setImmediate(function() {
	    return response.error(500);
    });
  });

 This should solve your problem today. We're enhancing Business Logic to do this automatically so in the future you can write the code as you originally did.


Hi Yegor,


I tried the same code for findAsync and I got 504 Gateway timeout error.


When I tried the sample code from documentation - http://devcenter.kinvey.com/rest/reference/business-logic/reference.html#collection-access-module, the following code worked correctly for me:

  

var findData = modules.collectionAccess.collection('Collection').findAsync({});
findData.then(function (docs) {
    response.body = docs;
    response.complete(200);
}, 
function(err){
    logger.error('Query failed: '+ err);
});

 

Can you try out this code and tell me if this works for you?



Regards,

Wani

Kinvey Support

Hi Wani,
Thanks for your reply.

This code works but it's not my case, I don't have an error in query.
I want to throw my custom error(call response.error) inside success result.

It seems like it doesn't work only for endpoints, with hooks everything seems fine here.

Hi,


If you modify the code I supplied like below, that should work for you.

 

var findData = modules.collectionAccess.collection('Collection').findAsync({});
findData.then(function (entities) {
//    response.body = entities;
//    response.complete(200);
    if(entities.length > 0) {
        return response.error('Count is more than 0');
    }
    return response.complete(200);
},
function(err){
    logger.error('Query failed: '+ err);
});

 

Let me know if that helps.


Regards,

Wani

Still get timeouts with your variant of code...

Can you share the app key (kid_xxx) and endpoint name?



Regards,

Wani

Answer

The issue here is that the response.error call needs to be wrapped with setImmediate. For example:

}).catch(function(err) {
    logger.info("err: " + err.stack);
    setImmediate(function() {
	    return response.error(500);
    });
  });

 This should solve your problem today. We're enhancing Business Logic to do this automatically so in the future you can write the code as you originally did.

Thanks Austin,
with catch and setImmediate it works

Login or Signup to post a comment