Tuesday 22 November 2022

Filter a try/catch error message

The following Google Apps Script is designed to exercise how you might go about searching the error message in a 'try/catch' for keywords. I wanted this specifically for a tool I built that contains a lot of code (and hence a lot of potential error messages) between a try/catch, for which a very small number of people were experiencing a timezone issue with their Google Sheet file.

The aim was to use a JavaScript 'match' to find the keyword timezone and display a set of instructions for the user to resolve the issue themselves instead of just a generic error message. The below Apps Script code is a simplified version of all of that aimed at just getting a file and using a typo in the code to trigger an error.

Filter an error message in a try/catch
Filter an error message in a try/catch

The Code

There is specifically a typo in order to trigger the 'catch' to kick in:

var file = DriveApp.getFilById('ENTER FILE ID HERE');

It is necessary to convert the information captured within in the 'catch' to a string before then implementing the 'match' and regular expression (using a global, case-insensitive search):

var errorString = error.toString();
var matching = errorString.match(/is not a function/gi);

 

 

Download

Filter a try/catch error message download (please use 'Overview' > 'Make a copy' for your own version).


No comments:

Post a Comment