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(WidgetDriver): Pass Matrix API errors to the widget #4241

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

toger5
Copy link
Contributor

@toger5 toger5 commented Nov 10, 2024

Currently the WidgetDriver just returns unspecified error strings to the widget that can be used to display an issue description to the user. It is not helpful to run code like a retry or other error mitigation logic.

Here it is proposed to add standardized errors for issues that every widget driver implementation can run into (all matrix cs api errors): matrix-org/matrix-spec-proposals#2762 (comment)

This PR forwards the errors that occur during the widget processing to the widget in the correct format.

NOTE:
It does not include request Url and http Headers. See also: matrix-org/matrix-spec-proposals#2762 (comment)

  • Public API changes documented in changelogs (optional)

Signed-off-by:

@toger5 toger5 force-pushed the toger5/widget-driver-matrix-api-errors branch 2 times, most recently from 1b1a0fb to 0dd249f Compare November 13, 2024 09:01
Copy link

codecov bot commented Nov 13, 2024

Codecov Report

Attention: Patch coverage is 81.25000% with 9 lines in your changes missing coverage. Please review.

Project coverage is 84.94%. Comparing base (b8a61cf) to head (6b3af4e).
Report is 36 commits behind head on main.

Files with missing lines Patch % Lines
crates/matrix-sdk/src/widget/machine/mod.rs 86.20% 4 Missing ⚠️
crates/matrix-sdk/src/widget/matrix.rs 33.33% 2 Missing ⚠️
crates/matrix-sdk/src/widget/mod.rs 60.00% 2 Missing ⚠️
...rates/matrix-sdk/src/widget/machine/from_widget.rs 90.90% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4241      +/-   ##
==========================================
+ Coverage   84.87%   84.94%   +0.06%     
==========================================
  Files         274      274              
  Lines       29719    29735      +16     
==========================================
+ Hits        25225    25257      +32     
+ Misses       4494     4478      -16     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@toger5 toger5 force-pushed the toger5/widget-driver-matrix-api-errors branch from 0dd249f to cccf7a0 Compare November 13, 2024 09:18
@toger5 toger5 marked this pull request as ready for review November 13, 2024 17:09
@toger5 toger5 requested a review from a team as a code owner November 13, 2024 17:09
@toger5 toger5 requested review from bnjbvr and removed request for a team November 13, 2024 17:09
Copy link
Member

@bnjbvr bnjbvr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense! But we need many more comments for this, please; let's try to add them as we touch these files.

@@ -76,7 +76,7 @@ where

pub(crate) fn then(
self,
response_handler: impl FnOnce(Result<T, String>, &mut WidgetMachine) -> Vec<Action>
response_handler: impl FnOnce(Result<T, crate::Error>, &mut WidgetMachine) -> Vec<Action>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you're around, can you add a doc comment to this function please?

}
}

#[derive(Serialize)]
struct FromWidgetError {
message: String,
matrix_api_error: Option<FromWidgetMatrixErrorBody>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This crate is barely documented, which makes it super hard to understand how it works. Let's change that; of course I wouldn't have you change all of it at once, so let's try to do it incrementally. The doc comments don't have to be long; a single short descriptive line is sufficient to give us a rough idea of why/how it's used.

Please add a doc comment to:

  • every single struct/enum you're adding fields to (here, FromWidgetError)
  • every field/variant from those structs/enums (here, message above)
  • every new struct/enum/field/variant

http_headers: HashMap<String, String>,
url: String,
response: Value,
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: always add a newline after a block closing

Suggested change
}
}

Comment on lines +101 to +106
struct FromWidgetMatrixErrorBody {
http_status: u32,
// TODO: figure out the which type to use here.
http_headers: HashMap<String, String>,
url: String,
response: Value,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want doc comments everywhere here too please :)

@@ -38,7 +38,7 @@ pub(crate) enum IncomingMessage {
request_id: Uuid,

/// The result of the request: response data or error message.
response: Result<MatrixDriverResponse, String>,
response: Result<MatrixDriverResponse, crate::Error>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The doc comment above needs an update^^

@@ -118,7 +116,7 @@ impl MatrixDriver {
state_key: Option<String>,
content: Box<RawJsonValue>,
delayed_event_parameters: Option<delayed_events::DelayParameters>,
) -> Result<SendEventResponse> {
) -> Result<SendEventResponse, Error> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto here

@@ -54,17 +52,17 @@ impl MatrixDriver {
}

/// Requests an OpenID token for the current user.
pub(crate) async fn get_open_id(&self) -> HttpResult<OpenIdResponse> {
pub(crate) async fn get_open_id(&self) -> Result<OpenIdResponse, Error> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to specify the error type here neither

@@ -167,9 +165,9 @@ impl MatrixDriver {
&self,
delay_id: String,
action: UpdateAction,
) -> HttpResult<delayed_events::update_delayed_event::unstable::Response> {
) -> Result<delayed_events::update_delayed_event::unstable::Response, Error> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: no need to specify the Error here too

@@ -195,7 +195,7 @@ impl<T: CapabilitiesProvider> ProcessingContext<T> {
self.to_widget_tx.send(msg).await.map_err(|_| ())?;
}
Action::MatrixDriverRequest { request_id, data } => {
let response = match data {
let response: Result<MatrixDriverResponse, Error> = match data {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this explicit type declaration needed?

.await;

Mock::given(method("PUT"))
.and(path_regex(r"^/_matrix/client/v3/rooms/.*/send/m.room.message/.*$"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a mock in the MatrixMockServer called .mock_room_send(), can you use that instead?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants