Skip to content

Commit c64fc8d

Browse files
committed
feat: Bundle raw response with UnknownError when available
1 parent 5964a94 commit c64fc8d

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,27 @@ public sealed interface NetworkResponse<S, E> {
123123
* (e.g. Serialization errors)
124124
*/
125125
public data class UnknownError<S, E>(
126-
public override val error: Throwable
126+
public override val error: Throwable,
127+
public val response: Response<*>?
127128
) : Error<S, E> {
128129
/**
129130
* Always `null` for an [UnknownError]
130131
*/
131132
override val body: E? = null
133+
134+
/**
135+
* The status code returned by the server.
136+
*
137+
* Alias for [Response.code] of the original response
138+
*/
139+
public val code: Int? = response?.code()
140+
141+
/**
142+
* The headers returned by the server.
143+
*
144+
* Alias for [Response.headers] of the original response
145+
*/
146+
public val headers: Headers? = response?.headers()
132147
}
133148
}
134149

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ private fun <S, E> parseUnsuccessfulResponse(
4848
val convertedBody = errorConverter.convert(errorBody)
4949
NetworkResponse.ServerError(convertedBody, response)
5050
} catch (error: Throwable) {
51-
NetworkResponse.UnknownError(error)
51+
NetworkResponse.UnknownError(error, response)
5252
}
5353
}
5454

@@ -98,6 +98,6 @@ internal fun <S, E> Throwable.asNetworkResponse(
9898
response.asNetworkResponse(successType, errorConverter) as NetworkResponse<S, E>
9999
}
100100
}
101-
else -> NetworkResponse.UnknownError(this)
101+
else -> NetworkResponse.UnknownError(this, null)
102102
}
103103
}

0 commit comments

Comments
 (0)