Nest: NestJS seems to fail silently

Created on 5 Dec 2017  路  13Comments  路  Source: nestjs/nest

I'm submitting a...


[ ] Regression 
[x ] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

I'm starting a new project using Nest and TypeORM. I have a few modules, a few entities, nothing too complicated yet.
Everything was working fine, but after creating new entities the project is not starting anymore. No errors are thrown, the server is just stopping and Nest is not initializing the modules to the end. I suspect TypeORM to be failing because of my new entities, but without errors it's hard to debug.

code - insiders_2017-12-05_18-25-18

Expected behavior

Nest should start the server or throw an error and its stack.

Minimal reproduction of the problem with instructions

Use the example repo available here https://github.com/nestjs/nest/tree/master/examples/05-sql-typeorm

Then (I'm guessing) make a mistake in a new entity.
You can also specify a wrong path for entities in your database.providers.ts (__dirname + '/../../**/**.entity.ts'). A wrong path means no entity, and TypeORM seems to fail (silently again).

What is the motivation / use case for changing the behavior?

Environment


Nest version: 4.4.2


For Tooling issues:
- Node version: 8.1.4 
- Platform: Windows  

Others:

- Postgresql
type question 馃檶 todo 馃挌

Most helpful comment

I'm on it, thx

All 13 comments

After further testing, it seems that typeorm is indeed returning an error, but while instantiating the modules Nest is not catching it.

Here is the error when trying to create the db connection outside of the db.providers.ts

code - insiders_2017-12-05_19-05-35

Hi @RDeluxe,
I did everything to reproduce this issue (even by throwing an exception directly from the factory function) and I can't do that. 馃檨 Wrong path is working properly as well

@kamilmysliwiec I encountered same behavior when provider factory threw error. Please note the example code

   {
        provide: 'push_service',
        inject: [AppConfiguration, 'logger'],
        useFactory: (
            config: AppConfiguration,
            logger: LoggerInterface
        ) => {
            const configuration = new PushServiceConfiguration();
            configuration.apiKey = config.apiKey;

            const pushService = PushService(configuration); // <-- note missing `new` keyword here
            pushService.setLogger(logger);

            return pushService;
        }
    }

This resulted in npm run failing silently with only following output

[Nest] 24763   - 2017-12-29 10:37:03   [NestFactory] Starting Nest application...

After some investigation I noticed this does happens if service being provided is being injected elsewhere and does not happen when this isn't the case:

/*
* This will throw output
*    Error at Object.useFactory
*/
[
    {
        provide: 'not_injected_service',
        useFactory: () => {
            throw new Error(); 
        }
    },
]

/*
* This will fail silently
*/
[
    {
        provide: 'push_service',
        useFactory: () => {
            throw new Error();
        }
    },
    {
        provide: 'push_consumer',
        inject: ['push_service'],
        useFactory: (pushService) => return new PushConsumer(pushService)
    }
];

Same here. My DB server was down and Nest just failed silently. After noticing I refactored the provider to throw an error in case of timeout, and the error isn't shown in the start output.

Here is my code:

import { createConnection } from 'typeorm';

export const databaseProviders = [
  {
    provide: 'DbConnectionToken',
    useFactory: async () => await initConnection(),
  },
];

function initConnection() {
  return createConnection({
      type: 'mysql',
      host: process.env.DB_HOST,
      port: 3306,
      username: process.env.DB_USERNAME,
      password: process.env.DB_PASSWORD,
      database: 'myapp',
      entities: [
        __dirname + '/../**/*.entity{.ts,.js}',
      ],
      synchronize: true,
    })
    .catch((error) => {
      if (error.code === 'ETIMEDOUT') {
        throw new Error('Connection to DB timed out..');
      } else {
        console.log('Error in Database Provider..');
        console.log(error);
      }
    });
}

I just had the same problem again on a Kubernet pod. I forgot to create the Psql pod and ofc Typeorm could not find anything. The only log I have :

yarn run v1.3.2
warning package.json: License should be a valid SPDX license expression
$ ts-node --verbose -r tsconfig-paths/register ./src/server.ts
[Nest] 52   -
2018-1-6 20:00:57
[NestFactory]
Starting Nest application...


[Nest] 52   -
2018-1-6 20:00:57
[InstanceLoader]
ApplicationModule dependencies initialized
 +9ms

@kamilmysliwiec Can you reopen the issue or should we open a new one ?

I concur, this is still a bug.

I'm on it, thx

This bug is fixed in v4.6.0 馃帀

4.6.1*

I'm getting no errors when an async provider blows up, using

"@nestjs/common": "5.3.9",
"@nestjs/core": "5.3.10",

It just exits. Is this the same as the OP's issue that was fixed in 4.6.1?

@ps2goat please create a new issue and provide a repository which reproduces your issue

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cojack picture cojack  路  3Comments

hackboy picture hackboy  路  3Comments

KamGor picture KamGor  路  3Comments

tronginc picture tronginc  路  3Comments

breitsmiley picture breitsmiley  路  3Comments