From 5774075cad2627cdc8d4cf5055e904083af90a01 Mon Sep 17 00:00:00 2001 From: Elias Projahn Date: Fri, 5 Feb 2021 10:25:37 +0100 Subject: [PATCH] backend: Replace some errors with warnings --- crates/musicus_backend/Cargo.toml | 1 + crates/musicus_backend/src/error.rs | 3 --- crates/musicus_backend/src/lib.rs | 27 +++++++++++++++++++-------- crates/musicus_backend/src/library.rs | 8 ++++++-- crates/musicus_backend/src/secure.rs | 10 +++++++--- 5 files changed, 33 insertions(+), 16 deletions(-) diff --git a/crates/musicus_backend/Cargo.toml b/crates/musicus_backend/Cargo.toml index ad731ae..bc70aed 100644 --- a/crates/musicus_backend/Cargo.toml +++ b/crates/musicus_backend/Cargo.toml @@ -10,6 +10,7 @@ gio = "0.9.1" glib = "0.10.3" gstreamer = "0.16.4" gstreamer-player = "0.16.3" +log = "0.4.14" mpris-player = "0.6.0" musicus_client = { version = "0.1.0", path = "../musicus_client" } musicus_database = { version = "0.1.0", path = "../musicus_database" } diff --git a/crates/musicus_backend/src/error.rs b/crates/musicus_backend/src/error.rs index 522ac6c..a06090d 100644 --- a/crates/musicus_backend/src/error.rs +++ b/crates/musicus_backend/src/error.rs @@ -10,9 +10,6 @@ pub enum Error { #[error("An error happened using the SecretService.")] SecretServiceError(#[from] secret_service::Error), - #[error("An error happened in GLib.")] - GlibError(#[from] glib::BoolError), - #[error("A channel was canceled.")] ChannelError(#[from] futures_channel::oneshot::Canceled), diff --git a/crates/musicus_backend/src/lib.rs b/crates/musicus_backend/src/lib.rs index d6f2f89..d02d021 100644 --- a/crates/musicus_backend/src/lib.rs +++ b/crates/musicus_backend/src/lib.rs @@ -1,5 +1,6 @@ use futures_channel::mpsc; use gio::prelude::*; +use log::warn; use std::cell::RefCell; use std::path::PathBuf; use std::rc::Rc; @@ -74,18 +75,24 @@ impl Backend { } } - if let Some(data) = Self::load_login_data().await? { - self.client.set_login_data(data); + match Self::load_login_data().await { + Ok(Some(data)) => self.client.set_login_data(data), + Err(err) => warn!("The login data could not be loaded from SecretService. It will not \ + be available. Error message: {}", err), + _ => (), } Ok(()) } /// Set the URL of the Musicus server to connect to. - pub fn set_server_url(&self, url: &str) -> Result<()> { - self.settings.set_string("server-url", url)?; + pub fn set_server_url(&self, url: &str) { + if let Err(err) = self.settings.set_string("server-url", url) { + warn!("An error happened while trying to save the server URL to GSettings. Most \ + likely it will not be available at the next startup. Error message: {}", err); + } + self.client.set_server_url(url); - Ok(()) } /// Get the currently set server URL. @@ -94,10 +101,14 @@ impl Backend { } /// Set the user credentials to use. - pub async fn set_login_data(&self, data: LoginData) -> Result<()> { - Self::store_login_data(data.clone()).await?; + pub async fn set_login_data(&self, data: LoginData) { + if let Err(err) = Self::store_login_data(data.clone()).await { + warn!("An error happened while trying to store the login data using SecretService. \ + This means, that they will not be available at the next startup most likely. \ + Error message: {}", err); + } + self.client.set_login_data(data); - Ok(()) } pub fn cl(&self) -> &Client { diff --git a/crates/musicus_backend/src/library.rs b/crates/musicus_backend/src/library.rs index cad6b57..7348cb4 100644 --- a/crates/musicus_backend/src/library.rs +++ b/crates/musicus_backend/src/library.rs @@ -1,6 +1,7 @@ use crate::{Backend, BackendState, Player, Result}; use musicus_database::DbThread; use gio::prelude::*; +use log::warn; use std::path::PathBuf; use std::rc::Rc; @@ -19,8 +20,11 @@ impl Backend { /// Set the path to the music library folder and start a database thread in the background. pub async fn set_music_library_path(&self, path: PathBuf) -> Result<()> { - self.settings - .set_string("music-library-path", path.to_str().unwrap())?; + if let Err(err) = self.settings.set_string("music-library-path", path.to_str().unwrap()) { + warn!("The music library path could not be saved using GSettings. It will most likely \ + not be available at the next startup. Error message: {}", err); + } + self.set_music_library_path_priv(path).await } diff --git a/crates/musicus_backend/src/secure.rs b/crates/musicus_backend/src/secure.rs index 02c34e4..2e42aee 100644 --- a/crates/musicus_backend/src/secure.rs +++ b/crates/musicus_backend/src/secure.rs @@ -1,4 +1,4 @@ -use crate::{Backend, Result}; +use crate::{Backend, Error, Result}; use musicus_client::LoginData; use futures_channel::oneshot; use secret_service::{Collection, EncryptionType, SecretService}; @@ -32,8 +32,12 @@ impl Backend { Ok(match item { Some(item) => { - // TODO: Delete the item when malformed. - let username = item.get_attributes()?.get("username").unwrap().to_owned(); + let username = item + .get_attributes()? + .get("username") + .ok_or(Error::Other("Missing username in SecretService attributes."))? + .to_owned(); + let password = std::str::from_utf8(&item.get_secret()?)?.to_owned(); Some(LoginData { username, password })