From cc4cde3dd2ebe58b4c044bbfea6a901143006485 Mon Sep 17 00:00:00 2001 From: Jay Ohms Date: Wed, 28 Feb 2024 10:10:32 -0500 Subject: [PATCH] Add a new HttpError.UnknownError type instead of throwing an exception --- .../kotlin/dev/hotwire/turbo/errors/HttpError.kt | 7 ++++++- .../dev/hotwire/turbo/errors/HttpErrorTest.kt | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/turbo/src/main/kotlin/dev/hotwire/turbo/errors/HttpError.kt b/turbo/src/main/kotlin/dev/hotwire/turbo/errors/HttpError.kt index dcc7b1b5..1d56d669 100644 --- a/turbo/src/main/kotlin/dev/hotwire/turbo/errors/HttpError.kt +++ b/turbo/src/main/kotlin/dev/hotwire/turbo/errors/HttpError.kt @@ -124,6 +124,11 @@ sealed interface HttpError : TurboVisitError { ) : ServerError } + data class UnknownError( + override val statusCode: Int, + override val reasonPhrase: String? + ) : HttpError + companion object { fun from(errorResponse: WebResourceResponse): HttpError { return getError(errorResponse.statusCode, errorResponse.reasonPhrase) @@ -148,7 +153,7 @@ sealed interface HttpError : TurboVisitError { ?: ServerError.Other(statusCode, reasonPhrase) } - throw IllegalArgumentException("Invalid HTTP error status code: $statusCode") + return UnknownError(statusCode, reasonPhrase) } } } diff --git a/turbo/src/test/kotlin/dev/hotwire/turbo/errors/HttpErrorTest.kt b/turbo/src/test/kotlin/dev/hotwire/turbo/errors/HttpErrorTest.kt index 7741a647..48b680aa 100644 --- a/turbo/src/test/kotlin/dev/hotwire/turbo/errors/HttpErrorTest.kt +++ b/turbo/src/test/kotlin/dev/hotwire/turbo/errors/HttpErrorTest.kt @@ -59,4 +59,18 @@ class HttpErrorTest : BaseUnitTest() { assertThat(error.statusCode).isEqualTo(it.first) } } + + @Test + fun unknownErrors() { + val errors = listOf( + 399 to HttpError.UnknownError(399, null), + 600 to HttpError.UnknownError(600, null) + ) + + errors.forEach { + val error = HttpError.from(it.first) + assertThat(error).isEqualTo(it.second) + assertThat(error.statusCode).isEqualTo(it.first) + } + } }