1package soy.iko.opencode.data.repo
3import io.ktor.client.plugins.ClientRequestException
4import io.ktor.client.plugins.HttpRequestTimeoutException
5import io.ktor.client.plugins.RedirectResponseException
6import io.ktor.client.plugins.ResponseException
7import io.ktor.client.plugins.ServerResponseException
8import java.io.IOException
9import java.net.ConnectException
10import java.net.SocketTimeoutException
11import java.net.UnknownHostException
14 * User-facing categories for a thrown error. Extracted so the classification logic is
15 * unit-testable without an Android [android.content.Context] (the mapping from a kind
16 * to a localized string lives in [friendlyError]).
19 /** Server host could not be resolved / connection refused. */
22 /** Connect or read timed out. */
25 /** Server returned a 5xx response. */
28 /** Server returned a 4xx response (auth, not-found, etc.). */
31 /** Any other network-level I/O failure. */
34 /** Anything that isn't network-related. */
39 * Classify a throwable into an [ErrorKind] by inspecting concrete exception types rather
40 * than string-matching class names. Unwraps [cause] chains so an exception surface-wrapped
41 * by a library still classifies correctly.
43fun classifyError(t: Throwable): ErrorKind {
44 var current: Throwable? = t
45 // Walk a bounded cause chain so wrappers (e.g. an IllegalStateException around an
46 // IOException) still classify by their root.
48 while (current != null && hops < 8) {
50 is UnknownHostException, is ConnectException -> return ErrorKind.NOT_REACHABLE
51 is SocketTimeoutException, is HttpRequestTimeoutException ->
52 return ErrorKind.TIMEOUT
53 is ServerResponseException -> return ErrorKind.SERVER
54 is ClientRequestException -> return ErrorKind.CLIENT
55 is RedirectResponseException -> return ErrorKind.CLIENT
56 is IOException -> return ErrorKind.NETWORK
58 // Any other ResponseException (unrecognized status family).
59 if (current is ResponseException) return ErrorKind.SERVER
62 current = current.cause
65 return ErrorKind.UNKNOWN
68/** The HTTP status code carried by a client/server error, if any. */
69fun responseStatusCode(t: Throwable): Int? {
70 var current: Throwable? = t
72 while (current != null && hops < 8) {
73 if (current is ResponseException) return current.response.status.value
74 current = current.cause
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list