New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details
Socket
Book a DemoSign in
Socket

@scriptaddicts/gas-error-handler

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@scriptaddicts/gas-error-handler

Methods to handle error in GAS

Source
npmnpm
Version
1.1.2
Version published
Weekly downloads
2
-71.43%
Maintainers
1
Weekly downloads
 
Created
Source

This is a library for Google Apps Script projects. It provides methods to perform an Exponential backoff logic whenever it is needed and rewrite error objects before sending them to Stackdriver Logging.

Methods

expBackoff(func)

In some cases, Google APIs can return errors on which it make sense to retry, like 'We're sorry, a server error occurred. Please wait a bit and try again.'.

In such cases, it make sense to wrap the call to the API in an Exponential backoff logic to retry multiple times.

Note that most Google Apps Script services, like GmailApp and SpreadsheetApp already have an Exponential backoff logic implemented by Google. Thus it does not make sense to wrap every call to those services in an Exponential backoff logic.

Things are different for Google Apps Script advanced services where such logic is not implemented. Thus this method is mostly useful for calls linked to Google Apps Script advanced services.

Example


// Calls an anonymous function that gets the subject of the vacation responder in Gmail for the currently authenticated user.

 var responseSubject = ErrorHandler.expBackoff(function(){return Gmail.Users.Settings.getVacation("me").responseSubject;});

Parameters

Name Type Description
Func Function The anonymous or named function to call.

Return

The value returned by the called function or nothing if the called function isn't returning anything.

urlFetchWithExpBackOff(url, params)

This method works exactly like the fetch() method of Apps Script UrlFetchApp service.

It simply wraps the fetch() call in an Exponential backoff logic.

We advise to replace all existing calls to UrlFetchApp.fetch() by this new method.

UrlFetchApp.fetch(url, params) => ErrorHandler.urlFetchWithExpBackOff(url, params)

logError(e, additionalParams)

When exception logging is enabled, unhandled exceptions are automatically sent to Stackdriver Logging with a stack trace (see official documentation).

But if you wrap your code in a try...catch statement and use console.error(error) to send the exception to Stackdriver Logging, only the error message will be logged and not the stack trace.

We advise to replace all existing calls to console.error(error) by this new method to correctly log the stack trace, in the exact same format used for unhandled exceptions.

console.error(error) => ErrorHandler.logError(error)

Parameters

Name Type Description
e String || Error || {lineNumber: number, fileName: string, responseCode: string} A standard error object retrieved in a try...catch statement or created with new Error() or a simple error message or an object

Setup

You can copy the code of this library in your own Google Apps Script project or reuse it as a standard library. In both cases, methods are called using the ErrorHandler class / namespace, meaning you will use them exactly in the same way.

To install it as a library, use the following script ID and select the latest version:

1mpYgNprGHnJW0BSPclqNIIErVaTyXQ7AjdmLaaE5O9s9bAOrsY14PMCy

To copy the code in your project, simply copy-past the content of this file in a new script file in your project:

https://github.com/RomainVialard/ErrorHandler/blob/master/src/ErrorHandler.gs.js

NPM install:

To be documented

Warning

This library contains 3 methods directly available as functions and callable without using the ErrorHandler class / namespace:

  • expBackoff()
  • urlFetchWithExpBackOff()
  • logError()

For this reason, if you copy the code in your project, make sure you don't have any other function with the exact same name.

FAQs

Package last updated on 05 Apr 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts