From faf841824d5bb17a42a4dc456a89984f42b50ef5 Mon Sep 17 00:00:00 2001 From: dece Date: Mon, 13 Dec 2021 16:20:31 +0100 Subject: [PATCH] MainActivity: show user-friendly error messages --- .../dev/lowrespalmtree/comet/MainActivity.kt | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/app/src/main/java/dev/lowrespalmtree/comet/MainActivity.kt b/app/src/main/java/dev/lowrespalmtree/comet/MainActivity.kt index 437528f..81e933c 100644 --- a/app/src/main/java/dev/lowrespalmtree/comet/MainActivity.kt +++ b/app/src/main/java/dev/lowrespalmtree/comet/MainActivity.kt @@ -205,6 +205,7 @@ class MainActivity : AppCompatActivity(), ContentAdapter.ContentAdapterListen { when (response.code.getCategory()) { Response.Code.Category.SUCCESS -> handleRequestSuccess(response, uri) Response.Code.Category.REDIRECT -> handleRedirect(response, redirects = redirects + 1) + Response.Code.Category.SERVER_ERROR -> handleError(response) else -> signalError("Can't handle code ${response.code}.") } } @@ -245,6 +246,22 @@ class MainActivity : AppCompatActivity(), ContentAdapter.ContentAdapterListen { private fun handleRedirect(response: Response, redirects: Int) { event.postValue(RedirectEvent(response.meta, redirects)) } + + private fun handleError(response: Response) { + event.postValue(FailureEvent(when (response.code) { + Response.Code.TEMPORARY_FAILURE -> "40: the server encountered a temporary failure." + Response.Code.SERVER_UNAVAILABLE -> "41: the server is currently unavailable." + Response.Code.CGI_ERROR -> "42: a CGI script encountered an error." + Response.Code.PROXY_ERROR -> "43: the server failed to proxy the request." + Response.Code.SLOW_DOWN -> "44: slow down!" + Response.Code.PERMANENT_FAILURE -> "50: this request failed and similar requests will likely fail as well." + Response.Code.NOT_FOUND -> "51: this page can't be found." + Response.Code.GONE -> "52: this page is gone." + Response.Code.PROXY_REQUEST_REFUSED -> "53: the server refused to proxy the request." + Response.Code.BAD_REQUEST -> "59: bad request." + else -> "${response.code}: unknown error code." + })) + } } companion object {