-
Notifications
You must be signed in to change notification settings - Fork 1
/
Error.gs
154 lines (120 loc) · 4.36 KB
/
Error.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/**
* Central error handling displays alert and sets the currenct cell when appropriate.
* @param {string} error - The type of error.
* @param {string} message - The message to display to the user.
* @param {number} [rowIndex] - The row index of the cell in the ledger sheet.
* @param {string} [columnName] - the name assigned to the column in the ledger sheet.
* Used to get the index from LedgerRecord.getColumnIndex(columnName).
* Avoids hard coding column numbers.
*/
CryptoTracker.prototype.handleError = function (error, message, rowIndex, columnName) {
if (error === 'validation') {
if (rowIndex && columnName) {
this.setCurrentCell(rowIndex, columnName);
}
SpreadsheetApp.getUi().alert(`Ledger validation failed`, message, SpreadsheetApp.getUi().ButtonSet.OK);
}
else if (error === 'cryptoAccount') {
if (rowIndex && columnName) {
this.setCurrentCell(rowIndex, columnName);
}
SpreadsheetApp.getUi().alert(`Insufficient funds`, message, SpreadsheetApp.getUi().ButtonSet.OK);
}
else if (error === 'api') {
SpreadsheetApp.getUi().alert(`Error updating crypto prices`, message, SpreadsheetApp.getUi().ButtonSet.OK);
}
else if (error === 'settings') {
SpreadsheetApp.getUi().alert(`Failed to save settings`, message, SpreadsheetApp.getUi().ButtonSet.OK);
}
};
/**
* Sets the currenct cell in the ledger sheet.
* @param {number} rowIndex - The row index of the cell in the ledger sheet.
* @param {string} columnName - the name assigned to the column in the ledger sheet.
* Used to get the index from LedgerRecord.getColumnIndex(columnName).
* Avoids hard coding column numbers.
*/
CryptoTracker.prototype.setCurrentCell = function (rowIndex, columnName) {
let ss = SpreadsheetApp.getActive();
let ledgerSheet = ss.getSheetByName(this.ledgerSheetName);
if (ledgerSheet) {
let columnIndex = LedgerRecord.getColumnIndex(columnName);
let range = ledgerSheet.getRange(rowIndex, columnIndex, 1, 1);
ss.setCurrentCell(range);
SpreadsheetApp.flush();
}
};
/**
* General custom error from which to inherit.
* Assigns the name of the class to the name property and passes the message to super.
* @extends Error
*/
class CustomError extends Error {
/**
* Initializes class with message, sets name property to the name of the class.
* @param {string} message - description of the error and suggested solution.
*/
constructor(message) {
super(message);
this.name = this.constructor.name;
}
}
/**
* Error in the validation of the ledger.
* @extends CustomError
*/
class ValidationError extends CustomError {
/**
* Initializes class with message, rowIndex and columnName, sets name property to the name of the class.
* @param {string} message - description of the error and suggested solution.
* @param {number} [rowIndex] - the row numer in the ledger sheet that requires atention.
* @param {string} [columnName] - the name assigned to the column in the ledger sheet.
* Used to get the index from LedgerRecord.getColumnIndex(columnName).
* Avoids hard coding column numbers.
*/
constructor(message, rowIndex, columnName) {
super(message);
/**
* The row numer in the ledger sheet that requires atention.
* @type {number}
*/
this.rowIndex = rowIndex;
/**
* The name assigned to the column in the ledger sheet.
* @type {string}
*/
this.columnName = columnName;
}
}
/**
* Error when attempting to withdraw from a cryptocurrency account.
* @extends CustomError
*/
class CryptoAccountError extends CustomError {
/**
* Initializes class with message and rowIndex, sets name property to the name of the class.
* @param {string} message - description of the error and suggested solution.
* @param {number} rowIndex - the row numer in the ledger sheet that requires atention.
*/
constructor(message, rowIndex) {
super(message);
/**
* The row numer in the ledger sheet that requires atention.
* @type {number}
*/
this.rowIndex = rowIndex;
}
}
/**
* Error when attempting to retrieve crypto prices from an API.
* @extends CustomError
*/
class ApiError extends CustomError {
/**
* Initializes class with message, sets name property to the name of the class.
* @param {string} message - description of the error and suggested solution.
*/
constructor(message) {
super(message);
}
}