Commit 1e9b6cc271

1e9b6cc2718782fb8fad88f45e5f296fac8f29bf

parent: 46e081b1e4

Unregistered key

Ulyssa <git@ulyssa.dev> · 2024-03-24 02:09 UTC

Provide better error message for M_UNKNOWN_TOKEN (#101)

Layout: unified · split

src/main.rs +23 −8
@@ -28,6 +28,7 @@ use std::time::{Duration, Instant};
2828
2929use clap::Parser;
3030use matrix_sdk::crypto::encrypt_room_key_export;
31use matrix_sdk::ruma::api::client::error::ErrorKind;
3132use matrix_sdk::ruma::OwnedUserId;
3233use modalkit::keybindings::InputBindings;
3334use rand::{distributions::Alphanumeric, Rng};
@@ -745,7 +746,7 @@ async fn login(worker: &Requester, settings: &ApplicationSettings) -> IambResult
745746}
746747
747748fn print_exit<T: Display, N>(v: T) -> N {
748 println!("{v}");
749 eprintln!("{v}");
749750 process::exit(2);
750751}
751752
@@ -847,7 +848,9 @@ async fn login_upgrade(
847848 }
848849
849850 println!("* Syncing...");
850 worker::do_first_sync(&worker.client, store).await;
851 worker::do_first_sync(&worker.client, store)
852 .await
853 .map_err(IambError::from)?;
851854
852855 Ok(())
853856}
@@ -859,7 +862,9 @@ async fn login_normal(
859862) -> IambResult<()> {
860863 println!("* Logging in for {}...", settings.profile.user_id);
861864 login(worker, settings).await?;
862 worker::do_first_sync(&worker.client, store).await;
865 worker::do_first_sync(&worker.client, store)
866 .await
867 .map_err(IambError::from)?;
863868 Ok(())
864869}
865870
@@ -878,12 +883,22 @@ async fn run(settings: ApplicationSettings) -> IambResult<()> {
878883 let store = Arc::new(AsyncMutex::new(store));
879884 worker.init(store.clone());
880885
881 if let Some((keydir, pass)) = import_keys {
882 login_upgrade(keydir, pass, &worker, &settings, &store)
883 .await
884 .unwrap_or_else(print_exit);
886 let res = if let Some((keydir, pass)) = import_keys {
887 login_upgrade(keydir, pass, &worker, &settings, &store).await
885888 } else {
886 login_normal(&worker, &settings, &store).await.unwrap_or_else(print_exit);
889 login_normal(&worker, &settings, &store).await
890 };
891
892 match res {
893 Err(UIError::Application(IambError::Matrix(e))) => {
894 if let Some(ErrorKind::UnknownToken { .. }) = e.client_api_error_kind() {
895 print_exit("Server did not recognize our API token; did you log out from this session elsewhere?")
896 } else {
897 print_exit(e)
898 }
899 },
900 Err(e) => print_exit(e),
901 Ok(()) => (),
887902 }
888903
889904 fn restore_tty() {
src/worker.rs +5 −5
@@ -79,6 +79,7 @@ use matrix_sdk::{
7979 Client,
8080 ClientBuildError,
8181 DisplayName,
82 Error as MatrixError,
8283 RoomMemberships,
8384};
8485
@@ -538,7 +539,7 @@ async fn send_receipts_forever(client: &Client, store: &AsyncProgramStore) {
538539 }
539540}
540541
541pub async fn do_first_sync(client: &Client, store: &AsyncProgramStore) {
542pub async fn do_first_sync(client: &Client, store: &AsyncProgramStore) -> Result<(), MatrixError> {
542543 // Perform an initial, lazily-loaded sync.
543544 let mut room = RoomEventFilter::default();
544545 room.lazy_load_options = LazyLoadOptions::Enabled { include_redundant_members: false };
@@ -551,10 +552,7 @@ pub async fn do_first_sync(client: &Client, store: &AsyncProgramStore) {
551552
552553 let settings = SyncSettings::new().filter(filter.into());
553554
554 if let Err(e) = client.sync_once(settings).await {
555 tracing::error!(err = e.to_string(), "Failed to perform initial sync; will retry later");
556 return;
557 }
555 client.sync_once(settings).await?;
558556
559557 // Populate sync_info with our initial set of rooms/dms/spaces.
560558 refresh_rooms(client, store).await;
@@ -572,6 +570,8 @@ pub async fn do_first_sync(client: &Client, store: &AsyncProgramStore) {
572570 let room_id = room.as_ref().0.room_id().to_owned();
573571 need_load.insert(room_id, Need::MESSAGES);
574572 }
573
574 Ok(())
575575}
576576
577577#[derive(Debug)]