Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NestJS seems to fail silently #287

Closed
RDeluxe opened this issue Dec 5, 2017 · 12 comments

Comments

@RDeluxe
Copy link

commented Dec 5, 2017

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
@RDeluxe

This comment has been minimized.

Copy link
Author

commented Dec 5, 2017

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

@kamilmysliwiec

This comment has been minimized.

Copy link
Member

commented Dec 11, 2017

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

@br0wn

This comment has been minimized.

Copy link

commented Dec 29, 2017

@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)
    }
];
@Yonben

This comment has been minimized.

Copy link

commented Jan 5, 2018

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);
      }
    });
}
@RDeluxe

This comment has been minimized.

Copy link
Author

commented Jan 7, 2018

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
@Yonben

This comment has been minimized.

Copy link

commented Jan 31, 2018

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

@RDeluxe

This comment has been minimized.

Copy link
Author

commented Feb 5, 2018

I concur, this is still a bug.

@kamilmysliwiec

This comment has been minimized.

Copy link
Member

commented Feb 5, 2018

I'm on it, thx

@kamilmysliwiec

This comment has been minimized.

Copy link
Member

commented Feb 15, 2018

This bug is fixed in v4.6.0 🎉

@FriOne

This comment has been minimized.

Copy link

commented Feb 16, 2018

4.6.1*

@ps2goat

This comment has been minimized.

Copy link

commented Sep 25, 2018

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?

@kamilmysliwiec

This comment has been minimized.

Copy link
Member

commented Sep 25, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
6 participants
You can’t perform that action at this time.