package ru.fincode.tsudesk.core.network.model

import ru.fincode.tsudesk.core.common.model.AppError

sealed class NetworkError(open val meta: NetworkMeta) {

    data class NoInternet(override val meta: NetworkMeta) : NetworkError(meta)
    data class Timeout(override val meta: NetworkMeta) : NetworkError(meta)

    /**
     * EOF / unexpected end of stream / reset / обрыв соединения.
     * Это НЕ NoInternet.
     */
    data class Temporary(override val meta: NetworkMeta) : NetworkError(meta)

    data class Http(
        val code: Int,
        override val meta: NetworkMeta
    ) : NetworkError(meta)

    data class Unknown(
        val throwable: Throwable,
        override val meta: NetworkMeta
    ) : NetworkError(meta)

    fun toAppError(): AppError = when (this) {
        is NoInternet -> AppError.NoInternet
        is Timeout -> AppError.Timeout
        is Temporary -> AppError.Temporary
        is Http -> AppError.Http(code)
        is Unknown -> AppError.Unknown(throwable.message)
    }
}