Improve error handling on startup

This commit is contained in:
2023-11-04 15:17:36 +01:00
parent 6998f0b84f
commit 0d24d659c5

View File

@ -19,7 +19,7 @@ use std::collections::VecDeque;
use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::RwLock; use std::sync::RwLock;
use color_eyre::eyre::{Result, WrapErr}; use color_eyre::eyre::{eyre, Result, WrapErr};
use crossterm::event::KeyEvent; use crossterm::event::KeyEvent;
use futures::stream::FuturesUnordered; use futures::stream::FuturesUnordered;
use futures::{FutureExt, StreamExt}; use futures::{FutureExt, StreamExt};
@ -68,15 +68,19 @@ impl App {
tokio::spawn(async move { tokio::spawn(async move {
let server_name = conf.user_id.server_name(); let server_name = conf.user_id.server_name();
let user_dir = datadir.join(conf.user_id.to_string()); let user_dir = datadir.join(conf.user_id.to_string());
std::fs::create_dir_all(&user_dir)
.with_context(|| format!("Could not create {}", user_dir.display()))?;
// If there is at least one device state for that user, use it. Otherwise, // If there is at least one device state for that user, use it. Otherwise,
// generate a random device id // generate a random device id
let device_dir = std::fs::read_dir(&user_dir) let device_dir = std::fs::read_dir(&user_dir)
.unwrap_or_else(|e| panic!("Could not read user dir: {}", e)) .with_context(|| format!("Could not read {}", user_dir.display()))?
.next() .next()
.map(|dir_entry| { .and_then(|dir_entry| match dir_entry {
dir_entry Ok(dir_entry) => Some(dir_entry.path()),
.unwrap_or_else(|e| panic!("Could not read entry in {}: {}", user_dir.display(), e)) Err(e) => {
.path() tracing::error!("Could not read entry in {}: {}", user_dir.display(), e);
None
},
}) })
.unwrap_or_else(|| user_dir.join(matrix_sdk::ruma::DeviceId::new().as_str())); .unwrap_or_else(|| user_dir.join(matrix_sdk::ruma::DeviceId::new().as_str()));
// Extract device id from the path // Extract device id from the path
@ -85,7 +89,7 @@ impl App {
.file_name() .file_name()
.expect("empty path to state store") .expect("empty path to state store")
.to_str() .to_str()
.expect("device id is not valid UTF8"); .ok_or_else(|| eyre!("device id in {} is not valid UTF8", device_dir.display()))?;
let client = matrix_sdk::Client::builder() let client = matrix_sdk::Client::builder()
.server_name(server_name) .server_name(server_name)
.sqlite_store( .sqlite_store(
@ -103,6 +107,12 @@ impl App {
.send() .send()
.await .await
.with_context(|| format!("Could not login as {}", conf.user_id))?; .with_context(|| format!("Could not login as {}", conf.user_id))?;
tracing::info!(
"Logged in as {}",
client
.user_id()
.ok_or_else(|| eyre!("Unexpectedly not logged in as {}", conf.user_id))?,
);
Ok::<_, color_eyre::eyre::Report>(client) Ok::<_, color_eyre::eyre::Report>(client)
}) })
}); });