Skip to content

Commit b68e345

Browse files
authored
Localized FetchError and CollectionError (#1130)
* Localized FetchError and CollectionError * Remove an unused variable * Generate LocalizedError implementation for wp_mobile types * Include SQL error in the error message
1 parent bebb4a1 commit b68e345

6 files changed

Lines changed: 50 additions & 14 deletions

File tree

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/swift-bindings.sh

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,14 @@ cargo run --release --quiet --bin wp_uniffi_bindgen generate \
2020
--no-format \
2121
--language swift
2222

23-
function patch_wp_api {
24-
error_types=$(grep -r "impl WpSupportsLocalization for" wp_api/src | grep -o "for [A-Za-z0-9]*" | cut -d' ' -f2)
23+
function generate_localized_error_extension {
24+
local package_name=$1
25+
local swift_binding=$2
26+
27+
error_types=$(grep -r "impl WpSupportsLocalization for" "${package_name}/src" | grep -o "for [A-Za-z0-9]*" | cut -d' ' -f2)
2528

2629
for error_type in $error_types; do
27-
cat <<EOF >> "$1"
30+
cat <<EOF >> "$swift_binding"
2831
2932
extension $error_type: LocalizedError {
3033
public var errorDescription: String? {
@@ -34,19 +37,23 @@ extension $error_type: LocalizedError {
3437
}
3538
EOF
3639
done
40+
}
41+
42+
function patch_wp_api {
43+
local swift_binding=$1
44+
generate_localized_error_extension wp_api "$swift_binding"
3745

3846
# Use sed to replace `import SQLite3` with the wrapped version
3947
sed -i.bak 's/^import SQLite3$/#if canImport(SQLite3)\
4048
import SQLite3\
41-
#endif/' $swift_binding
49+
#endif/' "$swift_binding"
4250
}
4351

44-
for swift_binding in "$output_dir"/*.swift; do
45-
options=("-i")
46-
if [[ $(uname) == "Darwin" ]]; then
47-
options+=("")
48-
fi
52+
function patch_wp_mobile {
53+
generate_localized_error_extension wp_mobile "$1"
54+
}
4955

56+
for swift_binding in "$output_dir"/*.swift; do
5057
basename=$(basename "$swift_binding" .swift)
5158
if [ "$(type -t "patch_$basename")" = "function" ]; then
5259
"patch_$basename" "$swift_binding"

wp_localization/localization/en-US/main.ftl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,5 @@ parse_api_root_failure_reason_server_fatal_error = Your server encountered an un
7272
parse_api_root_failure_reason_wordfence_blocking_access = Wordfence is blocking access to the site's API. Please check your Wordfence configuration.
7373
7474
already_logged_in = You are already logged in as {$username}.
75+
76+
database_generic_message = There was a problem loading your data: {$reason}.

wp_mobile/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ thiserror = { workspace = true }
1919
uniffi = { workspace = true }
2020
url = { workspace = true }
2121
wp_api = { path = "../wp_api" }
22+
wp_localization = { path = "../wp_localization" }
23+
wp_localization_macro = { path = "../wp_localization_macro" }
2224
wp_mobile_cache = { path = "../wp_mobile_cache" }
2325

2426
[dev-dependencies]

wp_mobile/src/collection/collection_error.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
use wp_localization::{MessageBundle, WpMessages, WpSupportsLocalization};
2+
use wp_localization_macro::WpDeriveLocalizable;
3+
14
/// Errors that can occur during collection operations
2-
#[derive(Debug, thiserror::Error, uniffi::Error)]
5+
#[derive(Debug, thiserror::Error, uniffi::Error, WpDeriveLocalizable)]
36
pub enum CollectionError {
4-
#[error("Database error: {err_message}")]
57
DatabaseError { err_message: String },
68
}
79

@@ -12,3 +14,13 @@ impl From<wp_mobile_cache::SqliteDbError> for CollectionError {
1214
}
1315
}
1416
}
17+
18+
impl WpSupportsLocalization for CollectionError {
19+
fn message_bundle(&self) -> MessageBundle<'_> {
20+
match self {
21+
CollectionError::DatabaseError { err_message } => {
22+
WpMessages::database_generic_message(err_message)
23+
}
24+
}
25+
}
26+
}

wp_mobile/src/collection/fetch_error.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
use wp_api::prelude::WpApiError;
2+
use wp_localization::{MessageBundle, WpMessages, WpSupportsLocalization};
3+
use wp_localization_macro::WpDeriveLocalizable;
24
use wp_mobile_cache::SqliteDbError;
35

46
/// Errors that can occur during network fetch operations
57
///
68
/// This combines API/network errors from wp_api with database errors
79
/// from cache operations.
8-
#[derive(Debug, thiserror::Error, uniffi::Error)]
10+
#[derive(Debug, thiserror::Error, uniffi::Error, WpDeriveLocalizable)]
911
pub enum FetchError {
1012
/// API or network error from wp_api
11-
#[error(transparent)]
1213
Api(WpApiError),
1314

1415
/// Database error during cache upsert
15-
#[error("Database error: {err_message}")]
1616
Database { err_message: String },
1717
}
1818

@@ -37,3 +37,14 @@ impl From<crate::service::WpServiceError> for FetchError {
3737
}
3838
}
3939
}
40+
41+
impl WpSupportsLocalization for FetchError {
42+
fn message_bundle(&self) -> MessageBundle<'_> {
43+
match self {
44+
FetchError::Api(api_err) => api_err.message_bundle(),
45+
FetchError::Database { err_message } => {
46+
WpMessages::database_generic_message(err_message)
47+
}
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)