- 
                Notifications
    
You must be signed in to change notification settings  - Fork 341
 
feat(WidgetDriver): Pass Matrix API errors to the widget #4241
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
Changes from 11 commits
cccf7a0
              6b3af4e
              69b980f
              6828ffa
              0a5b7d3
              b120aa8
              90fb0e9
              a7d0d7e
              2d3f2d4
              c2d676e
              e62e013
              1416ff0
              88ee196
              5ec845a
              4176d1c
              6a340f4
              92e9bb3
              File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| 
          
            
          
           | 
    @@ -12,11 +12,10 @@ | |
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| 
     | 
||
| use std::fmt; | ||
| 
     | 
||
| use ruma::{ | ||
| api::client::delayed_events::{ | ||
| delayed_message_event, delayed_state_event, update_delayed_event, | ||
| api::client::{ | ||
| delayed_events::{delayed_message_event, delayed_state_event, update_delayed_event}, | ||
| error::{ErrorBody, StandardErrorBody}, | ||
| }, | ||
| events::{AnyTimelineEvent, MessageLikeEventType, StateEventType}, | ||
| serde::Raw, | ||
| 
        
          
        
         | 
    @@ -25,7 +24,7 @@ use ruma::{ | |
| use serde::{Deserialize, Serialize}; | ||
| 
     | 
||
| use super::{SendEventRequest, UpdateDelayedEventRequest}; | ||
| use crate::widget::StateKeySelector; | ||
| use crate::{widget::StateKeySelector, Error, HttpError}; | ||
| 
     | 
||
| #[derive(Deserialize, Debug)] | ||
| #[serde(tag = "action", rename_all = "snake_case", content = "data")] | ||
| 
        
          
        
         | 
    @@ -41,28 +40,83 @@ pub(super) enum FromWidgetRequest { | |
| DelayedEventUpdate(UpdateDelayedEventRequest), | ||
| } | ||
| 
     | 
||
| /// The full response a client sends to a from widget request | ||
| /// in case of an error. | ||
| #[derive(Serialize)] | ||
| pub(super) struct FromWidgetErrorResponse { | ||
| error: FromWidgetError, | ||
| } | ||
| 
     | 
||
| impl FromWidgetErrorResponse { | ||
| pub(super) fn new(e: impl fmt::Display) -> Self { | ||
| Self { error: FromWidgetError { message: e.to_string() } } | ||
| /// Create a error response to send to the widget from an http error. | ||
| pub(crate) fn from_http_error(error: &HttpError) -> Self { | ||
| Self { | ||
| error: FromWidgetError { | ||
| message: error.to_string(), | ||
| matrix_api_error: error.as_client_api_error().and_then(|api_error| match api_error | ||
| .body | ||
| .clone() | ||
| { | ||
| ErrorBody::Standard { kind, message } => Some(FromWidgetMatrixErrorBody { | ||
| http_status: api_error.status_code.as_u16().into(), | ||
| response: StandardErrorBody { kind, message }, | ||
| }), | ||
| _ => None, | ||
| }), | ||
| }, | ||
| } | ||
| } | ||
| 
     | 
||
| /// Create a error response to send to the widget from a matrix sdk error. | ||
| pub(crate) fn from_error(error: &Error) -> Self { | ||
                
       | 
||
| match &error { | ||
| Error::Http(e) => FromWidgetErrorResponse::from_http_error(e), | ||
| // For UnknownError's we do not want to have the `unknown error` bit in the message. | ||
| // Hence we only convert the inner error to a string. | ||
| Error::UnknownError(e) => FromWidgetErrorResponse::from_string(e.to_string()), | ||
| _ => FromWidgetErrorResponse::from_string(error.to_string()), | ||
                
      
                  toger5 marked this conversation as resolved.
               
          
            Show resolved
            Hide resolved
         | 
||
| } | ||
| } | ||
| 
     | 
||
| /// Create a error response to send to the widget from a string. | ||
| pub(crate) fn from_string<S: Into<String>>(error: S) -> Self { | ||
| Self { error: FromWidgetError { message: error.into(), matrix_api_error: None } } | ||
| } | ||
| } | ||
| 
     | 
||
| /// The serializable section of an error response send by the client as a | ||
| /// response to a [`FromWidgetRequest`]. | ||
| #[derive(Serialize)] | ||
| struct FromWidgetError { | ||
| /// A unspecified error message text that caused this widget action to fail. | ||
| /// This is useful to prompt the user on an issue but cannot be used to | ||
| /// decide on how to deal with the error. | ||
| message: String, | ||
| /// An optional matrix error that contains specified | ||
| /// information and helps finding a work around for specific errors. | ||
| matrix_api_error: Option<FromWidgetMatrixErrorBody>, | ||
                
      
                  toger5 marked this conversation as resolved.
               
          
            Show resolved
            Hide resolved
         | 
||
| } | ||
| 
     | 
||
| /// The serializable section of a widget response that represents a matrix | ||
| /// error. | ||
| #[derive(Serialize)] | ||
| struct FromWidgetMatrixErrorBody { | ||
| /// The status code of the http response | ||
| http_status: u32, | ||
| /// The matrix standard error response including the `errorcode` and the | ||
| /// `error` message as defined in the spec: https://spec.matrix.org/v1.12/client-server-api/#standard-error-response | ||
| response: StandardErrorBody, | ||
| } | ||
                
      
                  toger5 marked this conversation as resolved.
               
          
            Show resolved
            Hide resolved
         | 
||
| 
     | 
||
| /// The serializable section of a widget response containing the supported | ||
| /// versions. | ||
| #[derive(Serialize)] | ||
| pub(super) struct SupportedApiVersionsResponse { | ||
| supported_versions: Vec<ApiVersion>, | ||
| } | ||
| 
     | 
||
| impl SupportedApiVersionsResponse { | ||
| /// The currently supported widget api versions from the rust widget driver. | ||
| pub(super) fn new() -> Self { | ||
| Self { | ||
| supported_versions: vec![ | ||
| 
          
            
          
           | 
    ||
Uh oh!
There was an error while loading. Please reload this page.