Skip to content

Commit 8c8edc3

Browse files
jkmasseloguzkocer
andauthored
Improve error messaging for some connection errors (#700)
* Improve error messaging for some connection errors * Properly handle Kotlin `NoRouteToHostException` * Update wp_localization/localization/tr-TR/main.ftl Co-authored-by: Oguz Kocer <[email protected]> --------- Co-authored-by: Oguz Kocer <[email protected]>
1 parent bc429d4 commit 8c8edc3

File tree

4 files changed

+25
-6
lines changed

4 files changed

+25
-6
lines changed

native/kotlin/api/kotlin/src/main/kotlin/rs/wordpress/api/kotlin/WpRequestExecutor.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import uniffi.wp_api.WpNetworkRequest
2222
import uniffi.wp_api.WpNetworkResponse
2323
import uniffi.wp_api.parseCertificate
2424
import java.io.File
25+
import java.net.NoRouteToHostException
2526
import java.net.UnknownHostException
2627
import javax.net.ssl.HttpsURLConnection
2728
import javax.net.ssl.SSLPeerUnverifiedException
@@ -67,6 +68,8 @@ class WpRequestExecutor(
6768
)
6869
} catch (e: UnknownHostException) {
6970
throw requestExecutionFailedWith(RequestExecutionErrorReason.unknownHost(e))
71+
} catch (e: NoRouteToHostException) {
72+
throw requestExecutionFailedWith(RequestExecutionErrorReason.noRouteToHost(e))
7073
}
7174
}
7275

@@ -122,6 +125,11 @@ private fun RequestExecutionErrorReason.Companion.unknownHost(e: UnknownHostExce
122125
suggestedAction = "Check that the URL is valid and try again"
123126
)
124127

128+
private fun RequestExecutionErrorReason.Companion.noRouteToHost(e: NoRouteToHostException) =
129+
RequestExecutionErrorReason.HttpError(
130+
reason = e.localizedMessage
131+
)
132+
125133
@Suppress("UNUSED_PARAMETER")
126134
private fun RequestExecutionErrorReason.Companion.invalidSSLError(
127135
e: SSLPeerUnverifiedException, // To avoid `SwallowedException` from Detekt

wp_api/src/api_error.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,9 @@ pub enum RequestExecutionErrorReason {
497497
DeviceIsOfflineError {
498498
error_message: String,
499499
},
500+
HttpError {
501+
reason: String,
502+
},
500503
GenericError {
501504
error_message: String,
502505
},
@@ -577,6 +580,9 @@ impl WpSupportsLocalization for RequestExecutionErrorReason {
577580
RequestExecutionErrorReason::DeviceIsOfflineError { error_message } => {
578581
WpMessages::just(error_message)
579582
}
583+
RequestExecutionErrorReason::HttpError { reason } => {
584+
WpMessages::http_server_error(reason)
585+
}
580586
RequestExecutionErrorReason::GenericError { error_message } => {
581587
WpMessages::just(error_message)
582588
}

wp_api/src/reqwest_request_executor.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -285,12 +285,15 @@ impl From<&ResolveError> for RequestExecutionErrorReason {
285285
impl From<&HyperError> for RequestExecutionErrorReason {
286286
fn from(error: &HyperError) -> Self {
287287
if let Some(http2_error) = error.find::<Http2Error>() {
288-
// TODO: We can probably handle more cases here, such as:
289-
// - Connection reset
290-
291-
return RequestExecutionErrorReason::GenericError {
292-
error_message: http2_error.to_string(),
293-
};
288+
if let Some(reason) = http2_error.reason() {
289+
return RequestExecutionErrorReason::HttpError {
290+
reason: reason.description().to_string(),
291+
};
292+
} else {
293+
return RequestExecutionErrorReason::GenericError {
294+
error_message: http2_error.to_string(),
295+
};
296+
}
294297
}
295298

296299
if error.is_closed() {

wp_localization/localization/en-US/main.ftl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ http_timeout_error = The connection timed out
2525
2626
http_authentication_rejected_error = The server at {$url} rejected your credentials. Please provide a valid username and password.
2727
28+
http_server_error = Unable to connect to server: {$reason}. Please contact your server provider.
29+
2830
misconfigured_http_authentication_error = The server is sending invalid HTTP authentication information. Please check your site's HTTP authentication configuration.
2931
3032
misconfigured_rate_limit_error = The server is rate limiting requests in a way that will never succeed. Please check your site's rate limit configuration.

0 commit comments

Comments
 (0)