iobroker.sql 4.1.2 → 4.1.4
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/README.md +7 -7
- package/build/lib/errors.js +127 -0
- package/build/lib/errors.js.map +1 -0
- package/build/main.js +67 -16
- package/build/main.js.map +1 -1
- package/io-package.json +27 -27
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -618,6 +618,13 @@ sendTo('sql.0', 'getEnabledDPs', {}, function (result) {
|
|
|
618
618
|
-->
|
|
619
619
|
|
|
620
620
|
## Changelog
|
|
621
|
+
### 4.1.4 (2026-08-27)
|
|
622
|
+
* (@GermanBluefox) Connection errors no longer start with the useless class name `AggregateError`: the log now shows only the real reason, e.g. `connect ECONNREFUSED 127.0.0.1:3306; connect ECONNREFUSED ::1:3306`
|
|
623
|
+
|
|
624
|
+
### 4.1.3 (2026-08-27)
|
|
625
|
+
* (@GermanBluefox) Connection errors are logged with the real reason again: Node reports a failed TCP connect as an `AggregateError` whose own message is empty, so the log only showed the word `AggregateError` instead of e.g. `connect ECONNREFUSED 127.0.0.1:3306`
|
|
626
|
+
* (@GermanBluefox) The reconnect loop no longer repeats the same connection error every 30 seconds: the first occurrence is logged as error, repetitions go to debug and once an hour a reminder is logged
|
|
627
|
+
|
|
621
628
|
### 4.1.2 (2026-08-27)
|
|
622
629
|
* (@GermanBluefox) Fixed `enableHistory` being answered with `success: true` but silently doing nothing when it arrived while the adapter was still starting up: the adapter subscribed to object changes only after it had read the logging settings, so a message that landed in that gap activated no logging
|
|
623
630
|
* (@joltcoke) Fixed average and total returning null for every interval that contains a null value: parseFloat(null) is NaN and poisoned the sum of the whole interval (thanks to @joltcoke, ioBroker/ioBroker.sql#526). As the result was NaN and not null, ignoreNull could not act on it either
|
|
@@ -639,13 +646,6 @@ sendTo('sql.0', 'getEnabledDPs', {}, function (result) {
|
|
|
639
646
|
### 4.0.4 (2026-08-11)
|
|
640
647
|
* (@GermanBluefox) Fixed that nothing was stored for datapoints with an `aliasId`: the adapter subscribed to the alias name instead of the real state ID, so no state change ever arrived
|
|
641
648
|
|
|
642
|
-
### 4.0.3 (2026-08-11)
|
|
643
|
-
* (@GermanBluefox) Corrected a small configuration error
|
|
644
|
-
|
|
645
|
-
### 4.0.2 (2026-08-10)
|
|
646
|
-
* (@GermanBluefox) Fixed empty charts for the aggregation `onchange` ("raw" in e-charts): it was run through the interval aggregation and returned only `null` values
|
|
647
|
-
* (@GermanBluefox) The MySQL and phpMyAdmin docker containers are no longer enabled by default: instances without the docker settings in their config (e.g. after an update from 3.x) reported "Docker is not installed"
|
|
648
|
-
|
|
649
649
|
[Older changelogs can be found there](CHANGELOG_OLD.md)
|
|
650
650
|
|
|
651
651
|
## License
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.formatError = formatError;
|
|
4
|
+
// Only the first few sub-errors of an AggregateError are rendered - a DNS name with many A/AAAA
|
|
5
|
+
// records would otherwise produce a log line of arbitrary length.
|
|
6
|
+
const MAX_NESTED_ERRORS = 5;
|
|
7
|
+
const MAX_DEPTH = 3;
|
|
8
|
+
/**
|
|
9
|
+
* Error names that say nothing about the cause. `Error` is the default of every `new Error()`, and
|
|
10
|
+
* `AggregateError` is just the box Node puts the real connect errors into - neither belongs in a log
|
|
11
|
+
* line. A driver-specific name like `ConnectionError` or a `TypeError` is kept, it does tell something.
|
|
12
|
+
*/
|
|
13
|
+
const GENERIC_ERROR_NAMES = ['Error', 'AggregateError'];
|
|
14
|
+
/** Fields drivers put on their errors that are worth showing when the message alone says nothing */
|
|
15
|
+
const DETAIL_FIELDS = ['code', 'errno', 'syscall', 'address', 'port', 'sqlMessage'];
|
|
16
|
+
/**
|
|
17
|
+
* Render an error as one readable line.
|
|
18
|
+
*
|
|
19
|
+
* `String(err)` is not good enough for connection errors: Node's happy-eyeballs connect (and with it
|
|
20
|
+
* every driver that opens a TCP socket) rejects with an `AggregateError` whose own `message` is empty,
|
|
21
|
+
* so `toString()` degrades to a bare "AggregateError" and the actual reason - "connect ECONNREFUSED
|
|
22
|
+
* 127.0.0.1:3306" for each address that was tried - stays hidden in `err.errors`. Unwrap that array,
|
|
23
|
+
* unwrap `err.cause`, and append the driver's error code when the message does not contain it already.
|
|
24
|
+
*
|
|
25
|
+
* @param err whatever was caught or handed to a callback
|
|
26
|
+
* @param depth recursion depth, used internally for nested errors
|
|
27
|
+
* @returns a non-empty, single-line description
|
|
28
|
+
*/
|
|
29
|
+
function formatError(err, depth = 0) {
|
|
30
|
+
if (err === null || err === undefined) {
|
|
31
|
+
return 'Unknown error';
|
|
32
|
+
}
|
|
33
|
+
if (typeof err === 'string') {
|
|
34
|
+
return err || 'Unknown error';
|
|
35
|
+
}
|
|
36
|
+
if (typeof err === 'number' || typeof err === 'boolean' || typeof err === 'bigint') {
|
|
37
|
+
return String(err);
|
|
38
|
+
}
|
|
39
|
+
if (typeof err !== 'object') {
|
|
40
|
+
// symbol or function - nothing sensible to print
|
|
41
|
+
return Object.prototype.toString.call(err);
|
|
42
|
+
}
|
|
43
|
+
const error = err;
|
|
44
|
+
const message = typeof error.message === 'string' ? oneLine(error.message) : '';
|
|
45
|
+
const name = typeof error.name === 'string' && error.name ? error.name : '';
|
|
46
|
+
let text = '';
|
|
47
|
+
if (name && !GENERIC_ERROR_NAMES.includes(name)) {
|
|
48
|
+
text = message ? `${name}: ${message}` : name;
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
text = message;
|
|
52
|
+
}
|
|
53
|
+
// AggregateError: the reason is in `errors`, not in `message`
|
|
54
|
+
if (Array.isArray(error.errors) && error.errors.length && depth < MAX_DEPTH) {
|
|
55
|
+
const details = [];
|
|
56
|
+
for (const nested of error.errors.slice(0, MAX_NESTED_ERRORS)) {
|
|
57
|
+
const nestedText = describeNested(nested, depth + 1);
|
|
58
|
+
// both stacks of a happy-eyeballs connect fail the same way more often than not
|
|
59
|
+
if (nestedText && !details.includes(nestedText)) {
|
|
60
|
+
details.push(nestedText);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
if (error.errors.length > MAX_NESTED_ERRORS) {
|
|
64
|
+
details.push(`and ${error.errors.length - MAX_NESTED_ERRORS} more`);
|
|
65
|
+
}
|
|
66
|
+
if (details.length) {
|
|
67
|
+
text = text ? `${text}: ${details.join('; ')}` : details.join('; ');
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
for (const field of DETAIL_FIELDS) {
|
|
71
|
+
const value = error[field];
|
|
72
|
+
if ((typeof value === 'string' && value) || typeof value === 'number') {
|
|
73
|
+
if (!text.includes(String(value))) {
|
|
74
|
+
text = text ? `${text} (${field}: ${value})` : `${field}: ${value}`;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
if (error.cause !== undefined && error.cause !== null && depth < MAX_DEPTH) {
|
|
79
|
+
const cause = formatError(error.cause, depth + 1);
|
|
80
|
+
if (cause && !text.includes(cause)) {
|
|
81
|
+
text = text ? `${text}; caused by ${cause}` : cause;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
if (!text) {
|
|
85
|
+
// nothing but an empty shell - show what it actually holds
|
|
86
|
+
try {
|
|
87
|
+
const json = JSON.stringify(error);
|
|
88
|
+
if (json && json !== '{}') {
|
|
89
|
+
return json;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
catch {
|
|
93
|
+
// circular or non-serializable - fall through
|
|
94
|
+
}
|
|
95
|
+
return name || Object.prototype.toString.call(error);
|
|
96
|
+
}
|
|
97
|
+
return text;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Squeeze all whitespace into single spaces, so a multi-line driver message stays one log line
|
|
101
|
+
*
|
|
102
|
+
* @param text the text to normalize
|
|
103
|
+
* @returns the text without line breaks
|
|
104
|
+
*/
|
|
105
|
+
function oneLine(text) {
|
|
106
|
+
return text.replace(/\s+/g, ' ').trim();
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Describe one sub-error of an AggregateError: the message alone if there is one, because it usually
|
|
110
|
+
* already carries the code ("connect ECONNREFUSED 127.0.0.1:3306").
|
|
111
|
+
*
|
|
112
|
+
* @param err the nested error
|
|
113
|
+
* @param depth current recursion depth
|
|
114
|
+
* @returns a short description of the nested error
|
|
115
|
+
*/
|
|
116
|
+
function describeNested(err, depth) {
|
|
117
|
+
if (err && typeof err === 'object') {
|
|
118
|
+
const nested = err;
|
|
119
|
+
const message = typeof nested.message === 'string' ? oneLine(nested.message) : '';
|
|
120
|
+
if (message) {
|
|
121
|
+
const code = typeof nested.code === 'string' ? nested.code : '';
|
|
122
|
+
return code && !message.includes(code) ? `${message} (${code})` : message;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
return formatError(err, depth);
|
|
126
|
+
}
|
|
127
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/lib/errors.ts"],"names":[],"mappings":";;AA4BA,kCA0EC;AAtGD,gGAAgG;AAChG,kEAAkE;AAClE,MAAM,iBAAiB,GAAG,CAAC,CAAC;AAC5B,MAAM,SAAS,GAAG,CAAC,CAAC;AAEpB;;;;GAIG;AACH,MAAM,mBAAmB,GAAG,CAAC,OAAO,EAAE,gBAAgB,CAAC,CAAC;AAExD,oGAAoG;AACpG,MAAM,aAAa,GAAG,CAAC,MAAM,EAAE,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,EAAE,YAAY,CAAU,CAAC;AAE7F;;;;;;;;;;;;GAYG;AACH,SAAgB,WAAW,CAAC,GAAY,EAAE,KAAK,GAAG,CAAC;IAC/C,IAAI,GAAG,KAAK,IAAI,IAAI,GAAG,KAAK,SAAS,EAAE,CAAC;QACpC,OAAO,eAAe,CAAC;IAC3B,CAAC;IACD,IAAI,OAAO,GAAG,KAAK,QAAQ,EAAE,CAAC;QAC1B,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,MAAM,IAAI,GAAG,OAAO,MAAM,CAAC,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC;YAChE,OAAO,IAAI,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,GAAG,OAAO,KAAK,IAAI,GAAG,CAAC,CAAC,CAAC,OAAO,CAAC;QAC9E,CAAC;IACL,CAAC;IACD,OAAO,WAAW,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;AACnC,CAAC"}
|
package/build/main.js
CHANGED
|
@@ -46,6 +46,7 @@ const mssql_client_1 = require("./lib/mssql-client");
|
|
|
46
46
|
const mysql_client_1 = require("./lib/mysql-client");
|
|
47
47
|
const postgresql_client_1 = require("./lib/postgresql-client");
|
|
48
48
|
const sqlite3_client_1 = require("./lib/sqlite3-client");
|
|
49
|
+
const errors_1 = require("./lib/errors");
|
|
49
50
|
const SQLFuncs = {
|
|
50
51
|
mssql: {
|
|
51
52
|
init: MSSQL.init,
|
|
@@ -175,6 +176,8 @@ function isEqual(a, b) {
|
|
|
175
176
|
const MAX_TASKS = 100;
|
|
176
177
|
/** Maximal number of rows one `getRawEntries` call may return */
|
|
177
178
|
const MAX_RAW_ENTRIES = 2000;
|
|
179
|
+
/** How often an unchanged connection error is repeated as error in the log */
|
|
180
|
+
const REPEATED_ERROR_INTERVAL = 3_600_000;
|
|
178
181
|
function sortByTs(a, b) {
|
|
179
182
|
const aTs = a.ts;
|
|
180
183
|
const bTs = b.ts;
|
|
@@ -203,6 +206,10 @@ class SqlAdapter extends adapter_core_1.Adapter {
|
|
|
203
206
|
postgresDbCreated = false;
|
|
204
207
|
lockTasks = false;
|
|
205
208
|
sqlFuncs = null;
|
|
209
|
+
/** Last connection error already written to the log, to not repeat it on every reconnect attempt */
|
|
210
|
+
lastConnectionError = null;
|
|
211
|
+
lastConnectionErrorTs = 0;
|
|
212
|
+
connectionErrorCount = 0;
|
|
206
213
|
constructor(options = {}) {
|
|
207
214
|
super({
|
|
208
215
|
...options,
|
|
@@ -348,7 +355,7 @@ class SqlAdapter extends adapter_core_1.Adapter {
|
|
|
348
355
|
if (!err && client) {
|
|
349
356
|
// make sure we always have at least one error listener to prevent crashes
|
|
350
357
|
if (client.on && client.listenerCount && !client.listenerCount('error')) {
|
|
351
|
-
client.on('error', (err) => this.log.warn(`SQL client error: ${err}`));
|
|
358
|
+
client.on('error', (err) => this.log.warn(`SQL client error: ${(0, errors_1.formatError)(err)}`));
|
|
352
359
|
}
|
|
353
360
|
}
|
|
354
361
|
else if (!client) {
|
|
@@ -546,6 +553,46 @@ class SqlAdapter extends adapter_core_1.Adapter {
|
|
|
546
553
|
void this.setState('info.connection', this.sqlConnected, true);
|
|
547
554
|
}
|
|
548
555
|
}
|
|
556
|
+
/**
|
|
557
|
+
* Log a connection error without flooding the log.
|
|
558
|
+
*
|
|
559
|
+
* `connect()` retries every 30 seconds, so a database that is simply switched off would otherwise
|
|
560
|
+
* write the very same line 2880 times a day. The first occurrence is logged as error, repetitions of
|
|
561
|
+
* the identical message go to debug, and once an hour a reminder is logged that the database is
|
|
562
|
+
* still unreachable.
|
|
563
|
+
*
|
|
564
|
+
* @param err error to report
|
|
565
|
+
* @param prefix optional text in front of the error message
|
|
566
|
+
*/
|
|
567
|
+
logConnectionError(err, prefix) {
|
|
568
|
+
const text = `${prefix ? `${prefix}: ` : ''}${(0, errors_1.formatError)(err)}`;
|
|
569
|
+
const now = Date.now();
|
|
570
|
+
if (text === this.lastConnectionError) {
|
|
571
|
+
this.connectionErrorCount++;
|
|
572
|
+
if (now - this.lastConnectionErrorTs >= REPEATED_ERROR_INTERVAL) {
|
|
573
|
+
this.lastConnectionErrorTs = now;
|
|
574
|
+
this.log.error(`${text} (still failing, ${this.connectionErrorCount} attempts)`);
|
|
575
|
+
}
|
|
576
|
+
else {
|
|
577
|
+
this.log.debug(`${text} (attempt ${this.connectionErrorCount})`);
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
else {
|
|
581
|
+
this.lastConnectionError = text;
|
|
582
|
+
this.lastConnectionErrorTs = now;
|
|
583
|
+
this.connectionErrorCount = 1;
|
|
584
|
+
this.log.error(text);
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
/** Forget the last connection error, so a new outage is logged again as error */
|
|
588
|
+
resetConnectionError() {
|
|
589
|
+
if (this.connectionErrorCount > 1) {
|
|
590
|
+
this.log.info(`Database is reachable again after ${this.connectionErrorCount} failed attempts`);
|
|
591
|
+
}
|
|
592
|
+
this.lastConnectionError = null;
|
|
593
|
+
this.lastConnectionErrorTs = 0;
|
|
594
|
+
this.connectionErrorCount = 0;
|
|
595
|
+
}
|
|
549
596
|
connect(callback) {
|
|
550
597
|
if (this.reconnectTimeout) {
|
|
551
598
|
clearTimeout(this.reconnectTimeout);
|
|
@@ -618,10 +665,10 @@ class SqlAdapter extends adapter_core_1.Adapter {
|
|
|
618
665
|
// connect first to DB postgres and create iobroker DB
|
|
619
666
|
this.log.info(`Postgres connection options: ${JSON.stringify(postgreSQLOptions).replace(postgreSQLOptions.password || '******', '****')}`);
|
|
620
667
|
const _client = new postgresql_client_1.PostgreSQLClient(postgreSQLOptions);
|
|
621
|
-
_client.on?.('error', (err) => this.log.warn(`SQL client error: ${err}`));
|
|
668
|
+
_client.on?.('error', (err) => this.log.warn(`SQL client error: ${(0, errors_1.formatError)(err)}`));
|
|
622
669
|
return _client.connect((err) => {
|
|
623
670
|
if (err) {
|
|
624
|
-
this.
|
|
671
|
+
this.logConnectionError(err, `Cannot connect to ${this.config.host}`);
|
|
625
672
|
if (this.reconnectTimeout) {
|
|
626
673
|
clearTimeout(this.reconnectTimeout);
|
|
627
674
|
}
|
|
@@ -647,7 +694,7 @@ class SqlAdapter extends adapter_core_1.Adapter {
|
|
|
647
694
|
if (typedError && typedError.code !== '42P04') {
|
|
648
695
|
// if error not about yet exists
|
|
649
696
|
this.postgresDbCreated = false;
|
|
650
|
-
this.
|
|
697
|
+
this.logConnectionError(typedError, `Cannot create database ${this.config.dbname}`);
|
|
651
698
|
this.reconnectTimeout && clearTimeout(this.reconnectTimeout);
|
|
652
699
|
this.reconnectTimeout = setTimeout(() => {
|
|
653
700
|
this.reconnectTimeout = null;
|
|
@@ -691,8 +738,9 @@ class SqlAdapter extends adapter_core_1.Adapter {
|
|
|
691
738
|
if (err) {
|
|
692
739
|
this.clientPool = null;
|
|
693
740
|
this.setConnected(false);
|
|
694
|
-
// JSON.stringify(Error) is "{}" and
|
|
695
|
-
|
|
741
|
+
// JSON.stringify(Error) is "{}", and toString() of an AggregateError is just
|
|
742
|
+
// "AggregateError" - formatError() digs the real reason out of both
|
|
743
|
+
this.logConnectionError(err);
|
|
696
744
|
this.reconnectTimeout && clearTimeout(this.reconnectTimeout);
|
|
697
745
|
this.reconnectTimeout = setTimeout(() => {
|
|
698
746
|
this.reconnectTimeout = null;
|
|
@@ -708,8 +756,10 @@ class SqlAdapter extends adapter_core_1.Adapter {
|
|
|
708
756
|
});
|
|
709
757
|
}
|
|
710
758
|
catch (ex) {
|
|
711
|
-
this.
|
|
712
|
-
|
|
759
|
+
this.logConnectionError(ex);
|
|
760
|
+
if (ex.stack) {
|
|
761
|
+
this.log.debug(ex.stack);
|
|
762
|
+
}
|
|
713
763
|
this.clientPool = null;
|
|
714
764
|
this.activeConnections = 0;
|
|
715
765
|
this.setConnected(false);
|
|
@@ -731,6 +781,7 @@ class SqlAdapter extends adapter_core_1.Adapter {
|
|
|
731
781
|
}, 30000);
|
|
732
782
|
}
|
|
733
783
|
else {
|
|
784
|
+
this.resetConnectionError();
|
|
734
785
|
this.log.info(`Connected to ${this.config.dbtype}`);
|
|
735
786
|
// read all DB IDs and all FROM ids
|
|
736
787
|
this.getAllIds(() => this.getAllFroms(callback));
|
|
@@ -830,7 +881,7 @@ class SqlAdapter extends adapter_core_1.Adapter {
|
|
|
830
881
|
this.sendTo(msg.from, msg.command, { error: 'Unknown DB type' }, msg.callback);
|
|
831
882
|
return;
|
|
832
883
|
}
|
|
833
|
-
client.on?.('error', (err) => this.log.warn(`SQL client error: ${err}`));
|
|
884
|
+
client.on?.('error', (err) => this.log.warn(`SQL client error: ${(0, errors_1.formatError)(err)}`));
|
|
834
885
|
this.testConnectTimeout = setTimeout(() => {
|
|
835
886
|
this.testConnectTimeout = null;
|
|
836
887
|
this.sendTo(msg.from, msg.command, { error: 'connect timeout' }, msg.callback);
|
|
@@ -841,7 +892,7 @@ class SqlAdapter extends adapter_core_1.Adapter {
|
|
|
841
892
|
clearTimeout(this.testConnectTimeout);
|
|
842
893
|
this.testConnectTimeout = null;
|
|
843
894
|
}
|
|
844
|
-
this.sendTo(msg.from, msg.command, { error:
|
|
895
|
+
this.sendTo(msg.from, msg.command, { error: (0, errors_1.formatError)(err) }, msg.callback);
|
|
845
896
|
return;
|
|
846
897
|
}
|
|
847
898
|
client.execute('SELECT 2 + 3 AS x', (err /* , rows, fields */) => {
|
|
@@ -852,7 +903,7 @@ class SqlAdapter extends adapter_core_1.Adapter {
|
|
|
852
903
|
if (this.testConnectTimeout) {
|
|
853
904
|
clearTimeout(this.testConnectTimeout);
|
|
854
905
|
this.testConnectTimeout = null;
|
|
855
|
-
this.sendTo(msg.from, msg.command, { error: err
|
|
906
|
+
this.sendTo(msg.from, msg.command, { error: err ? (0, errors_1.formatError)(err) : null }, msg.callback);
|
|
856
907
|
return;
|
|
857
908
|
}
|
|
858
909
|
}
|
|
@@ -898,7 +949,7 @@ class SqlAdapter extends adapter_core_1.Adapter {
|
|
|
898
949
|
this.log.debug(msg.message);
|
|
899
950
|
this.borrowClientFromPool((err, client) => {
|
|
900
951
|
if (err || !client) {
|
|
901
|
-
this.sendTo(msg.from, msg.command, { error: err
|
|
952
|
+
this.sendTo(msg.from, msg.command, { error: err ? (0, errors_1.formatError)(err) : 'No client' }, msg.callback);
|
|
902
953
|
this.returnClientToPool(client);
|
|
903
954
|
callback?.();
|
|
904
955
|
}
|
|
@@ -950,7 +1001,7 @@ class SqlAdapter extends adapter_core_1.Adapter {
|
|
|
950
1001
|
this.activeConnections = 0;
|
|
951
1002
|
this.clientPool = null;
|
|
952
1003
|
this.setConnected(false);
|
|
953
|
-
this.
|
|
1004
|
+
this.logConnectionError(err || 'No database connection');
|
|
954
1005
|
return cb?.(err || new Error('No database connection'));
|
|
955
1006
|
}
|
|
956
1007
|
this.log.debug(script);
|
|
@@ -1638,7 +1689,7 @@ class SqlAdapter extends adapter_core_1.Adapter {
|
|
|
1638
1689
|
this.borrowClientFromPool((err, client) => {
|
|
1639
1690
|
if (err || !client) {
|
|
1640
1691
|
this.returnClientToPool(client);
|
|
1641
|
-
this.log.error(err
|
|
1692
|
+
this.log.error(err ? (0, errors_1.formatError)(err) : 'No client');
|
|
1642
1693
|
cb?.();
|
|
1643
1694
|
}
|
|
1644
1695
|
else {
|
|
@@ -1705,7 +1756,7 @@ class SqlAdapter extends adapter_core_1.Adapter {
|
|
|
1705
1756
|
this.borrowClientFromPool((err, client) => {
|
|
1706
1757
|
if (err || !client) {
|
|
1707
1758
|
this.returnClientToPool(client);
|
|
1708
|
-
this.log.error(err
|
|
1759
|
+
this.log.error(err ? (0, errors_1.formatError)(err) : 'No client');
|
|
1709
1760
|
cb?.(); // BF asked (2021.12.14): may be return here err?
|
|
1710
1761
|
return;
|
|
1711
1762
|
}
|
|
@@ -1735,7 +1786,7 @@ class SqlAdapter extends adapter_core_1.Adapter {
|
|
|
1735
1786
|
this.borrowClientFromPool((err, client) => {
|
|
1736
1787
|
if (err || !client) {
|
|
1737
1788
|
this.returnClientToPool(client);
|
|
1738
|
-
this.log.error(err
|
|
1789
|
+
this.log.error(err ? (0, errors_1.formatError)(err) : 'No client');
|
|
1739
1790
|
cb?.(err || new Error('No client'));
|
|
1740
1791
|
}
|
|
1741
1792
|
else {
|