Skip to content

Commit

Permalink
MBSA-1575 Improving HttpException message
Browse files Browse the repository at this point in the history
  • Loading branch information
sboishtyan committed Oct 16, 2024
1 parent 72166c8 commit bc94abf
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
package com.avito.retrofit.adapter

public class HttpException(public val code: Int, public val body: String) : RuntimeException()
/**
* idk why we can't use [retrofit2.HttpException]
*/
public class HttpException(
code: Int,
body: String,
responseMessage: String,
) : RuntimeException("HTTP Response[ code: $code, message: $responseMessage, body: $body]")
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,21 @@ internal class ResultCall<T>(
override fun timeout(): Timeout = delegate.timeout()

private fun Response<T>.toResult(): Result<T> {
if (!isSuccessful) {
val errorBody = errorBody()?.string() ?: "empty"
return Result.Failure(HttpException(code(), errorBody))
}

body()?.let { body -> return Result.Success(body) }

// For example, in case of 204 No Content
@Suppress("UNCHECKED_CAST")
return when (successType) {
Unit::class.java -> Result.Success(Unit) as Result<T>
else -> Result.Failure(IllegalStateException("Response body was null"))
val responseMessage = message()
val code = code()

val result: Result<T> = if (!isSuccessful) {
val errorBody = errorBody()?.string() ?: "error body is null"
Result.Failure(HttpException(code, errorBody, responseMessage))
} else {
body()?.let { body -> return Result.Success(body) }
// For example, in case of 204 No Content
@Suppress("UNCHECKED_CAST")
return when (successType) {
Unit::class.java -> Result.Success(Unit) as Result<T>
else -> Result.Failure(HttpException(code, "Response body is null", responseMessage))
}
}
return result
}
}

0 comments on commit bc94abf

Please sign in to comment.