quantummaid / injectmaid

Lightweight dependency injection framework that does not rely on annotations.
https://quantummaid.de
Apache License 2.0
9 stars 0 forks source link

[ISSUE] Backtrack to root cause of registration in exceptions #19

Open quantummaiddeveloper opened 4 years ago

quantummaiddeveloper commented 4 years ago

Do you have an idea to improve the project? Please describe. Exceptions should tell why a type is needed for injection.

Compelling example where the user has no clue what's wrong where

java.lang.ExceptionInInitializerError
...
Caused by: de.quantummaid.injectmaid.InjectMaidException: Cannot decide how to instantiate type 'java.lang.String':
More than one public constructors or factory methods found
No annotations have been detected
More than one public constructors found
Static factories are not considered because public constructors have been found
    at de.quantummaid.injectmaid.InjectMaidException.injectMaidException(InjectMaidException.java:32)
    at de.quantummaid.injectmaid.statemachine.StateMachineRunner.runStateMachine(StateMachineRunner.java:69)
    at de.quantummaid.injectmaid.InjectMaidBuilder.build(InjectMaidBuilder.java:179)
    at de.builderrahmen.kurator.reliablemessaging.test.Handler$Companion.createInjectMaid(Handler.kt:156)
    at de.builderrahmen.kurator.reliablemessaging.test.Handler$Companion.access$createInjectMaid(Handler.kt:54)
    at de.builderrahmen.kurator.reliablemessaging.test.Handler.<clinit>(Handler.kt:56)
    ... 59 more

Is your feature request related to a problem? Please describe. It is not always obvious what top-level registered type caused another type to be registered.

Describe the solution you'd like A stack-trace like visualization of the dependency tree that lead to the registration of a type (see MapMaid).

lestephane commented 4 years ago

Test Idea.

lestephane commented 3 years ago

I have encountered another case where this would help. I have a dummy InjectorConfiguration that denies injection of a specific dependency:

inline fun <reified T> InjectMaidBuilder.denyInjectionsOfType(clazz: Class<T>): InjectMaidBuilder {
    return this.withCustomType(clazz) {
        throw DependencyInjectionDeniedException(clazz)
    }
}

The error message does not say who tried to get the denied dependency injected

de.quantummaid.injectmaid.InjectMaidException: Exception during instantiation of 'SqsClient'
  using custom instantiation via 'SCRUBBED.DenyDependencyInjectorConfigKt$denyInjectionsOfType$1@6f901d62'
...
Caused by: de.builderrahmen.examples.counter.sum.internal.infra.di.DependencyInjectionDeniedException:
  software.amazon.awssdk.services.sqs.SqsClient

(no indication of what class was being injected)