anote-server-libs 0.12.0 → 0.12.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/services/WithTransaction.js +20 -7
- package/services/WithTransaction.ts +19 -8
- package/services/utils.js +1 -0
- package/services/utils.ts +1 -0
package/package.json
CHANGED
|
@@ -45,7 +45,11 @@ function releaseAndDecrement(repo, dbClient) {
|
|
|
45
45
|
try {
|
|
46
46
|
dbClient.release();
|
|
47
47
|
}
|
|
48
|
-
catch (
|
|
48
|
+
catch (err) {
|
|
49
|
+
const log = utils_1.utils.logger;
|
|
50
|
+
if (log)
|
|
51
|
+
log.error('Error releasing dbClient: %j', { err, stack: new Error().stack });
|
|
52
|
+
}
|
|
49
53
|
}
|
|
50
54
|
exports.withTransactionConfig.activeCount = Math.max(0, exports.withTransactionConfig.activeCount - 1);
|
|
51
55
|
}
|
|
@@ -73,7 +77,7 @@ function withTransaction(repo, logger, previousMethod, lock, commitIfLost = true
|
|
|
73
77
|
};
|
|
74
78
|
exports.withTransactionConfig.activeCount++;
|
|
75
79
|
const connectTimeoutHandler = setTimeout(() => {
|
|
76
|
-
logger.error('Error timed out getting a client, exiting...');
|
|
80
|
+
logger.error('Error timed out getting a client, exiting... stack: %s', new Error().stack);
|
|
77
81
|
process.exit(22);
|
|
78
82
|
}, exports.withTransactionConfig.timeoutMillis);
|
|
79
83
|
Promise.all([
|
|
@@ -92,7 +96,14 @@ function withTransaction(repo, logger, previousMethod, lock, commitIfLost = true
|
|
|
92
96
|
dbClient.queryPatched = true;
|
|
93
97
|
}
|
|
94
98
|
dbClient.removeAllListeners('error');
|
|
95
|
-
dbClient.on('error', (err) =>
|
|
99
|
+
dbClient.on('error', (err) => {
|
|
100
|
+
if (utils_1.utils.clientErrorHandler) {
|
|
101
|
+
utils_1.utils.clientErrorHandler(err, dbClient);
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
logger.error('SQL [Client %d] ERROR (no handler): %j', dbClient?.processID, { err, stack: new Error().stack });
|
|
105
|
+
}
|
|
106
|
+
});
|
|
96
107
|
res.locals.dbClient = dbClient;
|
|
97
108
|
res.locals.dbClientCommited = false;
|
|
98
109
|
res.locals.dbClientCommit = (cb) => {
|
|
@@ -130,10 +141,10 @@ function withTransaction(repo, logger, previousMethod, lock, commitIfLost = true
|
|
|
130
141
|
if (res.statusCode > 303 && res.statusCode !== 412) {
|
|
131
142
|
if (logger) {
|
|
132
143
|
if (res.statusCode > 499) {
|
|
133
|
-
logger.error('Uncaught 500: %j', obj
|
|
144
|
+
logger.error('Uncaught 500 at %s %s: %j', req.method, req.originalUrl, { body: obj, stack: new Error().stack });
|
|
134
145
|
}
|
|
135
146
|
else {
|
|
136
|
-
logger.warn('Client error 4XX: %j', obj
|
|
147
|
+
logger.warn('Client error 4XX at %s %s: %j', req.method, req.originalUrl, { body: obj, stack: new Error().stack });
|
|
137
148
|
}
|
|
138
149
|
}
|
|
139
150
|
(repo.db ? dbClient.query('ROLLBACK') : dbClient.rollback()).catch((err) => obj && obj.error && (obj.error.additionalInfo2 = { message: err.message })).then(() => {
|
|
@@ -161,7 +172,7 @@ function withTransaction(repo, logger, previousMethod, lock, commitIfLost = true
|
|
|
161
172
|
res.end = () => {
|
|
162
173
|
if (res.statusCode > 303 && res.statusCode !== 412) {
|
|
163
174
|
if (logger && res.statusCode > 499) {
|
|
164
|
-
logger.error('Uncaught 500 with no details
|
|
175
|
+
logger.error('Uncaught 500 with no details at %s %s: %j', req.method, req.originalUrl, { stack: new Error().stack });
|
|
165
176
|
}
|
|
166
177
|
(repo.db ? dbClient.query('ROLLBACK') : dbClient.rollback()).catch(() => undefined).then(() => {
|
|
167
178
|
releaseAndDecrement(repo, dbClient);
|
|
@@ -189,6 +200,7 @@ function withTransaction(repo, logger, previousMethod, lock, commitIfLost = true
|
|
|
189
200
|
};
|
|
190
201
|
if (lock) {
|
|
191
202
|
dbClient.query('SELECT pg_advisory_xact_lock(' + lock + ')').then(() => finish()).catch((err) => {
|
|
203
|
+
logger.error('Error acquiring advisory lock %d at %s %s: %j', lock, req.method, req.originalUrl, { err, stack: new Error().stack });
|
|
192
204
|
res.status(500).json({
|
|
193
205
|
error: {
|
|
194
206
|
errorKey: 'internal.db',
|
|
@@ -203,7 +215,7 @@ function withTransaction(repo, logger, previousMethod, lock, commitIfLost = true
|
|
|
203
215
|
}
|
|
204
216
|
}).catch((err) => {
|
|
205
217
|
releaseAndDecrement(repo, dbClient);
|
|
206
|
-
logger.error('Error beginning transaction: %j', err);
|
|
218
|
+
logger.error('Error beginning transaction at %s %s: %j', req.method, req.originalUrl, { err, stack: new Error().stack });
|
|
207
219
|
res.status(500).json({
|
|
208
220
|
error: {
|
|
209
221
|
errorKey: 'internal.db',
|
|
@@ -213,6 +225,7 @@ function withTransaction(repo, logger, previousMethod, lock, commitIfLost = true
|
|
|
213
225
|
});
|
|
214
226
|
}).catch(err => {
|
|
215
227
|
clearTimeout(connectTimeoutHandler);
|
|
228
|
+
logger.error('Error connecting to database at %s %s: %j', req.method, req.originalUrl, { err, stack: new Error().stack });
|
|
216
229
|
exports.withTransactionConfig.activeCount = Math.max(0, exports.withTransactionConfig.activeCount - 1);
|
|
217
230
|
res.status(500).json({
|
|
218
231
|
error: {
|
|
@@ -75,7 +75,10 @@ export function releaseTransactionGate(): void {
|
|
|
75
75
|
|
|
76
76
|
function releaseAndDecrement(repo: BaseModelRepository, dbClient: any) {
|
|
77
77
|
if(repo.db && dbClient) {
|
|
78
|
-
try { dbClient.release(); } catch (
|
|
78
|
+
try { dbClient.release(); } catch (err) {
|
|
79
|
+
const log = utils.logger;
|
|
80
|
+
if(log) log.error('Error releasing dbClient: %j', {err, stack: new Error().stack});
|
|
81
|
+
}
|
|
79
82
|
}
|
|
80
83
|
withTransactionConfig.activeCount = Math.max(0, withTransactionConfig.activeCount - 1);
|
|
81
84
|
}
|
|
@@ -86,7 +89,7 @@ export function withTransaction(repo: BaseModelRepository, logger: Logger, previ
|
|
|
86
89
|
// If gate is active, queue this request and retry when gate is released
|
|
87
90
|
if(withTransactionConfig.gateActive) {
|
|
88
91
|
return new Promise<void>((resolve, reject) => {
|
|
89
|
-
withTransactionConfig.gateQueue.push({
|
|
92
|
+
withTransactionConfig.gateQueue.push({resolve, reject});
|
|
90
93
|
}).then(() => {
|
|
91
94
|
// Re-enter with gate now released
|
|
92
95
|
return withTransaction(repo, logger, previousMethod, lock, commitIfLost)(req, res, next);
|
|
@@ -109,7 +112,7 @@ export function withTransaction(repo: BaseModelRepository, logger: Logger, previ
|
|
|
109
112
|
|
|
110
113
|
const connectTimeoutHandler = setTimeout(() => {
|
|
111
114
|
// Timed out getting a client, restart worker or process...
|
|
112
|
-
logger.error('Error timed out getting a client, exiting...');
|
|
115
|
+
logger.error('Error timed out getting a client, exiting... stack: %s', new Error().stack);
|
|
113
116
|
process.exit(22);
|
|
114
117
|
}, withTransactionConfig.timeoutMillis);
|
|
115
118
|
Promise.all([
|
|
@@ -129,7 +132,13 @@ export function withTransaction(repo: BaseModelRepository, logger: Logger, previ
|
|
|
129
132
|
dbClient.queryPatched = true;
|
|
130
133
|
}
|
|
131
134
|
dbClient.removeAllListeners('error');
|
|
132
|
-
dbClient.on('error', (err: any) =>
|
|
135
|
+
dbClient.on('error', (err: any) => {
|
|
136
|
+
if(utils.clientErrorHandler) {
|
|
137
|
+
utils.clientErrorHandler(err, dbClient);
|
|
138
|
+
} else {
|
|
139
|
+
logger.error('SQL [Client %d] ERROR (no handler): %j', dbClient?.processID, {err, stack: new Error().stack});
|
|
140
|
+
}
|
|
141
|
+
});
|
|
133
142
|
|
|
134
143
|
res.locals.dbClient = dbClient;
|
|
135
144
|
res.locals.dbClientCommited = false;
|
|
@@ -170,9 +179,9 @@ export function withTransaction(repo: BaseModelRepository, logger: Logger, previ
|
|
|
170
179
|
if(res.statusCode > 303 && res.statusCode !== 412) {
|
|
171
180
|
if(logger) {
|
|
172
181
|
if(res.statusCode > 499) {
|
|
173
|
-
logger.error('Uncaught 500: %j', obj
|
|
182
|
+
logger.error('Uncaught 500 at %s %s: %j', req.method, req.originalUrl, {body: obj, stack: new Error().stack});
|
|
174
183
|
} else {
|
|
175
|
-
logger.warn('Client error 4XX: %j', obj
|
|
184
|
+
logger.warn('Client error 4XX at %s %s: %j', req.method, req.originalUrl, {body: obj, stack: new Error().stack});
|
|
176
185
|
}
|
|
177
186
|
}
|
|
178
187
|
(repo.db ? dbClient.query('ROLLBACK') : dbClient.rollback()).catch((err: any) => obj && obj.error && (obj.error.additionalInfo2 = {message: err.message})).then(() => {
|
|
@@ -197,7 +206,7 @@ export function withTransaction(repo: BaseModelRepository, logger: Logger, previ
|
|
|
197
206
|
res.end = () => {
|
|
198
207
|
if(res.statusCode > 303 && res.statusCode !== 412) {
|
|
199
208
|
if(logger && res.statusCode > 499) {
|
|
200
|
-
logger.error('Uncaught 500 with no details
|
|
209
|
+
logger.error('Uncaught 500 with no details at %s %s: %j', req.method, req.originalUrl, {stack: new Error().stack});
|
|
201
210
|
}
|
|
202
211
|
(repo.db ? dbClient.query('ROLLBACK') : dbClient.rollback()).catch((): any => undefined).then(() => {
|
|
203
212
|
releaseAndDecrement(repo, dbClient);
|
|
@@ -223,6 +232,7 @@ export function withTransaction(repo: BaseModelRepository, logger: Logger, previ
|
|
|
223
232
|
|
|
224
233
|
if(lock) {
|
|
225
234
|
dbClient.query('SELECT pg_advisory_xact_lock(' + lock + ')').then(() => finish()).catch((err: any) => {
|
|
235
|
+
logger.error('Error acquiring advisory lock %d at %s %s: %j', lock, req.method, req.originalUrl, {err, stack: new Error().stack});
|
|
226
236
|
res.status(500).json({
|
|
227
237
|
error: {
|
|
228
238
|
errorKey: 'internal.db',
|
|
@@ -237,7 +247,7 @@ export function withTransaction(repo: BaseModelRepository, logger: Logger, previ
|
|
|
237
247
|
}).catch((err: any) => {
|
|
238
248
|
// Error beginning transaction
|
|
239
249
|
releaseAndDecrement(repo, dbClient);
|
|
240
|
-
logger.error('Error beginning transaction: %j', err);
|
|
250
|
+
logger.error('Error beginning transaction at %s %s: %j', req.method, req.originalUrl, {err, stack: new Error().stack});
|
|
241
251
|
res.status(500).json({
|
|
242
252
|
error: {
|
|
243
253
|
errorKey: 'internal.db',
|
|
@@ -248,6 +258,7 @@ export function withTransaction(repo: BaseModelRepository, logger: Logger, previ
|
|
|
248
258
|
}).catch(err => {
|
|
249
259
|
// Error connecting to database for other reason than timeout or beginning transaction
|
|
250
260
|
clearTimeout(connectTimeoutHandler);
|
|
261
|
+
logger.error('Error connecting to database at %s %s: %j', req.method, req.originalUrl, {err, stack: new Error().stack});
|
|
251
262
|
withTransactionConfig.activeCount = Math.max(0, withTransactionConfig.activeCount - 1);
|
|
252
263
|
res.status(500).json({
|
|
253
264
|
error: {
|
package/services/utils.js
CHANGED
|
@@ -236,6 +236,7 @@ function getHtml(template, translationKeyValue) {
|
|
|
236
236
|
const replacer = (_, key) => replacePlaceholders(translationKeyValue, key.split('.'), 0) ?? key;
|
|
237
237
|
template = template.replace(pattern, replacer);
|
|
238
238
|
template = template.replace(pattern, replacer);
|
|
239
|
+
template = template.replace(pattern, replacer);
|
|
239
240
|
template = template.replace(new RegExp('\\[\\[\\s*([a-zA-Z._0-9:]+)\\s*\\]\\]', 'g'), (_, key) => {
|
|
240
241
|
const keyParts = key.split(':');
|
|
241
242
|
return (replacePlaceholders(translationKeyValue, keyParts[1].split('.'), 0) || [])
|
package/services/utils.ts
CHANGED
|
@@ -217,6 +217,7 @@ function getHtml(template: string, translationKeyValue: Record<string, any>): st
|
|
|
217
217
|
const replacer = (_: string, key: string) => replacePlaceholders(translationKeyValue, key.split('.'), 0) ?? key;
|
|
218
218
|
template = template.replace(pattern, replacer);
|
|
219
219
|
template = template.replace(pattern, replacer);
|
|
220
|
+
template = template.replace(pattern, replacer);
|
|
220
221
|
template = template.replace(new RegExp('\\[\\[\\s*([a-zA-Z._0-9:]+)\\s*\\]\\]', 'g'), (_, key) => {
|
|
221
222
|
const keyParts = key.split(':');
|
|
222
223
|
return (replacePlaceholders(translationKeyValue, keyParts[1].split('.'), 0) || [])
|