Skip to content

Выполнил домашнее задание Kotlin-7. #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions src/main/kotlin/ru/kotlin/homework/network/NetworkLogger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,22 @@ import java.time.LocalDateTime
* Известный вам список ошибок
*/
sealed class ApiException(message: String) : Throwable(message) {
data object NotAuthorized : ApiException("Not authorized")
data object NetworkException : ApiException("Not connected")
data object UnknownException: ApiException("Unknown exception")
}
data object NotAuthorized : ApiException("Not authorized") {
private fun readResolve(): Any = NotAuthorized
}

data object NetworkException : ApiException("Not connected") {
private fun readResolve(): Any = NetworkException
}

class ErrorLogger<E : Throwable> {
data object UnknownException: ApiException("Unknown exception") {
private fun readResolve(): Any = UnknownException
}
}

val errors = mutableListOf<Pair<LocalDateTime, E>>()
class ErrorLogger<E: Throwable>
{
private val errors = mutableListOf<Pair<LocalDateTime, E>>()

fun log(response: NetworkResponse<*, E>) {
if (response is Failure) {
Expand All @@ -30,6 +38,9 @@ class ErrorLogger<E : Throwable> {
println("Error at $date: ${error.message}")
}
}

// Метод dump для получения списка накопленных ошибок
fun dump(): List<Pair<LocalDateTime, E>> = errors
}

fun processThrowables(logger: ErrorLogger<Throwable>) {
Expand All @@ -42,7 +53,8 @@ fun processThrowables(logger: ErrorLogger<Throwable>) {
logger.dumpLog()
}

fun processApiErrors(apiExceptionLogger: ErrorLogger<ApiException>) {
fun processApiErrors(apiExceptionLogger: ErrorLogger<in ApiException>) {

apiExceptionLogger.log(Success("Success"))
Thread.sleep(100)
apiExceptionLogger.log(Success(Circle))
Expand Down
6 changes: 3 additions & 3 deletions src/main/kotlin/ru/kotlin/homework/network/NetworkResponse.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ import java.time.LocalDateTime
/**
* Network result
*/
sealed class NetworkResponse<T, R> {
sealed class NetworkResponse<out T, out R> {
val responseDateTime: LocalDateTime = LocalDateTime.now()
}

/**
* Network success
*/
data class Success<T, R>(val resp: T): NetworkResponse<T, R>()
data class Success<out T>(val resp: T): NetworkResponse<T, Nothing>()

/**
* Network error
*/
data class Failure<T, R>(val error: R): NetworkResponse<T, R>()
data class Failure<out R>(val error: R): NetworkResponse<Nothing, R>()

val s1 = Success("Message")
val r11: NetworkResponse<String, Error> = s1
Expand Down