I'm trying to implement some basic higher order function which would return a new function with provided function surrounded with try catch block. Something like this:
function wrapLogging(f) {
return async (...args) => {
try {
await f(...args);
} catch (e) {
log(e);
};
}
}
// dummy example:
const executeAndLog = wrapLogging(executeFunction);
executeAndLog();
// nestjs controller and service example:
@Post('/create')
async executeAndLog() {
const executeAndLog = wrapLogging(this.exampleService.create);
await executeAndLog();
}
Now the problem is that I get undefined errors in provided this.exampleService.create. "TypeError: Cannot read properties of undefined..." I do get that the context is missing but don't know how to 'connect' it. Tried googling examples and found similar problem with .call or .apply solutions but it didn't work in this case. Maybe it's because the service method provided is async, or it's something to do with nestjs services and their context?
any help is greatly appreciated! ty