Repository: ktorio/ktor
File: ktor-server/ktor-server-plugins/ktor-server-di/common/src/io/ktor/server/plugins/di/DependencyInjection.kt (Kotlin)

Code before:

        with(application) {
            for (reference in configuredDependencyReferences) {
                installReference(registry, reference)
            }
            monitor.subscribe(ApplicationModulesLoaded) {
                val exceptions = mutableListOf<Pair<DependencyKey, Throwable>>()
                for ((key, source) in registry.requirements) {
                    try {
                        registry.get<Any>(key)
                    } catch (e: Throwable) {
                        environment.log.error("Cannot resolve $key\n${source.externalTrace()}")
                        exceptions += key to e
                    }
                }
                when (exceptions.size) {
                    0 -> environment.log.debug("All dependencies resolved successfully")
                    else -> {
                        environment.log.error(
                            buildString {
                                append("Dependency resolution failed:")
                                append(
                                    exceptions.joinToString("\n", "\n") { (key, e) ->
                                        "  - $key: ${formatError(e)}"
                                    }
                                )
                            }
                        )
                        throw DependencyInjectionException(
                            "Some dependencies could not be resolved; check logs for details"
                        )
                    }
                }
            }

            attributes.put(DependencyRegistryKey, registry)
        }
    }

private fun formatError(e: Throwable): String =
    when (e) {
        is MissingDependencyException -> "Missing declaration"
        is CircularDependencyException -> "Circular dependency: ${e.keys.joinToString(" -> ")}"
        is DuplicateDependencyException -> "Conflicting declaration"
        is AmbiguousDependencyException -> "Ambiguous dependency: ${e.keys.joinToString(", ")}"
        else -> e.message ?: ("Unknown error" + e.stackTraceToString().let { "\n$it" })
    }

private fun PluginBuilder<*>.isTestEngine(): Boolean =
    application.engine::class.simpleName == "TestApplicationEngine"


Review comment (suggestion):
Minor
```suggestion
                                exceptions.forEach { (key, e) ->
                                    append("\n  - $key: ${formatError(e)}")
                                }
```

Code after:

        with(application) {
            for (reference in configuredDependencyReferences) {
                installReference(registry, reference)
            }
            monitor.subscribe(ApplicationModulesLoaded) {
                val exceptions = mutableListOf<Pair<DependencyKey, Throwable>>()
                for ((key, source) in registry.requirements) {
                    try {
                        registry.get<Any>(key)
                    } catch (e: Throwable) {
                        environment.log.error("Cannot resolve $key\n${source.externalTrace()}")
                        exceptions += key to e
                    }
                }
                when (exceptions.size) {
                    0 -> environment.log.debug("All dependencies resolved successfully")
                    else -> {
                        environment.log.error(
                            buildString {
                                append("Dependency resolution failed:")
                                exceptions.forEach { (key, e) ->
                                    append("\n  - $key: ${formatError(e)}")
                                }
                            }
                        )
                        throw DependencyInjectionException(
                            "Some dependencies could not be resolved; check logs for details"
                        )
                    }
                }
            }

            attributes.put(DependencyRegistryKey, registry)
        }
    }

private fun formatError(e: Throwable): String =
    when (e) {
        is MissingDependencyException -> "Missing declaration"
        is CircularDependencyException -> "Circular dependency: ${e.keys.joinToString(" -> ")}"
        is DuplicateDependencyException -> "Conflicting declaration"
        is AmbiguousDependencyException -> "Ambiguous dependency: ${e.keys.joinToString(", ")}"
        else -> e.message ?: ("Unknown error" + e.stackTraceToString().let { "\n$it" })
    }

private fun PluginBuilder<*>.isTestEngine(): Boolean =
    application.engine::class.simpleName == "TestApplicationEngine"

public val DependencyRegistryKey: AttributeKey<DependencyRegistry> =
    AttributeKey<DependencyRegistry>("DependencyRegistry")