All Projects → RomainVialard → ErrorHandler

RomainVialard / ErrorHandler

Licence: other
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.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to ErrorHandler

miette
Fancy upgrade to std::error::Error.
Stars: ✭ 945 (+6650%)
Mutual labels:  error-handling
belay
Robust error-handling for Kotlin and Android
Stars: ✭ 35 (+150%)
Mutual labels:  error-handling
moko-errors
Automated exceptions handler for mobile (android & ios) Kotlin Multiplatform development.
Stars: ✭ 45 (+221.43%)
Mutual labels:  error-handling
fejl
Error-making utility for Node apps.
Stars: ✭ 30 (+114.29%)
Mutual labels:  error-handling
gmail-gitlab-filtering
Google Apps Script for Gmail to filter and sort email from GitLab
Stars: ✭ 84 (+500%)
Mutual labels:  apps-script
rescue
🚒✨ Rescue: better errors through types (a more type directed MonadThrow/MonadCatch)
Stars: ✭ 18 (+28.57%)
Mutual labels:  error-handling
progress-bar-log
A component to display a progress bar and last X logs at the same time.
Stars: ✭ 44 (+214.29%)
Mutual labels:  error-handling
ErrorLayout
Simple layout to show custom error toast with animation
Stars: ✭ 13 (-7.14%)
Mutual labels:  error-handling
dx
JavaScript without `try...catch`.
Stars: ✭ 26 (+85.71%)
Mutual labels:  error-handling
whoops
It makes simple create qualified errors.
Stars: ✭ 28 (+100%)
Mutual labels:  error-handling
merr
🔥 Minimal and good enough error handling library for Clojure/ClojureScript
Stars: ✭ 25 (+78.57%)
Mutual labels:  error-handling
sentry-testkit
A Sentry plugin to allow Sentry report interception and further inspection of the data being sent
Stars: ✭ 78 (+457.14%)
Mutual labels:  error-handling
apollo-error-converter
Global Apollo Server Error handling made easy. Remove verbose and repetitive resolver / data source Error handling. Automatic Error catching, logging, and conversion to ApolloErrors.
Stars: ✭ 16 (+14.29%)
Mutual labels:  error-handling
google-apps-script
A collection of Google Apps Script that I've worked on over time.
Stars: ✭ 79 (+464.29%)
Mutual labels:  apps-script
gas-url-shortener
A URL Shortening service powered by Google Apps Script.
Stars: ✭ 43 (+207.14%)
Mutual labels:  apps-script
telegram client
library for help you make userbot or bot telegram and support tdlib telegram database and only support nodejs dart and google-apps-script
Stars: ✭ 38 (+171.43%)
Mutual labels:  apps-script
gommon
A collection of common util libraries for Go
Stars: ✭ 26 (+85.71%)
Mutual labels:  error-handling
jsonerror
Makes Go error-handling a breeze!
Stars: ✭ 28 (+100%)
Mutual labels:  error-handling
raygun4ruby
The Ruby & Ruby on Rails provider for Raygun
Stars: ✭ 37 (+164.29%)
Mutual labels:  error-handling
apps-script-db
A key-value database by Google Apps Script
Stars: ✭ 20 (+42.86%)
Mutual labels:  apps-script

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, options)

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 Default value Description
func Function The anonymous or named function to call.
options Object {} OPTIONAL, options for exponential backoff
options.throwOnFailure boolean false If true, throw a CustomError on failure
options.doNotLogKnownErrors boolean false If true, will not log known errors to stackdriver
options.verbose boolean false If true, will log a warning on a successful call that failed at least once
options.retryNumber number 5 Maximum number of retry on error

Return

The value returned by the called function, or a CustomError on failure if options.throwOnFailure == false

CustomError is an instance of Error

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(error, 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. This method will also leverage the list of known errors and their translation to better aggregate on Stackdriver Logging: the message will be the English version if available.

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 Default value Description
error String, Error, or { 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
additionalParams Object, {addonName: string} {} Add custom values to the logged Error, the 'addonName' property will pass the AddonName to remove from error fileName
options Object {} Options for logError
options.asWarning boolean false If true, use console.warn instead console.error
options.doNotLogKnownErrors booleab false if true, will not log known errors to stackdriver

Return

Return the CustomError, which is an Error, with a property 'context', as defined below:

/**
 * @typedef {Error} CustomError
 *
 * @property {{
 *   locale: string,
 *   originalMessage: string,
 *   knownError: boolean,
 *   variables: Array<{}>,
 *   errorName: string,
 *   reportLocation: {
 *     lineNumber: number,
 *     filePath: string,
 *     directLink: string,
 *   },
 * }} context
 */

getNormalizedError(localizedErrorMessage, partialMatches)

Try to get the corresponding english version of the error if listed in this library.

There are Error match exactly, and Errors that can be partially matched, for example when it contains a variable part (eg: a document ID, and email)

To get variable part, use the following pattern:

var variables = []; // The empty array will be filled if necessary in the function
var normalizedError = ErrorHandler.getNormalizedError('Documento 1234567890azerty mancante (forse è stato eliminato?)', variables);

// The normalized Error, with its message in English, or '' of no match are found:
// normalizedError = 'Document is missing (perhaps it was deleted?)'

// the variable part:
// variables = [{variable: 'docId', value: '1234567890azerty'}]

Parameters

Name Type Default value Description
localizedErrorMessage String The Error message in the user's locale
partialMatches Array<{ variable: string, value: string }> [] OPTIONAL, Pass an empty array, getNormalizedError() will populate it with found extracted variables in case of a partial match

The value returned is the error in English or '' if no matching error was found

getErrorLocale(localizedErrorMessage)

Try to find the locale of the localized thrown error

Parameters

Name Type Default value Description
localizedErrorMessage String The Error message in the user's locale

Return

The locale ('en', 'it', ...) or '' if no matching error found

NORMALIZED_ERRORS

constant

List all known Errors in a fixed explicit English message. Serves as reference for locallizing Errors.

Use this to check which error a normalized error is:

var normalizedError = ErrorHandler.getNormalizedError('Documento 1234567890azerty mancante (forse è stato eliminato?)');

if (normalizedError === ErrorHandler.NORMALIZED_ERRORS.DOCUMENT_MISSING) {
  // Do something on document missing error
}

NORETRY_ERRORS

constant

List all Errors for which there are no benefit in re-trying

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 5 methods directly available as functions and callable without using the ErrorHandler class / namespace:

  • expBackoff()
  • urlFetchWithExpBackOff()
  • logError()
  • getNormalizedError()
  • getErrorLocale()

and 2 Object constant

  • NORMALIZED_ERRORS
  • NORETRY_ERRORS

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].