package dp.kotul.starter

import dp.kotul.starter.filter.CorrelationHeaderAndLoggingFilter
import kotlinx.coroutines.CoroutineExceptionHandler
import org.slf4j.LoggerFactory
import kotlin.coroutines.CoroutineContext

class LoggingCoroutineExceptionHandler(private val correlationId: String) : CoroutineExceptionHandler {

    private val log = LoggerFactory.getLogger(LoggingCoroutineExceptionHandler::class.java)

    override val key: CoroutineContext.Key<*>
        get() = CoroutineExceptionHandler.Key

    override fun handleException(context: CoroutineContext, exception: Throwable) {
        CorrelationHeaderAndLoggingFilter.setCorrelationId(correlationId)
        log.error("LoggingCoroutineExceptionHandler ", exception)
    }
}
