diff --git a/src/server/rollbar.js b/src/server/rollbar.js index 05304ff73..acb90fa78 100644 --- a/src/server/rollbar.js +++ b/src/server/rollbar.js @@ -281,6 +281,45 @@ Rollbar.errorHandler = function() { } }; +Rollbar.prototype.azureFunctionHandler = function(handler) { + const AsyncFunction = (async () => {}).constructor; + if (handler instanceof AsyncFunction) { + return this.asyncAzureFunctionHandler(handler); + } + return this.syncAzureFunctionHandler(handler); +}; + +Rollbar.prototype.asyncAzureFunctionHandler = function(handler) { + const self = this; + return function(context, ...args) { + return new Promise(function(resolve, reject) { + handler(context, ...args) + .then(function(result) { + resolve(result); + }) + .catch(function(error) { + self.error(error); + self.wait(function() { + reject(error); + }); + }); + }) + } +} +Rollbar.prototype.syncAzureFunctionHandler = function(handler) { + const self = this; + return async function(context, ...args) { + try { + return handler(context, ...args); + } catch (error) { + self.error(error); + self.wait(function() { + throw error; + }) + } + } +} + Rollbar.prototype.lambdaHandler = function(handler, timeoutHandler) { if (handler.length <= 2) { return this.asyncLambdaHandler(handler, timeoutHandler);