1

I have a few function in my application in javascript

function( param1, param2 )
{

}

Sometimes I have caught exception and sometimes uncaught exception

My question is that: Is it possible to log out the parameters at the time of exception without explicitly writing error handling code in each function?

function(param1, param2)
{
try { /* Big try block contains all code */  } catch(e) { logger.log( param1 , param2) }
}

If there is anything like hooking in exceptions and print out the original parameter, that would be great !

2
  • arguments is an Array-like object accessible inside functions that contains the values of the arguments passed to that function. Commented Mar 25, 2021 at 7:01
  • Thanks @Bikki. That leaves only the question of how to hook on exceptions and print the args out Commented Mar 25, 2021 at 7:09

1 Answer 1

1

without explicitly writing error handling code in each function?

You can create a higher order function for that.

function printErrors(functionToWrap) {
  return function(...args) {
    try {
      functionToWrap(...args)
    } catch (error) {
      console.error(error);
      console.log('all passed parameters');
      args.forEach((e) => console.log(e))
    }
  }
}

function logHello(firstName, lastName) { console.log(`hello ${firstName} ${lastName}`) }

function logHelloError(firstName, lastName) { console.xyz(`hello ${firstName} ${lastName}`) }

printErrors(logHello)('John', 'Doe');
printErrors(logHelloError)('Mick','Jones');

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.