Nest: Improve Unknown[Element|Module]Exception

Created on 10 Jul 2018  路  5Comments  路  Source: nestjs/nest

I'm submitting a...


[ ] Regression 
[ ] 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


Currently the error message for when an 'element' or module is not found is rather cryptic.

e.g

    Nest cannot find given element (it does not exist in current context)
      at NestApplicationContext.findInstanceByPrototypeOrToken (../node_modules/@nestjs/core/nest-application-context.js:71:19)
      at NestApplicationContext.find (../node_modules/@nestjs/core/nest-application-context.js:38:21)
      at NestApplicationContext.get (../node_modules/@nestjs/core/nest-application-context.js:32:25)
      at Object.beforeAll (nodes/nodes.e2e-spec.ts:51:73)

Expected behavior


Let Nest tell me which element or module is not found.

Minimal reproduction of the problem with instructions

Just create a setup where one of the elements is never imported.

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


It's trivial to expose the name of the element/module:

https://github.com/nestjs/nest/blob/51d5be4e9da0cee7a5b16da2b91f3b5d3f8a2f30/packages/core/nest-application-context.ts#L107-L110

https://github.com/nestjs/nest/blob/51d5be4e9da0cee7a5b16da2b91f3b5d3f8a2f30/packages/core/nest-application-context.ts#L36-L39

As a more advanced solution it would be nice to get some kind of print out of the context.

Environment


Nest version: 5.1.0
core done 馃憦 type

Most helpful comment

Fixed by #1143

All 5 comments

Thanks for reporting @rhalff

I was unable to get an instance of my interceptor in main.ts using

app.select(SharedModule).get(SessionManagerInterceptor);

Error:

(node:15016) UnhandledPromiseRejectionWarning: Error: Nest cannot find given element (it does not exist in current context)
at NestApplicationContext.findInstanceByPrototypeOrToken (E:xamphtdocswwwloanApploan-apinode_modules@nestjscorenest-application-context.js:70:19)
at NestApplicationContext.find (E:xamphtdocswwwloanApploan-apinode_modules@nestjscorenest-application-context.js:38:21)
at NestApplicationContext.get (E:xamphtdocswwwloanApploan-apinode_modules@nestjscorenest-application-context.js:32:25)
at bootstrap (E:xamphtdocswwwloanApploan-apisrcmain.ts:16:30)
at
(node:15016) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a
promise which was not handled with .catch(). (rejection id: 1)
(node:15016) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a
non-zero exit code.

Nest version: 5.1.0

Any Solution to this?

Fixed by #1143

Fixed in 5.4.0

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

tronginc picture tronginc  路  3Comments

cojack picture cojack  路  3Comments

yanshuf0 picture yanshuf0  路  3Comments

thohoh picture thohoh  路  3Comments

mishelashala picture mishelashala  路  3Comments