Fix fetchUrl handling of HTTP errors with html content types

This commit is contained in:
Erik C. Thauvin 2025-03-16 16:54:21 -07:00
parent 4cc92e956f
commit 8978b56750
Signed by: erik
GPG key ID: 776702A6A2DA330E

View file

@ -59,9 +59,10 @@ internal fun fetchUrl(url: String, auth: String = ""): JokeResponse {
connection.setRequestProperty("Authentication", auth) 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() } val body = stream.bufferedReader().use { it.readText() }
if (body.isBlank()) { if (!isSuccess && (body.isBlank() || connection.contentType.contains("text/html"))) {
throw httpError(connection.responseCode) throw httpError(connection.responseCode)
} else if (JokeApi.logger.isLoggable(Level.FINE)) { } else if (JokeApi.logger.isLoggable(Level.FINE)) {
JokeApi.logger.fine(body) JokeApi.logger.fine(body)