From 8ec3e5687c1df9a79be1661b292b074a29cc6a84 Mon Sep 17 00:00:00 2001 From: Aaron Huggins <50631906+ahuggins-nhs@users.noreply.github.com> Date: Thu, 5 Mar 2020 11:10:31 -0600 Subject: [PATCH 1/2] Added handler for Azure Function runtime Azure functions operate similarly to AWS Lambda in that uncaughtException events are not emitted. Support for these patterns are added as convenience methods to pass-through the expected results from both sync and async patterns. https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-error-pages#use-structured-error-handling https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-node --- src/server/rollbar.js | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/server/rollbar.js b/src/server/rollbar.js index 05304ff73..a1f136f45 100644 --- a/src/server/rollbar.js +++ b/src/server/rollbar.js @@ -281,6 +281,41 @@ 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 async function(context, ...args) { + try { + return await handler(context, ...args); + } catch (error) { + self.error(error); + self.wait(function() { + throw 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); From 157cbe319266993aa98526e9c22ad77ae8c3403c Mon Sep 17 00:00:00 2001 From: Aaron Huggins <50631906+ahuggins-nhs@users.noreply.github.com> Date: Thu, 5 Mar 2020 16:00:49 -0600 Subject: [PATCH 2/2] Updated pattern for Async Testing revealed that errors were not surfacing correctly. Promise pattern similar to lambda handler resolves issue. --- src/server/rollbar.js | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/server/rollbar.js b/src/server/rollbar.js index a1f136f45..acb90fa78 100644 --- a/src/server/rollbar.js +++ b/src/server/rollbar.js @@ -291,15 +291,19 @@ Rollbar.prototype.azureFunctionHandler = function(handler) { Rollbar.prototype.asyncAzureFunctionHandler = function(handler) { const self = this; - return async function(context, ...args) { - try { - return await handler(context, ...args); - } catch (error) { - self.error(error); - self.wait(function() { - throw error; - }) - } + 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) {