-
Notifications
You must be signed in to change notification settings - Fork 60
/
NetworkResponse.kt
61 lines (55 loc) · 1.8 KB
/
NetworkResponse.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package com.haroldadmin.cnradapter
import okhttp3.Headers
import java.io.IOException
sealed class NetworkResponse<out T : Any, out U : Any> {
/**
* A request that resulted in a response with a 2xx status code that has a body.
*/
data class Success<T : Any>(
val body: T,
val headers: Headers? = null,
val code: Int
) : NetworkResponse<T, Nothing>()
/**
* Describe an error without a specific type.
* Makes it easier to deal with the case where you just want to know that an error occurred,
* without knowing the type
*
* @example
* val response = someNetworkAction()
*
* when (response) {
* is NetworkResponse.Success -> // Action Succeeded do something with body
*
* is NetworkResponse.Error -> // Action failed do something with error
* }
*/
interface Error {
val error: Throwable
}
/**
* A request that resulted in a response with a non-2xx status code.
*/
data class ServerError<U : Any>(
val body: U?,
val code: Int,
val headers: Headers? = null
) : NetworkResponse<Nothing, U>(), Error {
override val error = IOException("Network server error: $code \n$body")
}
/**
* A request that didn't result in a response.
*/
data class NetworkError(override val error: IOException) :
NetworkResponse<Nothing, Nothing>(), Error
/**
* A request that resulted in an error different from an IO or Server error.
*
* An example of such an error is JSON parsing exception thrown by a serialization library.
*/
data class UnknownError(
override val error: Throwable,
val code: Int? = null,
val headers: Headers? = null,
) : NetworkResponse<Nothing, Nothing>(), Error
}