Skip to content

Commit

Permalink
Provide better error message for M_UNKNOWN_TOKEN (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
ulyssa committed Mar 24, 2024
1 parent 46e081b commit 1e9b6cc
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
31 changes: 23 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use std::time::{Duration, Instant};

use clap::Parser;
use matrix_sdk::crypto::encrypt_room_key_export;
use matrix_sdk::ruma::api::client::error::ErrorKind;
use matrix_sdk::ruma::OwnedUserId;
use modalkit::keybindings::InputBindings;
use rand::{distributions::Alphanumeric, Rng};
Expand Down Expand Up @@ -745,7 +746,7 @@ async fn login(worker: &Requester, settings: &ApplicationSettings) -> IambResult
}

fn print_exit<T: Display, N>(v: T) -> N {
println!("{v}");
eprintln!("{v}");
process::exit(2);
}

Expand Down Expand Up @@ -847,7 +848,9 @@ async fn login_upgrade(
}

println!("* Syncing...");
worker::do_first_sync(&worker.client, store).await;
worker::do_first_sync(&worker.client, store)
.await
.map_err(IambError::from)?;

Ok(())
}
Expand All @@ -859,7 +862,9 @@ async fn login_normal(
) -> IambResult<()> {
println!("* Logging in for {}...", settings.profile.user_id);
login(worker, settings).await?;
worker::do_first_sync(&worker.client, store).await;
worker::do_first_sync(&worker.client, store)
.await
.map_err(IambError::from)?;
Ok(())
}

Expand All @@ -878,12 +883,22 @@ async fn run(settings: ApplicationSettings) -> IambResult<()> {
let store = Arc::new(AsyncMutex::new(store));
worker.init(store.clone());

if let Some((keydir, pass)) = import_keys {
login_upgrade(keydir, pass, &worker, &settings, &store)
.await
.unwrap_or_else(print_exit);
let res = if let Some((keydir, pass)) = import_keys {
login_upgrade(keydir, pass, &worker, &settings, &store).await
} else {
login_normal(&worker, &settings, &store).await.unwrap_or_else(print_exit);
login_normal(&worker, &settings, &store).await
};

match res {
Err(UIError::Application(IambError::Matrix(e))) => {
if let Some(ErrorKind::UnknownToken { .. }) = e.client_api_error_kind() {
print_exit("Server did not recognize our API token; did you log out from this session elsewhere?")
} else {
print_exit(e)
}
},
Err(e) => print_exit(e),
Ok(()) => (),
}

fn restore_tty() {
Expand Down
10 changes: 5 additions & 5 deletions src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ use matrix_sdk::{
Client,
ClientBuildError,
DisplayName,
Error as MatrixError,
RoomMemberships,
};

Expand Down Expand Up @@ -538,7 +539,7 @@ async fn send_receipts_forever(client: &Client, store: &AsyncProgramStore) {
}
}

pub async fn do_first_sync(client: &Client, store: &AsyncProgramStore) {
pub async fn do_first_sync(client: &Client, store: &AsyncProgramStore) -> Result<(), MatrixError> {
// Perform an initial, lazily-loaded sync.
let mut room = RoomEventFilter::default();
room.lazy_load_options = LazyLoadOptions::Enabled { include_redundant_members: false };
Expand All @@ -551,10 +552,7 @@ pub async fn do_first_sync(client: &Client, store: &AsyncProgramStore) {

let settings = SyncSettings::new().filter(filter.into());

if let Err(e) = client.sync_once(settings).await {
tracing::error!(err = e.to_string(), "Failed to perform initial sync; will retry later");
return;
}
client.sync_once(settings).await?;

// Populate sync_info with our initial set of rooms/dms/spaces.
refresh_rooms(client, store).await;
Expand All @@ -572,6 +570,8 @@ pub async fn do_first_sync(client: &Client, store: &AsyncProgramStore) {
let room_id = room.as_ref().0.room_id().to_owned();
need_load.insert(room_id, Need::MESSAGES);
}

Ok(())
}

#[derive(Debug)]
Expand Down

0 comments on commit 1e9b6cc

Please sign in to comment.