diff options
Diffstat (limited to 'okhttp-handler/src/main')
-rw-r--r-- | okhttp-handler/src/main/scala/com/softwaremill/sttp/okhttp/OkHttpClientHandler.scala | 14 |
1 files changed, 12 insertions, 2 deletions
diff --git a/okhttp-handler/src/main/scala/com/softwaremill/sttp/okhttp/OkHttpClientHandler.scala b/okhttp-handler/src/main/scala/com/softwaremill/sttp/okhttp/OkHttpClientHandler.scala index b95bf8a..af44132 100644 --- a/okhttp-handler/src/main/scala/com/softwaremill/sttp/okhttp/OkHttpClientHandler.scala +++ b/okhttp-handler/src/main/scala/com/softwaremill/sttp/okhttp/OkHttpClientHandler.scala @@ -86,7 +86,15 @@ abstract class OkHttpHandler[R[_], S](client: OkHttpClient) res: OkHttpResponse, responseAs: ResponseAs[T, S]): R[Response[T]] = { - val body = responseHandler(res).handle(responseAs, responseMonad) + val code = res.code() + + val body = if (codeIsSuccess(code)) { + responseMonad.map(responseHandler(res).handle(responseAs, responseMonad), + Right(_: T)) + } else { + responseMonad.map(responseHandler(res).handle(asString, responseMonad), + Left(_: String)) + } val headers = res .headers() @@ -94,7 +102,9 @@ abstract class OkHttpHandler[R[_], S](client: OkHttpClient) .asScala .flatMap(name => res.headers().values(name).asScala.map((name, _))) - responseMonad.map(body, Response(_: T, res.code(), headers.toList)) + responseMonad.map( + body, + Response(_: Either[String, T], res.code(), headers.toList)) } private def responseHandler(res: OkHttpResponse) = |