Skip to content

Commit 376327e

Browse files
committed
Catch feature enable/disable errors
1 parent 3964f89 commit 376327e

File tree

1 file changed

+27
-4
lines changed
  • core/src/main/kotlin/io/github/rothes/esu/core/module

1 file changed

+27
-4
lines changed

core/src/main/kotlin/io/github/rothes/esu/core/module/Feature.kt

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.github.rothes.esu.core.module
22

3+
import io.github.rothes.esu.core.EsuBootstrap
34
import io.github.rothes.esu.core.configuration.ConfigurationPart
45
import io.github.rothes.esu.core.configuration.MultiLangConfiguration
56
import io.github.rothes.esu.core.configuration.data.MessageData
@@ -33,15 +34,14 @@ interface Feature<C: ConfigurationPart, L: ConfigurationPart> {
3334
val available = isAvailable()
3435

3536
if (available.value && !enabled) {
36-
onEnable()
37-
setEnabled(true)
37+
enableInternal()
3838
} else if (!available.value && enabled) {
39-
setEnabled(false)
40-
onDisable()
39+
disableInternal()
4140
}
4241

4342
return available
4443
}
44+
4545
fun getFeatureMap(): Map<String, Feature<*, *>>
4646
fun getFeatures(): List<Feature<*, *>>
4747
fun getFeature(name: String): Feature<*, *>?
@@ -88,4 +88,27 @@ interface Feature<C: ConfigurationPart, L: ConfigurationPart> {
8888
}
8989
}
9090

91+
private companion object {
92+
93+
private fun Feature<*, *>.enableInternal() {
94+
try {
95+
onEnable()
96+
setEnabled(true)
97+
} catch (e: Throwable) {
98+
EsuBootstrap.instance.err("An exception occurred while enabling $name", e)
99+
disableInternal()
100+
}
101+
}
102+
103+
private fun Feature<*, *>.disableInternal() {
104+
try {
105+
setEnabled(false)
106+
onDisable()
107+
} catch (e: Throwable) {
108+
EsuBootstrap.instance.err("An exception occurred while disabling $name", e)
109+
}
110+
}
111+
112+
}
113+
91114
}

0 commit comments

Comments
 (0)