Skip to content

Commit 6acde56

Browse files
Merge pull request #469 from ISlavinskyi/fix-connection-error-output
Added more informative output when unable to connect.
2 parents 86a0f4f + 3082e30 commit 6acde56

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

lib/common/typeorm.utils.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -111,19 +111,24 @@ export function handleRetry(
111111
retryAttempts = 9,
112112
retryDelay = 3000,
113113
connectionName = DEFAULT_CONNECTION_NAME,
114+
verboseRetryLog = false,
114115
): <T>(source: Observable<T>) => Observable<T> {
115116
return <T>(source: Observable<T>) =>
116117
source.pipe(
117-
retryWhen(e =>
118+
retryWhen((e) =>
118119
e.pipe(
119120
scan((errorCount, error: Error) => {
120121
const connectionInfo =
121122
connectionName === DEFAULT_CONNECTION_NAME
122123
? ''
123124
: ` (${connectionName})`;
125+
const verboseMessage = verboseRetryLog
126+
? ` Message: ${error.message}.`
127+
: '';
124128
logger.error(
125-
`Unable to connect to the database${connectionInfo}. Retrying (${errorCount +
126-
1})...`,
129+
`Unable to connect to the database${connectionInfo}.${verboseMessage} Retrying (${
130+
errorCount + 1
131+
})...`,
127132
error.stack,
128133
);
129134
if (errorCount + 1 >= retryAttempts) {

lib/interfaces/typeorm-options.interface.ts

+4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ export type TypeOrmModuleOptions = {
2121
* If `true`, connection will not be closed on application shutdown.
2222
*/
2323
keepConnectionAlive?: boolean;
24+
/**
25+
* If `true`, will show verbose error messages on each connection retry.
26+
*/
27+
verboseRetryLog?: boolean;
2428
} & Partial<ConnectionOptions>;
2529

2630
export interface TypeOrmOptionsFactory {

lib/typeorm-core.module.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,12 @@ export class TypeOrmCoreModule implements OnApplicationShutdown {
198198
} as ConnectionOptions);
199199
})
200200
.pipe(
201-
handleRetry(options.retryAttempts, options.retryDelay, connectionToken),
201+
handleRetry(
202+
options.retryAttempts,
203+
options.retryDelay,
204+
connectionToken,
205+
options.verboseRetryLog,
206+
),
202207
)
203208
.toPromise();
204209
}

0 commit comments

Comments
 (0)