Soft error handling
This commit is contained in:
1 parent
73f5a3d4da
commit
ad54625980
2 files changed
+42
-3
No files matched your search
@@ -1,5 +1,6 @@
|
||||
package no.iktdev.eventi.events
|
||||
|
||||
import mu.KotlinLogging
|
||||
import no.iktdev.eventi.models.DeleteEvent
|
||||
import no.iktdev.eventi.models.Event
|
||||
import no.iktdev.eventi.models.SignalEvent
|
||||
@@ -8,6 +9,8 @@ import java.util.UUID
|
||||
|
||||
open class EventDispatcher(val eventStore: EventStore) {
|
||||
|
||||
private val log = KotlinLogging.logger {}
|
||||
|
||||
open fun dispatch(referenceId: UUID, events: List<Event>) {
|
||||
val derivedFromIds = events.mapNotNull { it.metadata.derivedFromId }.flatten().toSet()
|
||||
val deletedEventIds = events.filterIsInstance<DeleteEvent>().map { it.deletedEventId }
|
||||
@@ -23,9 +26,13 @@ open class EventDispatcher(val eventStore: EventStore) {
|
||||
|
||||
EventListenerRegistry.getListeners().forEach { listener ->
|
||||
for (candidate in candidates) {
|
||||
val result = listener.onEvent(candidate, effectiveHistory)
|
||||
if (result != null) {
|
||||
eventStore.persist(result)
|
||||
try {
|
||||
val result = listener.onEvent(candidate, effectiveHistory)
|
||||
if (result != null) {
|
||||
eventStore.persist(result)
|
||||
}
|
||||
} catch (e: SoftDispatchException) {
|
||||
log.warn( "Soft-dispatch in ${listener::class.simpleName} " + "for event ${e.eventType?.simpleName ?: "Unknown"}: ${e.message}" )
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
package no.iktdev.eventi.events
|
||||
|
||||
import no.iktdev.eventi.models.Event
|
||||
|
||||
sealed class SoftDispatchException(
|
||||
message: String,
|
||||
val eventType: Class<out Event>? = null
|
||||
) : RuntimeException(message) {
|
||||
|
||||
class MissingEventException(
|
||||
missingType: Class<out Event>
|
||||
) : SoftDispatchException(
|
||||
message = "Missing required event: ${missingType.simpleName}",
|
||||
eventType = missingType
|
||||
)
|
||||
|
||||
class SkipListenerException(
|
||||
reason: String,
|
||||
currentEvent: Class<out Event>
|
||||
) : SoftDispatchException(
|
||||
message = "Listener skipped: $reason",
|
||||
eventType = currentEvent
|
||||
)
|
||||
|
||||
class ForcedListenerEjectionException(
|
||||
reason: String,
|
||||
currentEvent: Class<out Event>
|
||||
) : SoftDispatchException(
|
||||
message = "Listener forcibly ejected: $reason",
|
||||
eventType = currentEvent
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user