From 7f0403d9247b45d24a5340271c69eb2abb0be39b Mon Sep 17 00:00:00 2001 From: miaowing Date: Tue, 17 Nov 2020 18:40:04 +0800 Subject: [PATCH 1/2] fix(): stop app with unexpected error when the connection is not ready --- lib/typeorm-core.module.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/typeorm-core.module.ts b/lib/typeorm-core.module.ts index eb84e6610..a5456bd60 100644 --- a/lib/typeorm-core.module.ts +++ b/lib/typeorm-core.module.ts @@ -6,6 +6,7 @@ import { OnApplicationShutdown, Provider, Type, + Logger, } from '@nestjs/common'; import { ModuleRef } from '@nestjs/core'; import { defer } from 'rxjs'; @@ -33,6 +34,8 @@ import { TYPEORM_MODULE_ID, TYPEORM_MODULE_OPTIONS } from './typeorm.constants'; @Global() @Module({}) export class TypeOrmCoreModule implements OnApplicationShutdown { + private readonly logger = new Logger('TypeOrmModule'); + constructor( @Inject(TYPEORM_MODULE_OPTIONS) private readonly options: TypeOrmModuleOptions, @@ -106,7 +109,11 @@ export class TypeOrmCoreModule implements OnApplicationShutdown { const connection = this.moduleRef.get( getConnectionToken(this.options as ConnectionOptions) as Type, ); - connection && (await connection.close()); + try { + connection && (await connection.close()); + } catch (e) { + this.logger.error(e.message); + } } private static createAsyncProviders( From 9238f1cee48887b3736be434855e5b14248681d2 Mon Sep 17 00:00:00 2001 From: Kamil Mysliwiec Date: Tue, 17 Nov 2020 11:55:01 +0100 Subject: [PATCH 2/2] Update lib/typeorm-core.module.ts --- lib/typeorm-core.module.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/typeorm-core.module.ts b/lib/typeorm-core.module.ts index a5456bd60..2b16ea78d 100644 --- a/lib/typeorm-core.module.ts +++ b/lib/typeorm-core.module.ts @@ -112,7 +112,7 @@ export class TypeOrmCoreModule implements OnApplicationShutdown { try { connection && (await connection.close()); } catch (e) { - this.logger.error(e.message); + this.logger.error(e?.message); } }