Skip to content

homework Kotlin-7 #35

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 2 commits 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
33 changes: 24 additions & 9 deletions src/main/kotlin/ru/kotlin/homework/network/NetworkLogger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,36 @@ 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
}

data object UnknownException : ApiException("Unknown exception") {
private fun readResolve(): Any = UnknownException
}
}

class ErrorLogger<E : Throwable> {

val errors = mutableListOf<Pair<LocalDateTime, E>>()
private val errors = mutableListOf<Pair<LocalDateTime, *>>()

fun log(response: NetworkResponse<*, E>) {
fun log(response: NetworkResponse<*, *>) {
if (response is Failure) {
errors.add(response.responseDateTime to response.error)
}
}

fun dump(): List<Pair<LocalDateTime, *>> {
return errors
}

fun dumpLog() {
errors.forEach { (date, error) ->
println("Error at $date: ${error.message}")
println("Error at $date: $error")
}
}
}
Expand All @@ -38,17 +50,15 @@ fun processThrowables(logger: ErrorLogger<Throwable>) {
logger.log(Success(Circle))
Thread.sleep(100)
logger.log(Failure(IllegalArgumentException("Something unexpected")))

logger.dumpLog()
}

fun processApiErrors(apiExceptionLogger: ErrorLogger<ApiException>) {
fun processApiErrors(apiExceptionLogger: ErrorLogger<*>) {
apiExceptionLogger.log(Success("Success"))
Thread.sleep(100)
apiExceptionLogger.log(Success(Circle))
Thread.sleep(100)
apiExceptionLogger.log(Failure(ApiException.NetworkException))

apiExceptionLogger.dumpLog()
}

Expand All @@ -60,5 +70,10 @@ fun main() {

println("Processing Api:")
processApiErrors(logger)

val errors = logger.dump()
errors.forEach {
println("List Errors = $it")
}
}

26 changes: 13 additions & 13 deletions src/main/kotlin/ru/kotlin/homework/network/NetworkResponse.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

package ru.kotlin.homework.network

import java.lang.Exception
import java.lang.IllegalArgumentException
import java.time.LocalDateTime

Expand All @@ -16,30 +15,31 @@ sealed class NetworkResponse<T, R> {
/**
* Network success
*/
data class Success<T, R>(val resp: T): NetworkResponse<T, R>()
data class Success<T>(val resp: T) : NetworkResponse<T, Nothing>()

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

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

val r11: Success<String> = s1
val r12: Success<String> = s1

val s2 = Success("Message")
val r21: NetworkResponse<String, Exception> = s2
val r22: NetworkResponse<Any, Exception> = s2
val r21: Success<String> = s2
val r22: Success<String> = s2

val s3 = Success(String())
val r31: Success<CharSequence> = s3
val r32: Success<Any> = s3
val r31: Success<String> = s3
val r32: Success<String> = s3

val e = Failure(Error())
val er1: NetworkResponse<String, Error> = e
val er2: NetworkResponse<Any, Error> = e
val er4: NetworkResponse<Any, Throwable> = e
val er1: Failure<Error> = e
val er2: Failure<Error> = e
val er4: Failure<Error> = e

val er5: NetworkResponse<Int, Throwable> = Failure(IllegalArgumentException("message"))
val er5: Failure<IllegalArgumentException> = Failure(IllegalArgumentException("message"))

val message = e.error.message