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};
28 28
29use clap::Parser; 29use clap::Parser;
30use matrix_sdk::crypto::encrypt_room_key_export; 30use matrix_sdk::crypto::encrypt_room_key_export;
31use matrix_sdk::ruma::api::client::error::ErrorKind;
31use matrix_sdk::ruma::OwnedUserId; 32use matrix_sdk::ruma::OwnedUserId;
32use modalkit::keybindings::InputBindings; 33use modalkit::keybindings::InputBindings;
33use rand::{distributions::Alphanumeric, Rng}; 34use rand::{distributions::Alphanumeric, Rng};
@@ -745,7 +746,7 @@ async fn login(worker: &Requester, settings: &ApplicationSettings) -> IambResult
745} 746}
746 747
747fn print_exit<T: Display, N>(v: T) -> N { 748fn print_exit<T: Display, N>(v: T) -> N {
748 println!("{v}"); 749 eprintln!("{v}");
749 process::exit(2); 750 process::exit(2);
750} 751}
751 752
@@ -847,7 +848,9 @@ async fn login_upgrade(
847 } 848 }
848 849
849 println!("* Syncing..."); 850 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)?;
851 854
852 Ok(()) 855 Ok(())
853} 856}
@@ -859,7 +862,9 @@ async fn login_normal(
859) -> IambResult<()> { 862) -> IambResult<()> {
860 println!("* Logging in for {}...", settings.profile.user_id); 863 println!("* Logging in for {}...", settings.profile.user_id);
861 login(worker, settings).await?; 864 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)?;
863 Ok(()) 868 Ok(())
864} 869}
865 870
@@ -878,12 +883,22 @@ async fn run(settings: ApplicationSettings) -> IambResult<()> {
878 let store = Arc::new(AsyncMutex::new(store)); 883 let store = Arc::new(AsyncMutex::new(store));
879 worker.init(store.clone()); 884 worker.init(store.clone());
880 885
881 if let Some((keydir, pass)) = import_keys { 886 let res = if let Some((keydir, pass)) = import_keys {
882 login_upgrade(keydir, pass, &worker, &settings, &store) 887 login_upgrade(keydir, pass, &worker, &settings, &store).await
883 .await
884 .unwrap_or_else(print_exit);
885 } else { 888 } 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(()) => (),
887 } 902 }
888 903
889 fn restore_tty() { 904 fn restore_tty() {
src/worker.rs +5 −5
@@ -79,6 +79,7 @@ use matrix_sdk::{
79 Client, 79 Client,
80 ClientBuildError, 80 ClientBuildError,
81 DisplayName, 81 DisplayName,
82 Error as MatrixError,
82 RoomMemberships, 83 RoomMemberships,
83}; 84};
84 85
@@ -538,7 +539,7 @@ async fn send_receipts_forever(client: &Client, store: &AsyncProgramStore) {
538 } 539 }
539} 540}
540 541
541pub async fn do_first_sync(client: &Client, store: &AsyncProgramStore) { 542pub async fn do_first_sync(client: &Client, store: &AsyncProgramStore) -> Result<(), MatrixError> {
542 // Perform an initial, lazily-loaded sync. 543 // Perform an initial, lazily-loaded sync.
543 let mut room = RoomEventFilter::default(); 544 let mut room = RoomEventFilter::default();
544 room.lazy_load_options = LazyLoadOptions::Enabled { include_redundant_members: false }; 545 room.lazy_load_options = LazyLoadOptions::Enabled { include_redundant_members: false };
@@ -551,10 +552,7 @@ pub async fn do_first_sync(client: &Client, store: &AsyncProgramStore) {
551 552
552 let settings = SyncSettings::new().filter(filter.into()); 553 let settings = SyncSettings::new().filter(filter.into());
553 554
554 if let Err(e) = client.sync_once(settings).await { 555 client.sync_once(settings).await?;
555 tracing::error!(err = e.to_string(), "Failed to perform initial sync; will retry later");
556 return;
557 }
558 556
559 // Populate sync_info with our initial set of rooms/dms/spaces. 557 // Populate sync_info with our initial set of rooms/dms/spaces.
560 refresh_rooms(client, store).await; 558 refresh_rooms(client, store).await;
@@ -572,6 +570,8 @@ pub async fn do_first_sync(client: &Client, store: &AsyncProgramStore) {
572 let room_id = room.as_ref().0.room_id().to_owned(); 570 let room_id = room.as_ref().0.room_id().to_owned();
573 need_load.insert(room_id, Need::MESSAGES); 571 need_load.insert(room_id, Need::MESSAGES);
574 } 572 }
573
574 Ok(())
575} 575}
576 576
577#[derive(Debug)] 577#[derive(Debug)]