-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
GasFramework.gs
105 lines (79 loc) · 2.78 KB
/
GasFramework.gs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// JSHint - TODO
/* jshint asi: true */
/* jshint esversion: 6 */
(function(){"use strict"})()
// !!! FILE_NAME !!!!.gs
// =====================
//
// Dev: AndrewRoberts.net
//
// External interface to this script - all of the event handlers.
//
// This files contains all of the event handlers.
let Log_ = null
// Public event handlers
// ---------------------
//
// All external event handlers need to be top-level function calls; they can't
// be part of an object, and to ensure they are all processed similarily
// for things like logging and error handling, they all go through
// errorHandler_(). These can be called from custom menus, web apps,
// triggers, etc
//
// The main functionality of a call is in a function with the same name but
// post-fixed with an underscore (to indicate it is private to the script)
//
// For debug, rather than production builds, lower level functions are exposed
// in the menu
const EVENT_HANDLERS_ = {
// Name onError Message Main Functionality
// ---- --------------- ------------------
onInstall: ['onInstall()', 'Failed to install', onInstall_],
}
function onInstall(args) {return eventHandler_(EVENT_HANDLERS_.onInstall, args)}
// Private Functions
// =================
// General
// -------
/**
* All external function calls should call this to ensure standard
* processing - logging, errors, etc - is always done.
*
* @param {Array} config:
* [0] {Function} prefunction
* [1] {String} eventName
* [2] {String} onErrorMessage
* [3] {Function} mainFunction
*
* @param {Object} args The argument passed to the top-level event handler
*/
function eventHandler_(config, args) {
const userEmail = Session.getActiveUser().getEmail()
try {
Log_ = BBLog.getLog({
level: DEBUG_LOG_LEVEL_,
displayFunctionNames: DEBUG_LOG_DISPLAY_FUNCTION_NAMES_,
})
Log_.info('Handling ' + config[0] + ' from ' + (userEmail || 'unknown email') + ' (' + SCRIPT_NAME + ' ' + SCRIPT_VERSION + ')')
// Call the main function
return config[2](args)
} catch (error) {
console.log(error.message)
const assertConfig = {
error: error,
userMessage: config[1],
log: Log_,
handleError: HANDLE_ERROR_,
sendErrorEmail: SEND_ERROR_EMAIL_,
emailAddress: userEmail || ADMIN_EMAIL_ADDRESS_,
scriptName: SCRIPT_NAME,
scriptVersion: SCRIPT_VERSION,
}
Assert.handleError(assertConfig)
}
} // eventHandler_()
// Private event handlers
// ----------------------
function onInstall_() {
// TODO ...
}