I'm getting an error (though not a breaking one) a...
# suitescript
e
I'm getting an error (though not a breaking one) and I'm not sure why... I have an array of functions that I want to call, but I want each to be wrapped in a try/catch so all of them run even if one has an error This bit of code works fine, but always throws "TypeError: fn is not a function" in the catch block. I've added that if statement to prevent the error from showing in the logs, but I'm trying to figure out why it appears in the first place.
Copy code
[
      ocUserEvents.functionOne(context),
      ocUserEvents.functionTwo(context)
    ].forEach(fn => {
      try {
        fn();
      } catch (error) {
        if (error == 'TypeError: fn is not a function') return;
        log.error({
          title: "error on afterSubmit",
          details: error,
        });
      }
    });
b
depends on how functional your code is
Copy code
ocUserEvents.functionOne(context)
is not normally what I would expect a function that returns a function to look like
e
More background: ocUserEvents is a library that returns a single object. Something like
Copy code
return {
  functionOne: function(context){
     //do stuff here
  },
  functionTwo: function(context){
     //do other  stuff here
  },
}
And that first bit of code does indeed call both functions, and they both run without issue
b
Copy code
functionOne: function(context){
    throw new Error('This should be more obvious')
  }
do that instead, your code does not do what you think it does
e
So I should handle errors within each function of the object rather than when I call those functions?
b
your idea is fine, your execution is useless
e
Let me ask this way- If you wanted to call 5 functions, but wanted to make sure all 5 ran even if the first one incurred an error, how would you handle that?
b
have you tried making one of your functions always throw an error
e
No. Let me do that now.
...it stops after the first function error. Second function doesn't run
So, I wrap the contents of ocUserEvetns.functionOne in a try catch (same with functionTwo) and then just call them one at a time?
s
I think battk is trying to make this a learning moment. in your original code, you're not passing functions, you're invoking them.
e
I'm allllll for learning moments. But want to make sure I'm grasping the concept
b
() is what you put after a function to call it
you are calling your functions outside of your try catch block, making it useless
s
if you're using lodash, you can try
_.partial(ocUserEvents.functionOne, context)
instead of
ocUserEvents.functionOne(context)
e
lightbulb
s
though in general, unless this
functions as values
complexity is really adding value I'd avoid it.
e
I think I get it now. So I should wait to call the function until I'm in the forEach like this: (?)
Copy code
[
      ocUserEvents.functionOne,
      ocUserEvents.functionTwo
    ].forEach(fn => {
      try {
        fn(context);
      } catch (error) {
        log.error({
          title: "error on afterSubmit",
          details: error,
        });
      }
    });
b
that should be better
although i disagree with hiding errors in logs
e
Yes, that worked as expected. I added a throw new Error to both functionOne and functionTwo and they both showed in the log
although i disagree with hiding errors in logs ^Agreed, was just logging them to figure out why I was getting errors. That catch block is not the end game*
b
if you were writing defensive code, you would pass a copy of the context to fn in case someone changes it inside the fn
e
This may be a lesson for another day. Can I pick your brain about this later?
b
sure
e
Thank you both for your help @battk @stalbert 🍻