@@ -10,52 +10,56 @@ import java.lang.reflect.Type
1010 */
1111internal object ResponseHandler {
1212
13- /* *
14- * Converts the given [response] to a subclass of [NetworkResponse] based on different conditions.
15- *
16- * If the server response is successful with:
17- * => a non-empty body -> NetworkResponse.Success<S, E>
18- * => an empty body (and [successBodyType] is Unit) -> NetworkResponse.Success<Unit, E>
19- * => an empty body (and [successBodyType] is not Unit) -> NetworkResponse.ServerError<E>
20- *
21- * @param response Retrofit's response object supplied to the call adapter
22- * @param successBodyType A [Type] representing the success body
23- * @param errorConverter A retrofit converter to convert the error body into the error response type (E)
24- * @param S The success body type generic parameter
25- * @param E The error body type generic parameter
26- */
27- fun <S : Any , E : Any > handle (
28- response : Response <S >,
29- successBodyType : Type ,
30- errorConverter : Converter <ResponseBody , E >
31- ): NetworkResponse <S , E > {
32- val body = response.body()
33- val headers = response.headers()
34- val code = response.code()
35- val errorBody = response.errorBody()
13+ /* *
14+ * Converts the given [response] to a subclass of [NetworkResponse] based on different conditions.
15+ *
16+ * If the server response is successful with:
17+ * => a non-empty body -> NetworkResponse.Success<S, E>
18+ * => an empty body (and [successBodyType] is Unit) -> NetworkResponse.Success<Unit, E>
19+ * => an empty body (and [successBodyType] is not Unit) -> NetworkResponse.ServerError<E>
20+ *
21+ * @param response Retrofit's response object supplied to the call adapter
22+ * @param successBodyType A [Type] representing the success body
23+ * @param errorConverter A retrofit converter to convert the error body into the error response type (E)
24+ * @param S The success body type generic parameter
25+ * @param E The error body type generic parameter
26+ */
27+ fun <S : Any , E : Any > handle (
28+ response : Response <S >,
29+ successBodyType : Type ,
30+ errorConverter : Converter <ResponseBody , E >
31+ ): NetworkResponse <S , E > {
32+ val body = response.body()
33+ val headers = response.headers()
34+ val code = response.code()
35+ val errorBody = response.errorBody()
3636
37- return if (response.isSuccessful) {
38- if (body != null ) {
39- NetworkResponse .Success (body, headers, code)
40- } else {
41- // Special case: If the response is successful and the body is null, return a successful response
42- // if the service method declares the success body type as Unit. Otherwise, return a server error
43- if (successBodyType == Unit ::class .java) {
44- @Suppress(" UNCHECKED_CAST" )
45- NetworkResponse .Success (Unit , headers, code) as NetworkResponse <S , E >
46- } else {
47- NetworkResponse .ServerError (null , code, headers)
48- }
49- }
37+ return if (response.isSuccessful) {
38+ if (body != null ) {
39+ NetworkResponse .Success (body, headers, code)
40+ } else {
41+ // Special case: If the response is successful and the body is null, return a successful response
42+ // if the service method declares the success body type as Unit. Otherwise, return a server error
43+ if (successBodyType == Unit ::class .java) {
44+ @Suppress(" UNCHECKED_CAST" )
45+ NetworkResponse .Success (Unit , headers, code) as NetworkResponse <S , E >
5046 } else {
51- val networkResponse: NetworkResponse <S , E > = try {
52- val convertedBody = errorConverter.convert(errorBody)
53- NetworkResponse .ServerError (convertedBody, code, headers)
54- } catch (ex: Exception ) {
55- NetworkResponse .UnknownError (ex)
56- }
57- networkResponse
47+ NetworkResponse .ServerError (null , code, headers)
5848 }
49+ }
50+ } else {
51+ val networkResponse: NetworkResponse <S , E > = try {
52+ val convertedBody = if (errorBody == null ) {
53+ null
54+ } else {
55+ errorConverter.convert(errorBody)
56+ }
57+ NetworkResponse .ServerError (convertedBody, code, headers)
58+ } catch (ex: Exception ) {
59+ NetworkResponse .UnknownError (ex, code = code, headers = headers)
60+ }
61+ networkResponse
5962 }
63+ }
6064
6165}
0 commit comments