From 8978b56750290ea928371cf395e20b41ef85724a Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Sun, 16 Mar 2025 16:54:21 -0700 Subject: [PATCH] Fix fetchUrl handling of HTTP errors with html content types --- src/main/kotlin/net/thauvin/erik/jokeapi/JokeUtil.kt | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/net/thauvin/erik/jokeapi/JokeUtil.kt b/src/main/kotlin/net/thauvin/erik/jokeapi/JokeUtil.kt index 460211c..9bd8ac1 100644 --- a/src/main/kotlin/net/thauvin/erik/jokeapi/JokeUtil.kt +++ b/src/main/kotlin/net/thauvin/erik/jokeapi/JokeUtil.kt @@ -59,9 +59,10 @@ internal fun fetchUrl(url: String, auth: String = ""): JokeResponse { connection.setRequestProperty("Authentication", auth) } - val stream = if (connection.responseCode in 200..399) connection.inputStream else connection.errorStream + val isSuccess = connection.responseCode in 200..399 + val stream = if (isSuccess) connection.inputStream else connection.errorStream val body = stream.bufferedReader().use { it.readText() } - if (body.isBlank()) { + if (!isSuccess && (body.isBlank() || connection.contentType.contains("text/html"))) { throw httpError(connection.responseCode) } else if (JokeApi.logger.isLoggable(Level.FINE)) { JokeApi.logger.fine(body)