All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.dk.cachet.carp.common.infrastructure.services.ApplicationServiceRequestLogger.kt Maven / Gradle / Ivy

Go to download

Helper classes and base types relied upon by all subsystems. This library does not contain any domain logic.

The newest version!
package dk.cachet.carp.common.infrastructure.services

import dk.cachet.carp.common.application.services.ApplicationService


/**
 * Notifies of incoming requests and responses through [log],
 * as well as events preceding the request and fired as a result of the request.
 */
class ApplicationServiceRequestLogger<
    TService : ApplicationService,
    TRequest : ApplicationServiceRequest
>(
    private val eventBusLog: EventBusLog,
    private val log: (LoggedRequest) -> Unit = { },
    private val decoratee: Command
) : Command
{
    override suspend fun invoke( request: TRequest ): Any?
    {
        fun getCurrentEvents() = eventBusLog.retrieveAndEmptyLog()
        val precedingEvents = getCurrentEvents()

        @Suppress( "TooGenericExceptionCaught" )
        val response =
            try { decoratee.invoke( request ) }
            catch ( ex: Exception )
            {
                val failed = LoggedRequest.Failed(
                    request,
                    precedingEvents,
                    getCurrentEvents(),
                    ex::class.simpleName!!
                )
                log( failed )
                throw ex
            }

        log( LoggedRequest.Succeeded( request, precedingEvents, getCurrentEvents(), response ) )

        return response
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy