You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
4. UnknownError: Represents every other kind of error which can not be classified as an API error or a network problem (eg JSON deserialization exceptions)
3. UnknownError: Any other errors, like serialization exceptions
21
23
22
-
It is generic on two types: a response (`S`), and an error (`E`). The response type is your Java/Kotlin representation of the API response, while the error type represents the error response sent by the API.
24
+
It is generic on two types: a success response (`S`), and an error response (`E`).
25
+
26
+
-`S`: Kotlin representation of a successful API response
27
+
-`E`: Representation of an unsuccessful API response
23
28
24
29
## Usage
25
30
26
-
- Suppose you have an API that returns the following response if the request is successful:
27
-
28
-
_Successful Response_
29
-
30
-
```json
31
-
{
32
-
"name": "John doe",
33
-
"age": 21
34
-
}
35
-
```
36
-
37
-
- And here's the response when the request was unsuccessful:
38
-
39
-
_Error Response_
40
-
41
-
```json
42
-
{
43
-
"message": "The requested person was not found"
44
-
}
45
-
```
46
-
47
-
- You can create two data classes to model the these responses:
48
-
49
-
```kotlin
50
-
data classPersonResponse(valname:String, valage:Int)
- You can also use the included utility function `executeWithRetry` to automatically retry your network requests if they result in `NetworkResponse.NetworkError`
113
-
114
-
```kotlin
115
-
suspendfungetPerson() {
116
-
val response = executeWithRetry(times =5) {
117
-
apiService.getPerson.await()
118
-
}
119
-
120
-
// or with suspending functions
121
-
val response = executeWithRetry(times =5) {
122
-
apiService.getPerson()
123
-
}
124
-
125
-
// Then handle the response
126
-
}
127
-
```
128
-
129
-
- There's also an overloaded invoke operator on the NetworkResponse class which returns the success body if the request was successful, or null otherwise
130
-
131
-
```kotlin
132
-
val usersResponse = usersRepo.getUsers().await()
133
-
println(usersResponse() ?:"No users were found")
134
-
```
135
-
136
-
- Some API responses convey information through headers only, and contain empty bodies. Such endpoints must be used with `Unit` as their success type.
Modelling errors as a part of your state is a recommended practice. This library helps you deal with scenarios where you can successfully recover from errors, and extract meaningful information from them too!
150
+
This library helps you deal with scenarios where you can successfully recover from errors, and extract meaningful
151
+
information from them too!
148
152
149
-
-`NetworkResponseAdapter` provides a much cleaner solution than Retrofit's built in `Call` type for dealing with errors.`Call` throws an exception on any kind of an error, leaving it up to you to catch it and parse it manually to figure out what went wrong. `NetworkResponseAdapter` does all of that for you and returns the result in an easily consumable `NetworkResponse` subtype.
153
+
-`NetworkResponseAdapter` provides a much cleaner solution than Retrofit's built in `Call` type for dealing with
154
+
errors.`Call` throws an exception on any kind of error, leaving it up to you to catch it and parse it manually to
155
+
figure out what went wrong. `NetworkResponseAdapter` does all of that for you and returns the result in an easily
156
+
consumable `NetworkResponse` type.
150
157
151
-
- The RxJava retrofit adapter treats non 2xx response codes as errors, which seems silly in the context of Rx where errors terminate streams. Also, just like the `Call<T>` type, it makes you deal with all types of errors in an `onError` callback, where you have to manually parse it to find out exactly what went wrong.
158
+
- The RxJava retrofit adapter treats non 2xx response codes as errors, which seems silly in the context of Rx where
159
+
errors terminate streams. Also, just like the `Call<T>` type, it makes you deal with all types of errors in
160
+
an `onError` callback, where you have to manually parse it to find out exactly what went wrong.
152
161
153
162
- Using the `Response` class provided by Retrofit is cumbersome, as you have to manually parse error bodies with it.
154
163
@@ -158,17 +167,17 @@ Add the Jitpack repository to your list of repositories:
0 commit comments