Skip to content

Commit

Permalink
Add message to HttpError; require not null errorBody
Browse files Browse the repository at this point in the history
Call string() on non nullable errorBody

Use !! on errorBody
  • Loading branch information
lukaszkalnik committed Jan 21, 2022
1 parent 1a3a9cd commit 02a347c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
Expand Up @@ -29,14 +29,16 @@ public abstract class arrow/retrofit/adapter/either/networkhandling/CallError {
}

public final class arrow/retrofit/adapter/either/networkhandling/HttpError : arrow/retrofit/adapter/either/networkhandling/CallError {
public fun <init> (ILjava/lang/String;)V
public fun <init> (ILjava/lang/String;Ljava/lang/String;)V
public final fun component1 ()I
public final fun component2 ()Ljava/lang/String;
public final fun copy (ILjava/lang/String;)Larrow/retrofit/adapter/either/networkhandling/HttpError;
public static synthetic fun copy$default (Larrow/retrofit/adapter/either/networkhandling/HttpError;ILjava/lang/String;ILjava/lang/Object;)Larrow/retrofit/adapter/either/networkhandling/HttpError;
public final fun component3 ()Ljava/lang/String;
public final fun copy (ILjava/lang/String;Ljava/lang/String;)Larrow/retrofit/adapter/either/networkhandling/HttpError;
public static synthetic fun copy$default (Larrow/retrofit/adapter/either/networkhandling/HttpError;ILjava/lang/String;Ljava/lang/String;ILjava/lang/Object;)Larrow/retrofit/adapter/either/networkhandling/HttpError;
public fun equals (Ljava/lang/Object;)Z
public final fun getBody ()Ljava/lang/String;
public final fun getCode ()I
public final fun getMessage ()Ljava/lang/String;
public fun hashCode ()I
public fun toString ()Ljava/lang/String;
}
Expand Down
Expand Up @@ -10,7 +10,11 @@ public sealed class CallError
/**
* Http request returned an error response.
*/
public data class HttpError(val code: Int, val body: String) : CallError()
public data class HttpError(
val code: Int,
val message: String,
val body: String
) : CallError()

/**
* IO error: no network, socket timeout etc. Check the [cause] for details.
Expand Down
Expand Up @@ -36,8 +36,12 @@ private class EitherCall<R>(
private fun Response<R>.toEither(): Either<CallError, R> {
// Http error response (4xx - 5xx)
if (!isSuccessful) {
val errorBody = errorBody()?.string() ?: ""
return HttpError(code(), errorBody).left()
val errorBody = errorBody()!!.string()
return HttpError(
code = code(),
message = message(),
body = errorBody
).left()
}

// Http success response with body
Expand Down
Expand Up @@ -61,7 +61,11 @@ private fun networkEitherCallAdapterTests(

val body = service!!.getEither()

body shouldBe HttpError(code = 400, body = """{"errorCode":666}""").left()
body shouldBe HttpError(
code = 400,
message = "Client Error",
body = """{"errorCode":666}"""
).left()
}

"should return CallError for 200 with invalid JSON" {
Expand All @@ -78,7 +82,11 @@ private fun networkEitherCallAdapterTests(

val body = service!!.getEither()

body shouldBe HttpError(code = 400, body = """not a valid JSON""").left()
body shouldBe HttpError(
code = 400,
message = "Client Error",
body = """not a valid JSON"""
).left()
}

"should return IOError when server disconnects" {
Expand Down

0 comments on commit 02a347c

Please sign in to comment.