Skip to content

Commit

Permalink
Merge pull request #469 from ISlavinskyi/fix-connection-error-output
Browse files Browse the repository at this point in the history
Added more informative output when unable to connect.
  • Loading branch information
kamilmysliwiec authored May 26, 2020
2 parents 86a0f4f + 3082e30 commit 6acde56
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
11 changes: 8 additions & 3 deletions lib/common/typeorm.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,19 +111,24 @@ export function handleRetry(
retryAttempts = 9,
retryDelay = 3000,
connectionName = DEFAULT_CONNECTION_NAME,
verboseRetryLog = false,
): <T>(source: Observable<T>) => Observable<T> {
return <T>(source: Observable<T>) =>
source.pipe(
retryWhen(e =>
retryWhen((e) =>
e.pipe(
scan((errorCount, error: Error) => {
const connectionInfo =
connectionName === DEFAULT_CONNECTION_NAME
? ''
: ` (${connectionName})`;
const verboseMessage = verboseRetryLog
? ` Message: ${error.message}.`
: '';
logger.error(
`Unable to connect to the database${connectionInfo}. Retrying (${errorCount +
1})...`,
`Unable to connect to the database${connectionInfo}.${verboseMessage} Retrying (${
errorCount + 1
})...`,
error.stack,
);
if (errorCount + 1 >= retryAttempts) {
Expand Down
4 changes: 4 additions & 0 deletions lib/interfaces/typeorm-options.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ export type TypeOrmModuleOptions = {
* If `true`, connection will not be closed on application shutdown.
*/
keepConnectionAlive?: boolean;
/**
* If `true`, will show verbose error messages on each connection retry.
*/
verboseRetryLog?: boolean;
} & Partial<ConnectionOptions>;

export interface TypeOrmOptionsFactory {
Expand Down
7 changes: 6 additions & 1 deletion lib/typeorm-core.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,12 @@ export class TypeOrmCoreModule implements OnApplicationShutdown {
} as ConnectionOptions);
})
.pipe(
handleRetry(options.retryAttempts, options.retryDelay, connectionToken),
handleRetry(
options.retryAttempts,
options.retryDelay,
connectionToken,
options.verboseRetryLog,
),
)
.toPromise();
}
Expand Down

0 comments on commit 6acde56

Please sign in to comment.