mirror of
https://github.com/johrpan/musicus.git
synced 2025-10-26 19:57:25 +01:00
Add custom error type for the backend
This commit is contained in:
parent
4ad7a1fb28
commit
5a41d5008f
14 changed files with 44 additions and 44 deletions
|
|
@ -22,6 +22,7 @@ rand = "0.7.3"
|
||||||
secret-service = "2.0.1"
|
secret-service = "2.0.1"
|
||||||
serde = { version = "1.0.117", features = ["derive"] }
|
serde = { version = "1.0.117", features = ["derive"] }
|
||||||
serde_json = "1.0.59"
|
serde_json = "1.0.59"
|
||||||
|
thiserror = "1.0.23"
|
||||||
uuid = { version = "0.8", features = ["v4"] }
|
uuid = { version = "0.8", features = ["v4"] }
|
||||||
|
|
||||||
[dependencies.gdk]
|
[dependencies.gdk]
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
use crate::backend::{Backend, Ensemble};
|
use crate::backend::{Backend, Ensemble, Result};
|
||||||
use anyhow::Result;
|
|
||||||
|
|
||||||
impl Backend {
|
impl Backend {
|
||||||
/// Get all available ensembles from the server.
|
/// Get all available ensembles from the server.
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
use crate::backend::{Backend, Instrument};
|
use crate::backend::{Backend, Instrument, Result};
|
||||||
use anyhow::Result;
|
|
||||||
|
|
||||||
impl Backend {
|
impl Backend {
|
||||||
/// Get all available instruments from the server.
|
/// Get all available instruments from the server.
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
use crate::backend::{Backend, Medium};
|
use crate::backend::{Backend, Medium, Result};
|
||||||
use anyhow::Result;
|
|
||||||
|
|
||||||
impl Backend {
|
impl Backend {
|
||||||
/// Get all available mediums from the server, that contain the specified
|
/// Get all available mediums from the server, that contain the specified
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,4 @@
|
||||||
use super::secure;
|
use crate::backend::{Backend, Error, Result, secure};
|
||||||
use super::Backend;
|
|
||||||
use anyhow::{anyhow, bail, Result};
|
|
||||||
use gio::prelude::*;
|
use gio::prelude::*;
|
||||||
use isahc::http::StatusCode;
|
use isahc::http::StatusCode;
|
||||||
use isahc::prelude::*;
|
use isahc::prelude::*;
|
||||||
|
|
@ -74,11 +72,20 @@ impl Backend {
|
||||||
self.server_url.borrow().clone()
|
self.server_url.borrow().clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Require the server URL to be set.
|
||||||
|
fn server_url(&self) -> Result<String> {
|
||||||
|
self.get_server_url().ok_or(Error::Other("The server URL is not available!"))
|
||||||
|
}
|
||||||
|
|
||||||
/// Get the currently stored login credentials.
|
/// Get the currently stored login credentials.
|
||||||
pub fn get_login_data(&self) -> Option<LoginData> {
|
pub fn get_login_data(&self) -> Option<LoginData> {
|
||||||
self.login_data.borrow().clone()
|
self.login_data.borrow().clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn login_data(&self) -> Result<LoginData> {
|
||||||
|
self.get_login_data().ok_or(Error::Other("The login data is unset!"))
|
||||||
|
}
|
||||||
|
|
||||||
/// Set the user credentials to use.
|
/// Set the user credentials to use.
|
||||||
pub async fn set_login_data(&self, data: LoginData) -> Result<()> {
|
pub async fn set_login_data(&self, data: LoginData) -> Result<()> {
|
||||||
secure::store_login_data(data.clone()).await?;
|
secure::store_login_data(data.clone()).await?;
|
||||||
|
|
@ -89,8 +96,8 @@ impl Backend {
|
||||||
|
|
||||||
/// Try to login a user with the provided credentials and return, wether the login suceeded.
|
/// Try to login a user with the provided credentials and return, wether the login suceeded.
|
||||||
pub async fn login(&self) -> Result<bool> {
|
pub async fn login(&self) -> Result<bool> {
|
||||||
let server_url = self.get_server_url().ok_or(anyhow!("No server URL set!"))?;
|
let server_url = self.server_url()?;
|
||||||
let data = self.get_login_data().ok_or(anyhow!("No login data set!"))?;
|
let data = self.login_data()?;
|
||||||
|
|
||||||
let request = Request::post(format!("{}/login", server_url))
|
let request = Request::post(format!("{}/login", server_url))
|
||||||
.timeout(Duration::from_secs(10))
|
.timeout(Duration::from_secs(10))
|
||||||
|
|
@ -106,7 +113,7 @@ impl Backend {
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
StatusCode::UNAUTHORIZED => false,
|
StatusCode::UNAUTHORIZED => false,
|
||||||
_ => bail!("Unexpected response status!"),
|
status_code => Err(Error::UnexpectedResponse(status_code))?,
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(success)
|
Ok(success)
|
||||||
|
|
@ -114,7 +121,7 @@ impl Backend {
|
||||||
|
|
||||||
/// Make an unauthenticated get request to the server.
|
/// Make an unauthenticated get request to the server.
|
||||||
async fn get(&self, url: &str) -> Result<String> {
|
async fn get(&self, url: &str) -> Result<String> {
|
||||||
let server_url = self.get_server_url().ok_or(anyhow!("No server URL set!"))?;
|
let server_url = self.server_url()?;
|
||||||
|
|
||||||
let mut response = Request::get(format!("{}/{}", server_url, url))
|
let mut response = Request::get(format!("{}/{}", server_url, url))
|
||||||
.timeout(Duration::from_secs(10))
|
.timeout(Duration::from_secs(10))
|
||||||
|
|
@ -138,7 +145,7 @@ impl Backend {
|
||||||
if self.login().await? {
|
if self.login().await? {
|
||||||
response = self.post_priv(url, body).await?;
|
response = self.post_priv(url, body).await?;
|
||||||
} else {
|
} else {
|
||||||
bail!("Login failed!");
|
Err(Error::LoginFailed)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -148,7 +155,7 @@ impl Backend {
|
||||||
let mut response = if self.login().await? {
|
let mut response = if self.login().await? {
|
||||||
self.post_priv(url, body).await?
|
self.post_priv(url, body).await?
|
||||||
} else {
|
} else {
|
||||||
bail!("Login failed!");
|
Err(Error::LoginFailed)?
|
||||||
};
|
};
|
||||||
|
|
||||||
response.text_async().await?
|
response.text_async().await?
|
||||||
|
|
@ -160,8 +167,8 @@ impl Backend {
|
||||||
|
|
||||||
/// Post something to the server assuming there is a valid login token.
|
/// Post something to the server assuming there is a valid login token.
|
||||||
async fn post_priv(&self, url: &str, body: String) -> Result<Response<Body>> {
|
async fn post_priv(&self, url: &str, body: String) -> Result<Response<Body>> {
|
||||||
let server_url = self.get_server_url().ok_or(anyhow!("No server URL set!"))?;
|
let server_url = self.server_url()?;
|
||||||
let token = self.get_token().ok_or(anyhow!("No login token!"))?;
|
let token = self.get_token().ok_or(Error::Other("No login token found!"))?;
|
||||||
|
|
||||||
let response = Request::post(format!("{}/{}", server_url, url))
|
let response = Request::post(format!("{}/{}", server_url, url))
|
||||||
.timeout(Duration::from_secs(10))
|
.timeout(Duration::from_secs(10))
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
use crate::backend::{Backend, Person};
|
use crate::backend::{Backend, Person, Result};
|
||||||
use anyhow::Result;
|
|
||||||
|
|
||||||
impl Backend {
|
impl Backend {
|
||||||
/// Get all available persons from the server.
|
/// Get all available persons from the server.
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
use crate::backend::{Backend, Recording};
|
use crate::backend::{Backend, Recording, Result};
|
||||||
use anyhow::Result;
|
|
||||||
|
|
||||||
impl Backend {
|
impl Backend {
|
||||||
/// Get all available recordings from the server.
|
/// Get all available recordings from the server.
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
use super::Backend;
|
use crate::backend::{Backend, Result};
|
||||||
use anyhow::{anyhow, Result};
|
|
||||||
use isahc::http::StatusCode;
|
use isahc::http::StatusCode;
|
||||||
use isahc::prelude::*;
|
use isahc::prelude::*;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
@ -35,7 +34,7 @@ impl Backend {
|
||||||
/// Register a new user and return whether the process suceeded. This will
|
/// Register a new user and return whether the process suceeded. This will
|
||||||
/// not store the new login credentials.
|
/// not store the new login credentials.
|
||||||
pub async fn register(&self, data: UserRegistration) -> Result<bool> {
|
pub async fn register(&self, data: UserRegistration) -> Result<bool> {
|
||||||
let server_url = self.get_server_url().ok_or(anyhow!("No server URL set!"))?;
|
let server_url = self.server_url()?;
|
||||||
|
|
||||||
let mut response = Request::post(format!("{}/users", server_url))
|
let mut response = Request::post(format!("{}/users", server_url))
|
||||||
.timeout(Duration::from_secs(10))
|
.timeout(Duration::from_secs(10))
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
use crate::backend::{Backend, Work};
|
use crate::backend::{Backend, Result, Work};
|
||||||
use anyhow::Result;
|
|
||||||
|
|
||||||
impl Backend {
|
impl Backend {
|
||||||
/// Get all available works from the server.
|
/// Get all available works from the server.
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
use super::{Backend, BackendState, DbThread, Player};
|
use crate::backend::{Backend, BackendState, DbThread, Player, Result};
|
||||||
use anyhow::Result;
|
|
||||||
use gio::prelude::*;
|
use gio::prelude::*;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,3 @@
|
||||||
use anyhow::Result;
|
|
||||||
use futures_channel::mpsc;
|
use futures_channel::mpsc;
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
@ -10,6 +9,9 @@ pub use client::*;
|
||||||
pub mod database;
|
pub mod database;
|
||||||
pub use database::*;
|
pub use database::*;
|
||||||
|
|
||||||
|
pub mod error;
|
||||||
|
pub use error::*;
|
||||||
|
|
||||||
pub mod library;
|
pub mod library;
|
||||||
pub use library::*;
|
pub use library::*;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,4 @@
|
||||||
use crate::backend::TrackSet;
|
use crate::backend::{Error, Result, TrackSet};
|
||||||
use anyhow::anyhow;
|
|
||||||
use anyhow::Result;
|
|
||||||
use gstreamer_player::prelude::*;
|
use gstreamer_player::prelude::*;
|
||||||
use std::cell::{Cell, RefCell};
|
use std::cell::{Cell, RefCell};
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
@ -122,9 +120,7 @@ impl Player {
|
||||||
|
|
||||||
pub fn add_item(&self, item: PlaylistItem) -> Result<()> {
|
pub fn add_item(&self, item: PlaylistItem) -> Result<()> {
|
||||||
if item.indices.is_empty() {
|
if item.indices.is_empty() {
|
||||||
Err(anyhow!(
|
Err(Error::Other("Tried to add an empty playlist item!"))
|
||||||
"Tried to add playlist item without tracks to playlist!"
|
|
||||||
))
|
|
||||||
} else {
|
} else {
|
||||||
let was_empty = {
|
let was_empty = {
|
||||||
let mut playlist = self.playlist.borrow_mut();
|
let mut playlist = self.playlist.borrow_mut();
|
||||||
|
|
@ -188,11 +184,13 @@ impl Player {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn previous(&self) -> Result<()> {
|
pub fn previous(&self) -> Result<()> {
|
||||||
let mut current_item = self.current_item.get().ok_or(anyhow!("No current item!"))?;
|
let mut current_item = self.current_item.get()
|
||||||
|
.ok_or(Error::Other("Player tried to access non existant current item."))?;
|
||||||
|
|
||||||
let mut current_track = self
|
let mut current_track = self
|
||||||
.current_track
|
.current_track
|
||||||
.get()
|
.get()
|
||||||
.ok_or(anyhow!("No current track!"))?;
|
.ok_or(Error::Other("Player tried to access non existant current track."))?;
|
||||||
|
|
||||||
let playlist = self.playlist.borrow();
|
let playlist = self.playlist.borrow();
|
||||||
if current_track > 0 {
|
if current_track > 0 {
|
||||||
|
|
@ -201,7 +199,7 @@ impl Player {
|
||||||
current_item -= 1;
|
current_item -= 1;
|
||||||
current_track = playlist[current_item].indices.len() - 1;
|
current_track = playlist[current_item].indices.len() - 1;
|
||||||
} else {
|
} else {
|
||||||
return Err(anyhow!("No previous track!"));
|
return Err(Error::Other("No existing previous track."));
|
||||||
}
|
}
|
||||||
|
|
||||||
self.set_track(current_item, current_track)
|
self.set_track(current_item, current_track)
|
||||||
|
|
@ -223,11 +221,12 @@ impl Player {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn next(&self) -> Result<()> {
|
pub fn next(&self) -> Result<()> {
|
||||||
let mut current_item = self.current_item.get().ok_or(anyhow!("No current item!"))?;
|
let mut current_item = self.current_item.get()
|
||||||
|
.ok_or(Error::Other("Player tried to access non existant current item."))?;
|
||||||
let mut current_track = self
|
let mut current_track = self
|
||||||
.current_track
|
.current_track
|
||||||
.get()
|
.get()
|
||||||
.ok_or(anyhow!("No current track!"))?;
|
.ok_or(Error::Other("Player tried to access non existant current track."))?;
|
||||||
|
|
||||||
let playlist = self.playlist.borrow();
|
let playlist = self.playlist.borrow();
|
||||||
let item = &playlist[current_item];
|
let item = &playlist[current_item];
|
||||||
|
|
@ -237,7 +236,7 @@ impl Player {
|
||||||
current_item += 1;
|
current_item += 1;
|
||||||
current_track = 0;
|
current_track = 0;
|
||||||
} else {
|
} else {
|
||||||
return Err(anyhow!("No next track!"));
|
return Err(Error::Other("No existing previous track."));
|
||||||
}
|
}
|
||||||
|
|
||||||
self.set_track(current_item, current_track)
|
self.set_track(current_item, current_track)
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
use crate::backend::LoginData;
|
use crate::backend::{LoginData, Result};
|
||||||
use anyhow::Result;
|
|
||||||
use futures_channel::oneshot;
|
use futures_channel::oneshot;
|
||||||
use secret_service::{Collection, EncryptionType, SecretService};
|
use secret_service::{Collection, EncryptionType, SecretService};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
|
use crate::backend::Result;
|
||||||
use crate::widgets::List;
|
use crate::widgets::List;
|
||||||
use anyhow::Result;
|
|
||||||
use glib::clone;
|
use glib::clone;
|
||||||
use gtk::prelude::*;
|
use gtk::prelude::*;
|
||||||
use gtk_macros::get_widget;
|
use gtk_macros::get_widget;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue