Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/improve serde error handling #645

Merged
merged 6 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 74 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ stremio-official-addons = "=2.0.12"
# (De)Serialization
serde = { version = "1", features = ["derive"]}
serde_json = "1.0.*"
serde_path_to_error = "0.1"
serde_url_params = "0.2"
serde_bencode = "0.2.*"
stremio-serde-hex = "0.1.*" # keep track of https://github.com/fspmarshall/serde-hex/pull/8
Expand Down Expand Up @@ -95,4 +96,3 @@ tokio-current-thread = "=0.2.0-alpha.1"
serde_test = "1.0"
assert_matches = "1.5"
pretty_assertions = "1"

7 changes: 2 additions & 5 deletions src/analytics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ impl<E: Env + 'static> Analytics<E> {
return Either::Left(
send_events_batch_to_api::<E>(&batch)
.map(|result| match result {
Ok(APIResult::Err { error }) if error.code != 1 => Err(()),
Ok(APIResult::Err(error)) if error.code != 1 => Err(()),
Err(EnvError::Fetch(_)) | Err(EnvError::Serde(_)) => Err(()),
_ => Ok(()),
})
Expand Down Expand Up @@ -157,10 +157,7 @@ fn send_events_batch_to_api<E: Env>(
#[cfg(debug_assertions)]
if cfg!(debug_assertions) {
E::log(format!("send_events_batch_to_api: {:#?}", &batch));
return future::ok(APIResult::Ok {
result: SuccessResponse { success: True },
})
.boxed_env();
return future::ok(APIResult::Ok(SuccessResponse { success: True })).boxed_env();
};

fetch_api::<E, _, _, _>(&APIRequest::Events {
Expand Down
16 changes: 8 additions & 8 deletions src/models/ctx/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,8 @@ fn authenticate<E: Env + 'static>(auth_request: &AuthRequest) -> Effect {
.await
.map_err(CtxError::from)
.and_then(|result| match result {
APIResult::Ok { result } => Ok(result),
APIResult::Err { error } => Err(CtxError::from(error)),
APIResult::Ok(result) => Ok(result),
APIResult::Err(error) => Err(CtxError::from(error)),
})
.map(|AuthResponse { key, user }| Auth { key, user })?;

Expand All @@ -297,8 +297,8 @@ fn authenticate<E: Env + 'static>(auth_request: &AuthRequest) -> Effect {
.await
.map_err(CtxError::from)
.and_then(|result: APIResult<CollectionResponse>| match result {
APIResult::Ok { result } => Ok(result),
APIResult::Err { error } => Err(CtxError::from(error)),
APIResult::Ok(result) => Ok(result),
APIResult::Err(error) => Err(CtxError::from(error)),
})
.map(|CollectionResponse { addons, .. }| addons)
};
Expand All @@ -320,8 +320,8 @@ fn authenticate<E: Env + 'static>(auth_request: &AuthRequest) -> Effect {
.await
.map_err(CtxError::from)
.and_then(|result| match result {
APIResult::Ok { result } => Ok(result.0),
APIResult::Err { error } => Err(CtxError::from(error)),
APIResult::Ok(result) => Ok(result.0),
APIResult::Err(error) => Err(CtxError::from(error)),
})
};

Expand Down Expand Up @@ -362,8 +362,8 @@ fn delete_session<E: Env + 'static>(auth_key: &AuthKey) -> Effect {
})
.map_err(CtxError::from)
.and_then(|result| match result {
APIResult::Ok { result } => future::ok(result),
APIResult::Err { error } => future::err(CtxError::from(error)),
APIResult::Ok(result) => future::ok(result),
APIResult::Err(error) => future::err(CtxError::from(error)),
})
.map(enclose!((auth_key) move |result| match result {
Ok(_) => Msg::Event(Event::SessionDeleted { auth_key }),
Expand Down
8 changes: 4 additions & 4 deletions src/models/ctx/update_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ fn get_modal<E: Env + 'static>() -> Effect {
fetch_api::<E, _, _, Option<GetModalResponse>>(&request)
.map_err(CtxError::from)
.and_then(|result| match result {
APIResult::Ok { result } => future::ok(result),
APIResult::Err { error } => future::err(CtxError::from(error)),
APIResult::Ok(result) => future::ok(result),
APIResult::Err(error) => future::err(CtxError::from(error)),
})
.map(move |result| Msg::Internal(Internal::GetModalResult(request, result)))
.boxed_env(),
Expand All @@ -126,8 +126,8 @@ fn get_notification<E: Env + 'static>() -> Effect {
fetch_api::<E, _, _, Option<GetNotificationResponse>>(&request)
.map_err(CtxError::from)
.and_then(|result| match result {
APIResult::Ok { result } => future::ok(result),
APIResult::Err { error } => future::err(CtxError::from(error)),
APIResult::Ok(result) => future::ok(result),
APIResult::Err(error) => future::err(CtxError::from(error)),
})
.map(move |result| Msg::Internal(Internal::GetNotificationResult(request, result)))
.boxed_env(),
Expand Down
12 changes: 6 additions & 6 deletions src/models/ctx/update_library.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,8 +400,8 @@ fn push_items_to_api<E: Env + 'static>(items: Vec<LibraryItem>, auth_key: &AuthK
})
.map_err(CtxError::from)
.and_then(|result| match result {
APIResult::Ok { result } => future::ok(result),
APIResult::Err { error } => future::err(CtxError::from(error)),
APIResult::Ok(result) => future::ok(result),
APIResult::Err(error) => future::err(CtxError::from(error)),
})
.map(move |result| match result {
Ok(_) => Msg::Event(Event::LibraryItemsPushedToAPI { ids }),
Expand All @@ -425,8 +425,8 @@ fn pull_items_from_api<E: Env + 'static>(ids: Vec<String>, auth_key: &AuthKey) -
fetch_api::<E, _, _, LibraryItemsResponse>(&request)
.map_err(CtxError::from)
.and_then(|result| match result {
APIResult::Ok { result } => future::ok(result.0),
APIResult::Err { error } => future::err(CtxError::from(error)),
APIResult::Ok(result) => future::ok(result.0),
APIResult::Err(error) => future::err(CtxError::from(error)),
})
.map(move |result| Msg::Internal(Internal::LibraryPullResult(request, result)))
.boxed_env(),
Expand Down Expand Up @@ -454,8 +454,8 @@ fn plan_sync_with_api<E: Env + 'static>(library: &LibraryBucket, auth_key: &Auth
fetch_api::<E, _, _, Vec<LibraryItemModified>>(&request)
.map_err(CtxError::from)
.and_then(|result| match result {
APIResult::Ok { result } => future::ok(result),
APIResult::Err { error } => future::err(CtxError::from(error)),
APIResult::Ok(result) => future::ok(result),
APIResult::Err(error) => future::err(CtxError::from(error)),
})
.map_ok(|remote_mtimes| {
remote_mtimes
Expand Down
16 changes: 8 additions & 8 deletions src/models/ctx/update_profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,8 +400,8 @@ fn push_addons_to_api<E: Env + 'static>(addons: Vec<Descriptor>, auth_key: &Auth
fetch_api::<E, _, _, SuccessResponse>(&request)
.map_err(CtxError::from)
.and_then(|result| match result {
APIResult::Ok { result } => future::ok(result),
APIResult::Err { error } => future::err(CtxError::from(error)),
APIResult::Ok(result) => future::ok(result),
APIResult::Err(error) => future::err(CtxError::from(error)),
})
.map(move |result| match result {
Ok(_) => Msg::Event(Event::AddonsPushedToAPI { transport_urls }),
Expand All @@ -423,8 +423,8 @@ fn pull_user_from_api<E: Env + 'static>(auth_key: &AuthKey) -> Effect {
fetch_api::<E, _, _, _>(&request)
.map_err(CtxError::from)
.and_then(|result| match result {
APIResult::Ok { result } => future::ok(result),
APIResult::Err { error } => future::err(CtxError::from(error)),
APIResult::Ok(result) => future::ok(result),
APIResult::Err(error) => future::err(CtxError::from(error)),
})
.map(move |result| Msg::Internal(Internal::UserAPIResult(request, result)))
.boxed_env(),
Expand All @@ -442,8 +442,8 @@ fn push_user_to_api<E: Env + 'static>(user: User, auth_key: &AuthKey) -> Effect
fetch_api::<E, _, _, SuccessResponse>(&request)
.map_err(CtxError::from)
.and_then(|result| match result {
APIResult::Ok { result } => future::ok(result),
APIResult::Err { error } => future::err(CtxError::from(error)),
APIResult::Ok(result) => future::ok(result),
APIResult::Err(error) => future::err(CtxError::from(error)),
})
.map(move |result| match result {
Ok(_) => Msg::Event(Event::UserPushedToAPI { uid }),
Expand All @@ -466,8 +466,8 @@ fn pull_addons_from_api<E: Env + 'static>(auth_key: &AuthKey) -> Effect {
fetch_api::<E, _, _, _>(&request)
.map_err(CtxError::from)
.and_then(|result| match result {
APIResult::Ok { result } => future::ok(result),
APIResult::Err { error } => future::err(CtxError::from(error)),
APIResult::Ok(result) => future::ok(result),
APIResult::Err(error) => future::err(CtxError::from(error)),
})
.map_ok(|CollectionResponse { addons, .. }| addons)
.map(move |result| Msg::Internal(Internal::AddonsAPIResult(request, result)))
Expand Down
4 changes: 2 additions & 2 deletions src/models/data_export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ fn export_data_from_api<E: Env + 'static>(auth_key: AuthKey) -> Effect {
fetch_api::<E, _, _, DataExportResponse>(&api_request)
.map_err(CtxError::from)
.and_then(|result| match result {
APIResult::Ok { result } => future::ok(result),
APIResult::Err { error } => future::err(CtxError::from(error)),
APIResult::Ok(result) => future::ok(result),
APIResult::Err(error) => future::err(CtxError::from(error)),
})
.map(move |result| Msg::Internal(Internal::DataExportResult(auth_key, result)))
.boxed_env(),
Expand Down
8 changes: 4 additions & 4 deletions src/models/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ fn create_code<E: Env + 'static>() -> Effect {
fetch_api::<E, _, _, LinkCodeResponse>(&LinkRequest::Create)
.map_err(LinkError::from)
.and_then(|result| match result {
APIResult::Ok { result } => future::ok(result),
APIResult::Err { error } => future::err(LinkError::from(error)),
APIResult::Ok(result) => future::ok(result),
APIResult::Err(error) => future::err(LinkError::from(error)),
})
.map(|result| Msg::Internal(Internal::LinkCodeResult(result)))
.boxed_env(),
Expand All @@ -133,8 +133,8 @@ fn read_data<E: Env + 'static>(code: &str) -> Effect {
})
.map_err(LinkError::from)
.and_then(|result| match result {
APIResult::Ok { result } => future::ok(result),
APIResult::Err { error } => future::err(LinkError::from(error)),
APIResult::Ok(result) => future::ok(result),
APIResult::Err(error) => future::err(LinkError::from(error)),
})
.map(enclose!((code.to_owned() => code) move |result| {
Msg::Internal(Internal::LinkDataResult(code, result))
Expand Down
Loading