Skip to content

Commit a2bf233

Browse files
authored
KTOR-5915 Warn when the RateLimit plugin installed after the routing (#3615)
1 parent c3d1f48 commit a2bf233

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

ktor-server/ktor-server-plugins/ktor-server-auth/jvmAndNix/src/io/ktor/server/auth/AuthenticationInterceptors.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,10 @@ private fun AuthenticationConfig.findProviders(
171171
private fun AuthenticationConfig.findProvider(configurationName: String?): AuthenticationProvider {
172172
return providers[configurationName] ?: throw IllegalArgumentException(
173173
if (configurationName == null) {
174-
"Default authentication configuration was not found"
174+
"Default authentication configuration was not found. "
175175
} else {
176-
"Authentication configuration with the name $configurationName was not found"
177-
}
176+
"Authentication configuration with the name $configurationName was not found. "
177+
} + "Make sure that you install Authentication plugin before you use it in Routing"
178178
)
179179
}
180180

ktor-server/ktor-server-plugins/ktor-server-rate-limit/jvmAndNix/src/io/ktor/server/plugins/ratelimit/RateLimitInterceptors.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ internal val RateLimitApplicationInterceptors = createApplicationPlugin(
3535
private fun PluginBuilder<RateLimitInterceptorsConfig>.rateLimiterPluginBuilder() {
3636
val configs = application.attributes.getOrNull(RateLimiterConfigsRegistryKey) ?: emptyMap()
3737
val providers = pluginConfig.providerNames.map { name ->
38-
configs[name] ?: throw IllegalStateException("Rate limit provider with name $name is not configured")
38+
configs[name] ?: throw IllegalStateException(
39+
"Rate limit provider with name $name is not configured. " +
40+
"Make sure that you install RateLimit plugin before you use it in Routing"
41+
)
3942
}
4043
val registry = application.attributes.computeIfAbsent(RateLimiterInstancesRegistryKey) { ConcurrentMap() }
4144
val clearOnRefillJobs = ConcurrentMap<ProviderKey, Job>()

ktor-server/ktor-server-tests/jvmAndNix/test/io/ktor/tests/server/plugins/RateLimitTest.kt

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,11 @@ class RateLimitTest {
541541
val error = assertFailsWith<IllegalStateException> {
542542
startApplication()
543543
}
544-
assertEquals("Rate limit provider with name RateLimitName(name=other_name) is not configured", error.message)
544+
assertEquals(
545+
"Rate limit provider with name RateLimitName(name=other_name) is not configured. " +
546+
"Make sure that you install RateLimit plugin before you use it in Routing",
547+
error.message
548+
)
545549
}
546550

547551
@Test
@@ -563,7 +567,8 @@ class RateLimitTest {
563567
startApplication()
564568
}
565569
assertEquals(
566-
"Rate limit provider with name RateLimitName(name=KTOR_NO_NAME_RATE_LIMITER) is not configured",
570+
"Rate limit provider with name RateLimitName(name=KTOR_NO_NAME_RATE_LIMITER) is not configured. " +
571+
"Make sure that you install RateLimit plugin before you use it in Routing",
567572
error.message
568573
)
569574
}
@@ -586,7 +591,11 @@ class RateLimitTest {
586591
val error = assertFailsWith<IllegalStateException> {
587592
startApplication()
588593
}
589-
assertEquals("Rate limit provider with name RateLimitName(name=name) is not configured", error.message)
594+
assertEquals(
595+
"Rate limit provider with name RateLimitName(name=name) is not configured. " +
596+
"Make sure that you install RateLimit plugin before you use it in Routing",
597+
error.message
598+
)
590599
}
591600

592601
@Test

0 commit comments

Comments
 (0)