Google Apps Scripts + Honeybadger = ❤️

Recently I’ve been working on Google Data Studio Connectors (you can read more about it here) which are advanced Google Apps Scripts. During the first few weeks after shipping the code to production, I had been constantly checking the executions list in order to quickly find and react to any incoming bugs. This process quickly became annoying, so I started browsing the web trying to find a way of automating it.

Unfortunately, I didn’t find any useful tool that’d support the Google Apps Scripts environment, so I decided to write one by myself.
In Leadfeeder for our Ruby apps, we use a great SaaS tool called Honeybadger which frees us from checking the logs every day and lets us just receive a Slack notification with error info right at the moment when something bad happens.
I’m a big fan of how simple and reliable Honeybadger is, so I quickly checked their API docs and decided that I’ll start building a client library to support Google Apps Scripts environment.

After a few days of TypeScript coding, I managed to create a version that satisfies my needs.
In the following paragraphs you’ll learn how to set up and use the library. Let me share my knowledge and work with you now :)

Installation

Add the library to your script

Click on the menu item “Resources > Libraries…” In the “Find a Library” text box, enter the script ID M4hoogS1IHPlDGE3VXDWwuTI3yRUM8Pyy and click the "Select" button. Choose a version in the drop-down box (usually best to pick the latest version). Click the "Save" button.

Adjust your manifest file

  1. If you’re whitelisting URLs accessible by UrlFetchApp make sure to add "https://api.honeybadger.io/" to urlFetchWhitelist key in appsscript.jsonfile.

Setup the client

Next, in your GAS function initialize the client by executing the following line:

Honeybadger.configure({
apiKey: '[ YOUR API KEY HERE ]'
});

Send error notifications to Honeybadger

function yourGasFunction() {
try {
// Your code
} catch(e) {
Honeybadger.notify(e);
}
}

Add execution context

Honeybadger.context({
user_id: Session.getTemporaryActiveUserKey(),
user_email: Session.getActiveUser().getEmail()
});

Personally, I use the following helper function that does the initialization, adds execution context and sets up error handling at once:

function withHoneybadger(callback) {
const scriptProperties = PropertiesService.getScriptProperties();
const apiKey = scriptProperties.getProperty('HONEYBADGER_API_KEY');
Honeybadger.init({ apiKey: apiKey });
Honeybadger.context({
user_id: Session.getTemporaryActiveUserKey(),
user_locale: Session.getActiveUserLocale(),
user_email: Session.getActiveUser().getEmail()
});
try {
return callback();
} catch (e) {
Honeybadger.notify(e);
throw e;
}
}
function myGasFunction() {
return withHoneybadger(function() {
return gasFunctionLogic();
});
}

It works 🎉

Here’s how an example error notification for my script looks like. You can find error’s stack trace, affected users, number of occurrences and the execution’s context at a quick glance.

Example error page created via honeybadger-gas library

Help & ideas welcome

Full stack developer @Leadfeeder. Working on random stuff in my free time.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store