Skip to content

Commit

Permalink
Add a new HttpError.UnknownError type instead of throwing an exception
Browse files Browse the repository at this point in the history
  • Loading branch information
jayohms committed Feb 28, 2024
1 parent b75dd26 commit cc4cde3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
7 changes: 6 additions & 1 deletion turbo/src/main/kotlin/dev/hotwire/turbo/errors/HttpError.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -148,7 +153,7 @@ sealed interface HttpError : TurboVisitError {
?: ServerError.Other(statusCode, reasonPhrase)
}

throw IllegalArgumentException("Invalid HTTP error status code: $statusCode")
return UnknownError(statusCode, reasonPhrase)
}
}
}
14 changes: 14 additions & 0 deletions turbo/src/test/kotlin/dev/hotwire/turbo/errors/HttpErrorTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}

0 comments on commit cc4cde3

Please sign in to comment.