Skip to content

Commit d9912f3

Browse files
authored
Merge pull request #31 from gilgoldzweig/master
Added [NetworkResponse.Error] an interface to generalize errors
2 parents 37f9427 + 8827927 commit d9912f3

File tree

3 files changed

+40
-7
lines changed

3 files changed

+40
-7
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,16 @@ It is generic on two types: a response (`S`), and an error (`E`). The response t
8282
// or if you use suspending functions,
8383
val person = apiService.getPerson()
8484

85+
when (person) {
86+
is NetworkResponse.Success -> {
87+
// Handle successful response
88+
}
89+
is NetworkResponse.Error -> {
90+
// Handle error
91+
}
92+
}
93+
94+
// If you care about what type of error
8595
when (person) {
8696
is NetworkResponse.Success -> {
8797
// Handle successful response

src/main/kotlin/com/haroldadmin/cnradapter/NetworkResponse.kt

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,51 @@ sealed class NetworkResponse<out T : Any, out U : Any> {
1313
val code: Int
1414
) : NetworkResponse<T, Nothing>()
1515

16+
fun wasSuccessful(): Boolean = this is Success
17+
18+
/**
19+
* Describe an error without a specific type.
20+
* Makes it easier to deal with the case where you just want to know that an error occurred,
21+
* without knowing the type
22+
*
23+
* @example
24+
* val response = someNetworkAction()
25+
*
26+
* when (response) {
27+
* is NetworkResponse.Success -> // Action Succeeded do something with body
28+
*
29+
* is NetworkResponse.Error -> // Action failed do something with error
30+
* }
31+
*/
32+
interface Error {
33+
val error: Throwable
34+
}
35+
1636
/**
1737
* A request that resulted in a response with a non-2xx status code.
1838
*/
1939
data class ServerError<U : Any>(
2040
val body: U?,
2141
val code: Int,
2242
val headers: Headers? = null
23-
) : NetworkResponse<Nothing, U>()
43+
) : NetworkResponse<Nothing, U>(), Error {
44+
override val error = IOException("Network server error: $code \n$body")
45+
}
2446

2547
/**
2648
* A request that didn't result in a response.
2749
*/
28-
data class NetworkError(val error: IOException) : NetworkResponse<Nothing, Nothing>()
50+
data class NetworkError(override val error: IOException) :
51+
NetworkResponse<Nothing, Nothing>(), Error
2952

3053
/**
3154
* A request that resulted in an error different from an IO or Server error.
3255
*
3356
* An example of such an error is JSON parsing exception thrown by a serialization library.
3457
*/
3558
data class UnknownError(
36-
val error: Throwable,
59+
override val error: Throwable,
3760
val code: Int? = null,
3861
val headers: Headers? = null,
39-
) : NetworkResponse<Nothing, Nothing>()
62+
) : NetworkResponse<Nothing, Nothing>(), Error
4063
}

src/test/kotlin/com/haroldadmin/cnradapter/ErrorExtractionTest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ internal class ErrorExtractionTest: AnnotationSpec() {
2222
val serverResponse = "Server Error".toResponseBody()
2323
val response = Response.error<String>(404, serverResponse)
2424
val exception = HttpException(response)
25-
with(exception.extractFromHttpException<String>(errorConverter)) {
25+
with(exception.extractFromHttpException(errorConverter)) {
2626

2727
shouldBeTypeOf<NetworkResponse.ServerError<String>>()
28-
this as NetworkResponse.ServerError
28+
2929

3030
body shouldBe errorConverter.convert(serverResponse)
3131
code shouldBe 404
@@ -54,7 +54,7 @@ internal class ErrorExtractionTest: AnnotationSpec() {
5454
with(response) {
5555
shouldBeTypeOf<NetworkResponse.UnknownError>()
5656
this as NetworkResponse.UnknownError
57-
this.error shouldBe exception
57+
error shouldBe exception
5858
}
5959
}
6060
}

0 commit comments

Comments
 (0)