2

I setuped configuration validation as described in documentation of NestJS: https://docs.nestjs.com/techniques/configuration#schema-validation

Validation works as expected, but when my .env file is not valid, I am getting plain error, for example:

Error: Config validation error: "FOO_BAR" is required at Function.forRoot (<PATH>\@nestjs\config\dist\config.module.js:44:27)

This error is not thrown by Nest so approach with globalFilter what can catch every Nest error not work: https://docs.nestjs.com/websockets/exception-filters#exception-filters

How to catch this error to be able to format it?

EDIT: This is more general question: How to catch errors thrown inside forRoot, forFeature in NestJS?

Baterka
  • 3,075
  • 5
  • 31
  • 60
  • How would you like to format the error? Are you trying to send it somewhere? – Jay McDoniel Feb 25 '20 at 16:35
  • No. I am just trying to catch it and do nice formatting...But I am not able to catch error what is thrown in `forRoot` of module... It seems like poor design. – Baterka Feb 25 '20 at 16:37
  • You could always try wrapping the `NestFactory.create` in a `try/catch` and catch all startup errors. Not really sure what you'd be doing with formatting for the error, but I'd say essentially if an error is thrown on bootstrap you wouldn't want the server to start in the first place – Jay McDoniel Feb 25 '20 at 16:43
  • Not working. I already tried this. Also I do not want to start server, I just want nicer error to let user know about whats wrong and not annoy him with long stacks. – Baterka Feb 25 '20 at 17:19

0 Answers0