Skip to content

Commit 7e8facd

Browse files
committed
Improve error handling
Signed-off-by: Dmitry Sulman <[email protected]>
1 parent e999fb7 commit 7e8facd

File tree

2 files changed

+79
-30
lines changed

2 files changed

+79
-30
lines changed

logback-access-reactor-netty/src/main/kotlin/io/github/dmitrysulman/logback/access/reactor/netty/ReactorNettyAccessLogFactory.kt

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -140,37 +140,43 @@ class ReactorNettyAccessLogFactory : AccessLogFactory {
140140
addStatus(InfoStatus("Done configuring", this::class.java.simpleName))
141141
} catch (e: Exception) {
142142
addStatus(ErrorStatus("Failed to configure ReactorNettyAccessLogFactory", this::class.java.simpleName, e))
143+
throw e
143144
}
144145
if (!debug) {
145146
StatusPrinter2().printInCaseOfErrorsOrWarnings(accessContext)
146147
}
147148
}
148149

149150
private fun getDefaultConfig(): URL? {
150-
return try {
151-
val fileNameFromSystemProperty =
152-
System.getProperty(CONFIG_FILE_NAME_PROPERTY)?.also {
153-
addStatus(
154-
InfoStatus(
155-
"Found system property [$CONFIG_FILE_NAME_PROPERTY] value: [$it]",
156-
this::class.java.simpleName,
157-
),
158-
)
159-
}
160-
val fileName =
161-
fileNameFromSystemProperty ?: run {
162-
addStatus(
163-
InfoStatus(
164-
"No system property [$CONFIG_FILE_NAME_PROPERTY] provided, checking [$DEFAULT_CONFIG_FILE_NAME]",
165-
this::class.java.simpleName,
166-
),
167-
)
168-
DEFAULT_CONFIG_FILE_NAME
169-
}
170-
getConfigFromFileName(fileName)
171-
} catch (e: FileNotFoundException) {
172-
addStatus(WarnStatus(e.message, this::class.java.simpleName))
173-
return null
151+
val fileNameFromSystemProperty =
152+
System.getProperty(CONFIG_FILE_NAME_PROPERTY)?.also {
153+
addStatus(
154+
InfoStatus(
155+
"Found system property [$CONFIG_FILE_NAME_PROPERTY] value: [$it]",
156+
this::class.java.simpleName,
157+
),
158+
)
159+
}
160+
return if (fileNameFromSystemProperty != null) {
161+
try {
162+
getConfigFromFileName(fileNameFromSystemProperty)
163+
} catch (e: FileNotFoundException) {
164+
addStatus(ErrorStatus(e.message, this::class.java.simpleName))
165+
throw e
166+
}
167+
} else {
168+
addStatus(
169+
InfoStatus(
170+
"No system property [$CONFIG_FILE_NAME_PROPERTY] provided, checking [$DEFAULT_CONFIG_FILE_NAME]",
171+
this::class.java.simpleName,
172+
),
173+
)
174+
try {
175+
getConfigFromFileName(DEFAULT_CONFIG_FILE_NAME)
176+
} catch (e: FileNotFoundException) {
177+
addStatus(WarnStatus(e.message, this::class.java.simpleName))
178+
null
179+
}
174180
}
175181
}
176182

logback-access-reactor-netty/src/test/kotlin/io/github/dmitrysulman/logback/access/reactor/netty/ReactorNettyAccessLogFactoryTests.kt

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
package io.github.dmitrysulman.logback.access.reactor.netty
22

33
import ch.qos.logback.access.common.joran.JoranConfigurator
4+
import ch.qos.logback.core.joran.spi.JoranException
45
import ch.qos.logback.core.status.OnConsoleStatusListener
6+
import ch.qos.logback.core.status.Status
57
import io.github.dmitrysulman.logback.access.reactor.netty.ReactorNettyAccessLogFactory.Companion.CONFIG_FILE_NAME_PROPERTY
8+
import io.github.dmitrysulman.logback.access.reactor.netty.ReactorNettyAccessLogFactory.Companion.DEFAULT_CONFIG_FILE_NAME
69
import io.github.dmitrysulman.logback.access.reactor.netty.integration.EventCaptureAppender
710
import io.kotest.assertions.throwables.shouldThrowExactly
8-
import io.kotest.matchers.booleans.shouldBeFalse
911
import io.kotest.matchers.booleans.shouldBeTrue
12+
import io.kotest.matchers.nulls.shouldBeNull
1013
import io.kotest.matchers.nulls.shouldNotBeNull
1114
import io.kotest.matchers.types.shouldBeTypeOf
15+
import io.mockk.every
16+
import io.mockk.mockk
17+
import io.mockk.spyk
18+
import io.mockk.verify
1219
import org.junit.jupiter.api.Test
1320
import java.io.FileNotFoundException
21+
import java.net.URL
1422

1523
class ReactorNettyAccessLogFactoryTests {
1624
@Test
@@ -96,18 +104,53 @@ class ReactorNettyAccessLogFactoryTests {
96104
}.shouldBeTrue()
97105
}
98106

107+
@Test
108+
fun `test not existing file url from constructor parameter`() {
109+
shouldThrowExactly<JoranException> { ReactorNettyAccessLogFactory(URL("file:logback-access-not-exist.xml")) }
110+
}
111+
99112
@Test
100113
fun `test not existing filename from configuration property`() {
101114
System.setProperty(CONFIG_FILE_NAME_PROPERTY, "logback-access-not-exist.xml")
102-
val reactorNettyAccessLogFactory = ReactorNettyAccessLogFactory()
103-
reactorNettyAccessLogFactory.accessContext
104-
.iteratorForAppenders()
105-
.hasNext()
106-
.shouldBeFalse()
115+
shouldThrowExactly<FileNotFoundException> { ReactorNettyAccessLogFactory() }
107116
}
108117

109118
@Test
110119
fun `test not existing filename from constructor parameter`() {
111120
shouldThrowExactly<FileNotFoundException> { ReactorNettyAccessLogFactory("logback-access-not-exist.xml") }
112121
}
122+
123+
@Test
124+
fun `test not existing default config file`() {
125+
val reactorNettyAccessLogFactory = spyk<ReactorNettyAccessLogFactory>(recordPrivateCalls = true)
126+
every { reactorNettyAccessLogFactory["getConfigFromFileName"](DEFAULT_CONFIG_FILE_NAME) } throws FileNotFoundException()
127+
val getDefaultConfigMethod = reactorNettyAccessLogFactory::class.java.getDeclaredMethod("getDefaultConfig")
128+
getDefaultConfigMethod.trySetAccessible()
129+
val defaultConfigUrl = getDefaultConfigMethod.invoke(reactorNettyAccessLogFactory) as URL?
130+
defaultConfigUrl.shouldBeNull()
131+
}
132+
133+
@Test
134+
fun `test initialization without config`() {
135+
val reactorNettyAccessLogFactory = ReactorNettyAccessLogFactory()
136+
val joranConfigurator = mockk<JoranConfigurator>(relaxed = true)
137+
val initializeMethod =
138+
reactorNettyAccessLogFactory::class.java.getDeclaredMethod(
139+
"initialize",
140+
URL::class.java,
141+
JoranConfigurator::class.java,
142+
Boolean::class.java,
143+
)
144+
initializeMethod.trySetAccessible()
145+
initializeMethod.invoke(reactorNettyAccessLogFactory, null, joranConfigurator, false)
146+
147+
verify(exactly = 0) { joranConfigurator.context }
148+
verify(exactly = 0) { joranConfigurator.doConfigure(any<URL>()) }
149+
150+
reactorNettyAccessLogFactory.accessContext.statusManager.copyOfStatusList
151+
.any {
152+
it.effectiveLevel == Status.WARN &&
153+
it.message == "No configuration file provided, skipping configuration"
154+
}.shouldBeTrue()
155+
}
113156
}

0 commit comments

Comments
 (0)