Error cleanup
This commit is contained in:
parent
4ec8f2161b
commit
1e9d307a05
5 changed files with 32 additions and 82 deletions
|
@ -16,14 +16,6 @@ pub enum Error {
|
||||||
DatabaseConnection(#[from] db::Error),
|
DatabaseConnection(#[from] db::Error),
|
||||||
#[error(transparent)]
|
#[error(transparent)]
|
||||||
Database(#[from] diesel::result::Error),
|
Database(#[from] diesel::result::Error),
|
||||||
#[error("Unspecified")]
|
|
||||||
Unspecified,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<anyhow::Error> for Error {
|
|
||||||
fn from(_: anyhow::Error) -> Self {
|
|
||||||
Error::Unspecified
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, Deserialize, Insertable, PartialEq, Eq, Queryable, Serialize)]
|
#[derive(Clone, Debug, Deserialize, Insertable, PartialEq, Eq, Queryable, Serialize)]
|
||||||
|
|
|
@ -11,6 +11,8 @@ use crate::db::{self, playlist_songs, playlists, users, DB};
|
||||||
|
|
||||||
#[derive(thiserror::Error, Debug)]
|
#[derive(thiserror::Error, Debug)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
|
#[error(transparent)]
|
||||||
|
Database(#[from] diesel::result::Error),
|
||||||
#[error(transparent)]
|
#[error(transparent)]
|
||||||
DatabaseConnection(#[from] db::Error),
|
DatabaseConnection(#[from] db::Error),
|
||||||
#[error("User not found")]
|
#[error("User not found")]
|
||||||
|
@ -19,14 +21,6 @@ pub enum Error {
|
||||||
PlaylistNotFound,
|
PlaylistNotFound,
|
||||||
#[error(transparent)]
|
#[error(transparent)]
|
||||||
Vfs(#[from] vfs::Error),
|
Vfs(#[from] vfs::Error),
|
||||||
#[error("Unspecified")]
|
|
||||||
Unspecified,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<anyhow::Error> for Error {
|
|
||||||
fn from(_: anyhow::Error) -> Self {
|
|
||||||
Error::Unspecified
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
|
@ -49,8 +43,7 @@ impl Manager {
|
||||||
.filter(name.eq(owner))
|
.filter(name.eq(owner))
|
||||||
.select((id,))
|
.select((id,))
|
||||||
.first(&mut connection)
|
.first(&mut connection)
|
||||||
.optional()
|
.optional()?
|
||||||
.map_err(anyhow::Error::new)?
|
|
||||||
.ok_or(Error::UserNotFound)?
|
.ok_or(Error::UserNotFound)?
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -58,8 +51,7 @@ impl Manager {
|
||||||
use self::playlists::dsl::*;
|
use self::playlists::dsl::*;
|
||||||
let found_playlists: Vec<String> = Playlist::belonging_to(&user)
|
let found_playlists: Vec<String> = Playlist::belonging_to(&user)
|
||||||
.select(name)
|
.select(name)
|
||||||
.load(&mut connection)
|
.load(&mut connection)?;
|
||||||
.map_err(anyhow::Error::new)?;
|
|
||||||
Ok(found_playlists)
|
Ok(found_playlists)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -84,8 +76,7 @@ impl Manager {
|
||||||
.filter(name.eq(owner))
|
.filter(name.eq(owner))
|
||||||
.select((id,))
|
.select((id,))
|
||||||
.first(&mut connection)
|
.first(&mut connection)
|
||||||
.optional()
|
.optional()?
|
||||||
.map_err(anyhow::Error::new)?
|
|
||||||
.ok_or(Error::UserNotFound)?
|
.ok_or(Error::UserNotFound)?
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -97,16 +88,14 @@ impl Manager {
|
||||||
|
|
||||||
diesel::insert_into(playlists::table)
|
diesel::insert_into(playlists::table)
|
||||||
.values(&new_playlist)
|
.values(&new_playlist)
|
||||||
.execute(&mut connection)
|
.execute(&mut connection)?;
|
||||||
.map_err(anyhow::Error::new)?;
|
|
||||||
|
|
||||||
playlist = {
|
playlist = {
|
||||||
use self::playlists::dsl::*;
|
use self::playlists::dsl::*;
|
||||||
playlists
|
playlists
|
||||||
.select((id, owner))
|
.select((id, owner))
|
||||||
.filter(name.eq(playlist_name).and(owner.eq(user.id)))
|
.filter(name.eq(playlist_name).and(owner.eq(user.id)))
|
||||||
.get_result(&mut connection)
|
.get_result(&mut connection)?
|
||||||
.map_err(anyhow::Error::new)?
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -130,19 +119,17 @@ impl Manager {
|
||||||
|
|
||||||
{
|
{
|
||||||
let mut connection = self.db.connect()?;
|
let mut connection = self.db.connect()?;
|
||||||
connection
|
connection.transaction::<_, diesel::result::Error, _>(|connection| {
|
||||||
.transaction::<_, diesel::result::Error, _>(|connection| {
|
// Delete old content (if any)
|
||||||
// Delete old content (if any)
|
let old_songs = PlaylistSong::belonging_to(&playlist);
|
||||||
let old_songs = PlaylistSong::belonging_to(&playlist);
|
diesel::delete(old_songs).execute(connection)?;
|
||||||
diesel::delete(old_songs).execute(connection)?;
|
|
||||||
|
|
||||||
// Insert content
|
// Insert content
|
||||||
diesel::insert_into(playlist_songs::table)
|
diesel::insert_into(playlist_songs::table)
|
||||||
.values(&new_songs)
|
.values(&new_songs)
|
||||||
.execute(&mut *connection)?; // TODO https://github.com/diesel-rs/diesel/issues/1822
|
.execute(&mut *connection)?; // TODO https://github.com/diesel-rs/diesel/issues/1822
|
||||||
Ok(())
|
Ok(())
|
||||||
})
|
})?;
|
||||||
.map_err(anyhow::Error::new)?;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -162,8 +149,7 @@ impl Manager {
|
||||||
.filter(name.eq(owner))
|
.filter(name.eq(owner))
|
||||||
.select((id,))
|
.select((id,))
|
||||||
.first(&mut connection)
|
.first(&mut connection)
|
||||||
.optional()
|
.optional()?
|
||||||
.map_err(anyhow::Error::new)?
|
|
||||||
.ok_or(Error::UserNotFound)?
|
.ok_or(Error::UserNotFound)?
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -174,8 +160,7 @@ impl Manager {
|
||||||
.select((id, owner))
|
.select((id, owner))
|
||||||
.filter(name.eq(playlist_name).and(owner.eq(user.id)))
|
.filter(name.eq(playlist_name).and(owner.eq(user.id)))
|
||||||
.get_result(&mut connection)
|
.get_result(&mut connection)
|
||||||
.optional()
|
.optional()?
|
||||||
.map_err(anyhow::Error::new)?
|
|
||||||
.ok_or(Error::PlaylistNotFound)?
|
.ok_or(Error::PlaylistNotFound)?
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -190,9 +175,7 @@ impl Manager {
|
||||||
"#,
|
"#,
|
||||||
);
|
);
|
||||||
let query = query.bind::<sql_types::Integer, _>(playlist.id);
|
let query = query.bind::<sql_types::Integer, _>(playlist.id);
|
||||||
songs = query
|
songs = query.get_results(&mut connection)?;
|
||||||
.get_results(&mut connection)
|
|
||||||
.map_err(anyhow::Error::new)?;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Map real path to virtual paths
|
// Map real path to virtual paths
|
||||||
|
@ -213,18 +196,14 @@ impl Manager {
|
||||||
.filter(name.eq(owner))
|
.filter(name.eq(owner))
|
||||||
.select((id,))
|
.select((id,))
|
||||||
.first(&mut connection)
|
.first(&mut connection)
|
||||||
.optional()
|
.optional()?
|
||||||
.map_err(anyhow::Error::new)?
|
|
||||||
.ok_or(Error::UserNotFound)?
|
.ok_or(Error::UserNotFound)?
|
||||||
};
|
};
|
||||||
|
|
||||||
{
|
{
|
||||||
use self::playlists::dsl::*;
|
use self::playlists::dsl::*;
|
||||||
let q = Playlist::belonging_to(&user).filter(name.eq(playlist_name));
|
let q = Playlist::belonging_to(&user).filter(name.eq(playlist_name));
|
||||||
match diesel::delete(q)
|
match diesel::delete(q).execute(&mut connection)? {
|
||||||
.execute(&mut connection)
|
|
||||||
.map_err(anyhow::Error::new)?
|
|
||||||
{
|
|
||||||
0 => Err(Error::PlaylistNotFound),
|
0 => Err(Error::PlaylistNotFound),
|
||||||
_ => Ok(()),
|
_ => Ok(()),
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,14 +20,6 @@ pub enum Error {
|
||||||
IndexAlbumArtPatternInvalid,
|
IndexAlbumArtPatternInvalid,
|
||||||
#[error(transparent)]
|
#[error(transparent)]
|
||||||
Database(#[from] diesel::result::Error),
|
Database(#[from] diesel::result::Error),
|
||||||
#[error("Unspecified")]
|
|
||||||
Unspecified,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<anyhow::Error> for Error {
|
|
||||||
fn from(_: anyhow::Error) -> Self {
|
|
||||||
Error::Unspecified
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Default)]
|
#[derive(Clone, Default)]
|
||||||
|
|
|
@ -16,14 +16,6 @@ pub enum Error {
|
||||||
DatabaseConnection(#[from] db::Error),
|
DatabaseConnection(#[from] db::Error),
|
||||||
#[error(transparent)]
|
#[error(transparent)]
|
||||||
Database(#[from] diesel::result::Error),
|
Database(#[from] diesel::result::Error),
|
||||||
#[error("Unspecified")]
|
|
||||||
Unspecified,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<anyhow::Error> for Error {
|
|
||||||
fn from(_: anyhow::Error) -> Self {
|
|
||||||
Error::Unspecified
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, Deserialize, Insertable, PartialEq, Eq, Queryable, Serialize)]
|
#[derive(Clone, Debug, Deserialize, Insertable, PartialEq, Eq, Queryable, Serialize)]
|
||||||
|
@ -123,16 +115,14 @@ impl Manager {
|
||||||
|
|
||||||
pub fn set_mount_dirs(&self, mount_dirs: &[MountDir]) -> Result<(), Error> {
|
pub fn set_mount_dirs(&self, mount_dirs: &[MountDir]) -> Result<(), Error> {
|
||||||
let mut connection = self.db.connect()?;
|
let mut connection = self.db.connect()?;
|
||||||
connection
|
connection.transaction::<_, diesel::result::Error, _>(|connection| {
|
||||||
.transaction::<_, diesel::result::Error, _>(|connection| {
|
use self::mount_points::dsl::*;
|
||||||
use self::mount_points::dsl::*;
|
diesel::delete(mount_points).execute(&mut *connection)?;
|
||||||
diesel::delete(mount_points).execute(&mut *connection)?;
|
diesel::insert_into(mount_points)
|
||||||
diesel::insert_into(mount_points)
|
.values(mount_dirs)
|
||||||
.values(mount_dirs)
|
.execute(&mut *connection)?; // TODO https://github.com/diesel-rs/diesel/issues/1822
|
||||||
.execute(&mut *connection)?; // TODO https://github.com/diesel-rs/diesel/issues/1822
|
Ok(())
|
||||||
Ok(())
|
})?;
|
||||||
})
|
|
||||||
.map_err(anyhow::Error::new)?;
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -62,11 +62,11 @@ impl From<config::Error> for APIError {
|
||||||
impl From<playlist::Error> for APIError {
|
impl From<playlist::Error> for APIError {
|
||||||
fn from(error: playlist::Error) -> APIError {
|
fn from(error: playlist::Error) -> APIError {
|
||||||
match error {
|
match error {
|
||||||
|
playlist::Error::Database(_) => APIError::Internal,
|
||||||
playlist::Error::DatabaseConnection(e) => e.into(),
|
playlist::Error::DatabaseConnection(e) => e.into(),
|
||||||
playlist::Error::PlaylistNotFound => APIError::PlaylistNotFound,
|
playlist::Error::PlaylistNotFound => APIError::PlaylistNotFound,
|
||||||
playlist::Error::UserNotFound => APIError::UserNotFound,
|
playlist::Error::UserNotFound => APIError::UserNotFound,
|
||||||
playlist::Error::Vfs(e) => e.into(),
|
playlist::Error::Vfs(e) => e.into(),
|
||||||
playlist::Error::Unspecified => APIError::Unspecified,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -92,7 +92,6 @@ impl From<settings::Error> for APIError {
|
||||||
settings::Error::MiscSettingsNotFound => APIError::Internal,
|
settings::Error::MiscSettingsNotFound => APIError::Internal,
|
||||||
settings::Error::IndexAlbumArtPatternInvalid => APIError::Internal,
|
settings::Error::IndexAlbumArtPatternInvalid => APIError::Internal,
|
||||||
settings::Error::Database(_) => APIError::Internal,
|
settings::Error::Database(_) => APIError::Internal,
|
||||||
settings::Error::Unspecified => APIError::Unspecified,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -123,7 +122,6 @@ impl From<vfs::Error> for APIError {
|
||||||
vfs::Error::CouldNotMapToRealPath(_) => APIError::VFSPathNotFound,
|
vfs::Error::CouldNotMapToRealPath(_) => APIError::VFSPathNotFound,
|
||||||
vfs::Error::Database(_) => APIError::Internal,
|
vfs::Error::Database(_) => APIError::Internal,
|
||||||
vfs::Error::DatabaseConnection(e) => e.into(),
|
vfs::Error::DatabaseConnection(e) => e.into(),
|
||||||
vfs::Error::Unspecified => APIError::Unspecified,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -131,10 +129,9 @@ impl From<vfs::Error> for APIError {
|
||||||
impl From<ddns::Error> for APIError {
|
impl From<ddns::Error> for APIError {
|
||||||
fn from(error: ddns::Error) -> APIError {
|
fn from(error: ddns::Error) -> APIError {
|
||||||
match error {
|
match error {
|
||||||
|
ddns::Error::Database(_) => APIError::Internal,
|
||||||
ddns::Error::DatabaseConnection(e) => e.into(),
|
ddns::Error::DatabaseConnection(e) => e.into(),
|
||||||
ddns::Error::UpdateQueryFailed(_) => APIError::Internal,
|
ddns::Error::UpdateQueryFailed(_) => APIError::Internal,
|
||||||
ddns::Error::Database(_) => APIError::Internal,
|
|
||||||
ddns::Error::Unspecified => APIError::Unspecified,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue