1

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"

Community
  • 1
  • 1
Michael Merchant
  • 1,509
  • 2
  • 17
  • 28

1 Answers1

0

I've found a workaround that will allow me to continue using this pattern, but is not fully robust. After creating the function, we create use store the name in the prototype of the function.

The ideal scenario is still to be able to reference the variable's name itself.

Final code sample:

exports.test_util_func = function(params, callback){
 //initialize the functions
 var a = function(callback){
  ...
  //response might be "{status:400, body: {error: {...} } }"
  callback(error, response);
 }
 a.prototype.name = "a";
 var b = function(callback){
  ...
  callback(error, response);
 }
 a.prototype.name = "b";
 //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(functions.map(function(func){return func.prototype.name})[i]);
      console.log(response.body);
    }
  })(responses[i]);}
 })
}
Michael Merchant
  • 1,509
  • 2
  • 17
  • 28