Skip to content

Kotlin #7 Домашнее задание [Крикунов Сергей] #46

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
21 changes: 16 additions & 5 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
}

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, E>>()

fun log(response: NetworkResponse<*, E>) {
if (response is Failure) {
Expand All @@ -42,7 +50,10 @@ fun processThrowables(logger: ErrorLogger<Throwable>) {
logger.dumpLog()
}

fun processApiErrors(apiExceptionLogger: ErrorLogger<ApiException>) {
// Type mismatch: inferred type is ErrorLogger<Throwable> but ErrorLogger<ApiException> was expected
// fix: logger экземпляр ErrorLogger<Throwable>, и на входе ожидается Throwable вместо ApiException
// ApiException наследник Throwable, разрешение принимать в том числе и наследников дает модификатор "IN"
fun processApiErrors(apiExceptionLogger: ErrorLogger<in ApiException>) {
apiExceptionLogger.log(Success("Success"))
Thread.sleep(100)
apiExceptionLogger.log(Success(Circle))
Expand Down
16 changes: 12 additions & 4 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,27 @@ import java.time.LocalDateTime
/**
* Network result
*/
sealed class NetworkResponse<T, R> {
// Type parameter T is declared as 'out' but occurs in 'invariant' position in type NetworkResponse<T, Nothing>
// fix: родитель должен иметь такой же модификатор OUT как и дочка
// Type mismatch: inferred type is Success<String> but NetworkResponse<String, kotlin.Error /* = java.lang.Error */> was expected
// fix: второй параметр так же "OUT" для обобщенного типа как ковариантного
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>()
// Success - наследник без R, заменяем R на Nothing
// Type mismatch: inferred type is Success<String> but NetworkResponse<Any, kotlin.Error /* = java.lang.Error */> was expected
// fix: Any - предок для String, используем "OUT" для обобщенного типа как ковариантного
data class Success<out T>(val resp: T): NetworkResponse<T, Nothing>()

/**
* Network error
*/
data class Failure<T, R>(val error: R): NetworkResponse<T, R>()
// Failure - наследник без T, заменяем T на Nothing
data class Failure<R>(val error: R): NetworkResponse<Nothing, R>()

val s1 = Success("Message")
val r11: NetworkResponse<String, Error> = s1
Expand All @@ -42,4 +50,4 @@ val er4: NetworkResponse<Any, Throwable> = e

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

val message = e.error.message
val message = e.error.message