Not returning as error?

9 views
Skip to first unread message

Kevin Burton

unread,
Dec 30, 2015, 2:32:00 PM12/30/15
to Q Continuum (JavaScript)
I have some code like:

var workqueue = [ autoFillRule.createNodes,
 autoFillRuleGroup
.createNodes,
 fieldDefinition
.createNodes,
 fieldValidation
.createNodes,
 formDefinition
.createNodes,
 formDefinitionEmail
.createNodes,
 formDefinitionImage
.createNodes,
 formDefinitionImageIndex
.createNodes,
 pageDefinition
.createNodes,
 pageValidation
.createNodes,
 validationRule
.createNodes,
 workItem
.createNodes
];
return Promise.all(workqueue.map(function(work) {
     
return work().then(function (result) {
         
return result;
     
}).catch(function (err) {
         
return err;
     
});
})).catch(function (err) {
     
return err;
});

Each of these functions (in the workqueue look like:

var request = new sql.Request();
var query = 'select r.autoFillRuleID, r.autoFillRuleGroupID, r.sourceFieldKey, r.destinationFieldKey, r.ordinal' +
 
' from [EForms].[OnlineForm].[AutoFillRule] r';
return request.query(query).then(function(rows) {
     
return Promise.all(rows.map(function(row) {
         
var props = {};
         
for (var property in row) {
             
if (row.hasOwnProperty(property)) {
                 props
[property] = row[property];
             
}
         
}
         props
['id'] = row.autoFillRuleID;
         
var params = {
             props
: props
         
};
         
return new Promise(function(resolve, reject) {
             db
.query("CREATE (n:AutoFileRule {props}) RETURN n", params, function(err, result) {
                 
if(err) {
                     reject
(err);
                 
} else {
                     resolve
(result);
                 
}
             
});
         
});
     
})).catch(function(err) {
         
return err;
     
});
}).catch(function(err) {
     
return err;
});

The problem is that errors are not being returned as errors. For example if I change one of the column names (this is a SQL query) so that the query fails then I would expect the SQL error to be caught in the "reject" portion of the promise. It is not (I would expect the error to show up in the 'catch'). Also (I am using Node-Neo4j) the statement beginning with 'db.query(' returns an error because the database is not started up yet. But the error is returned as part of the "result" and not the error. Ideas on how I should structure this so the error shows up in the right place?

Thank you.



Kris Kowal

unread,
Jan 2, 2016, 7:35:14 PM1/2/16
to q-con...@googlegroups.com
You need to throw, not return, errors. Also, if a handler just rethrows an error, it can be omitted entirely. The entire chain should be terminated with .done() to surface any error that propagates through the entire chain. Catch clauses should only be provided if you intend to handle the error.

var request = new sql.Request();
var query = 'select r.autoFillRuleID, r.autoFillRuleGroupID, r.sourceFieldKey, r.destinationFieldKey, r.ordinal' +
 
' from [EForms].[OnlineForm].[AutoFillRule] r';
return request.query(query).then(function(rows) {
     
return Promise.all(rows.map(function(row) {
         
var props = {};
         
for (var property in row) {
             
if (row.hasOwnProperty(property)) {
                 props
[property] = row[property];
             
}
         
}
         props
['id'] = row.autoFillRuleID;
         
var params = {
             props
: props
         
};
         return new Promise(function(resolve, reject) {
             db
.query("CREATE (n:AutoFileRule {props}) RETURN n", params, function(err, result) {
                 
if(err) {
                     reject
(err);
                 
} else {
                     resolve
(result);
                 
}
             
});
         
});

     
}));
})
.done();

--
You received this message because you are subscribed to the Google Groups "Q Continuum (JavaScript)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to q-continuum...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Kevin Burton

unread,
Jan 3, 2016, 9:10:13 PM1/3/16
to q-con...@googlegroups.com
Thank you for the clarification. 
I apparently got hung up on two points that hopefully won't bite other developers. One, calling the rejection function passed in the Promise constructor. Since this was a function call I didn't consider that "rejection" would take the form of an exception. Two, as part of most "then" methods there is a success function as the first argument then there is a failure method. Again this method seemed like a "normal" method and I didn't consider failure or error as an exception. 
Again thank you.
You received this message because you are subscribed to a topic in the Google Groups "Q Continuum (JavaScript)" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/q-continuum/AF3fuATbvLs/unsubscribe.
To unsubscribe from this group and all its topics, send an email to q-continuum...@googlegroups.com.

Kevin Burton

unread,
Jan 4, 2016, 8:21:21 AM1/4/16
to Q Continuum (JavaScript), kris....@cixar.com

If I am following the parenthesis right it looks like you are chaining like then().done();.If I try this I get:

request.query(...).then(...).done is not a function


...

Kris Kowal

unread,
Jan 4, 2016, 1:07:49 PM1/4/16
to Q Continuum (JavaScript)
I see. This means that request.query() is not returning a Q promise, but some other variety. It can be coerced with the Q() function, which is an alias for Q’s Promise.resolve(). If you’re not using Q, there is no .done() method.

Q(request.query(…)).then(…).done()

--

Kevin Burton

unread,
Jan 4, 2016, 2:25:02 PM1/4/16
to q-con...@googlegroups.com
Thank you for the tip. I am using Q but as you deduced the libraries I have no control over apparently do not.
You received this message because you are subscribed to a topic in the Google Groups "Q Continuum (JavaScript)" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/q-continuum/AF3fuATbvLs/unsubscribe.
To unsubscribe from this group and all its topics, send an email to q-continuum...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages