How can we determine the name of variable function that was called?
I have a function that acts as a test utility function to setup my test cases. Depending on the parameters passed by the test to the test utility function, certain sub-functions may or may not be executed. Sometimes these sub-functions fail and I need to know which one failed, but am not sure how to do this. In other languages, reflexion is what I would use here. Is that an option?
Here's an abstracted example of what we're doing:
exports.test_util_func = function(params, callback){
//initialize the functions
var a = function(callback){
...
//response might be "{status:400, body: {error: {...} } }"
callback(error, response);
}
var b = function(callback){
...
callback(error, response);
}
//determine what functions to run
var functions_to_run = [];
if(params.a) functions_to_run.push(a);
if(params.b) functions_to_run.push(a);
async.series(functions, function(error, responses){
if(error) throw new Error(error);
for(var i in responses){(function(response){
//verify that we received a 200 success status from the server
//if we didn't, capture the error
if(response.status!==200){
console.log(response.body);
console.log(i);
//HELP NEEDED HERE - how do we capture better than the function iteration so we know what actually failed? Ideally, we would have: "reflective call()
throw new Error(i+": "+JSON.stringify(response.body));
}
})(responses[i]);}
})
}
Edit: there might be something in this following post that we can use, but I imagine there must be some easier way by using the __prototype info: Get variable name. javascript "reflection"
Aucun commentaire:
Enregistrer un commentaire