Error cleanup

This commit is contained in:
Antoine Gersant 2022-11-21 16:06:18 -08:00
parent 4ec8f2161b
commit 1e9d307a05
5 changed files with 32 additions and 82 deletions

View file

@ -16,14 +16,6 @@ pub enum Error {
DatabaseConnection(#[from] db::Error),
#[error(transparent)]
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)]

View file

@ -11,6 +11,8 @@ use crate::db::{self, playlist_songs, playlists, users, DB};
#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error(transparent)]
Database(#[from] diesel::result::Error),
#[error(transparent)]
DatabaseConnection(#[from] db::Error),
#[error("User not found")]
@ -19,14 +21,6 @@ pub enum Error {
PlaylistNotFound,
#[error(transparent)]
Vfs(#[from] vfs::Error),
#[error("Unspecified")]
Unspecified,
}
impl From<anyhow::Error> for Error {
fn from(_: anyhow::Error) -> Self {
Error::Unspecified
}
}
#[derive(Clone)]
@ -49,8 +43,7 @@ impl Manager {
.filter(name.eq(owner))
.select((id,))
.first(&mut connection)
.optional()
.map_err(anyhow::Error::new)?
.optional()?
.ok_or(Error::UserNotFound)?
};
@ -58,8 +51,7 @@ impl Manager {
use self::playlists::dsl::*;
let found_playlists: Vec<String> = Playlist::belonging_to(&user)
.select(name)
.load(&mut connection)
.map_err(anyhow::Error::new)?;
.load(&mut connection)?;
Ok(found_playlists)
}
}
@ -84,8 +76,7 @@ impl Manager {
.filter(name.eq(owner))
.select((id,))
.first(&mut connection)
.optional()
.map_err(anyhow::Error::new)?
.optional()?
.ok_or(Error::UserNotFound)?
};
@ -97,16 +88,14 @@ impl Manager {
diesel::insert_into(playlists::table)
.values(&new_playlist)
.execute(&mut connection)
.map_err(anyhow::Error::new)?;
.execute(&mut connection)?;
playlist = {
use self::playlists::dsl::*;
playlists
.select((id, owner))
.filter(name.eq(playlist_name).and(owner.eq(user.id)))
.get_result(&mut connection)
.map_err(anyhow::Error::new)?
.get_result(&mut connection)?
}
}
@ -130,19 +119,17 @@ impl Manager {
{
let mut connection = self.db.connect()?;
connection
.transaction::<_, diesel::result::Error, _>(|connection| {
// Delete old content (if any)
let old_songs = PlaylistSong::belonging_to(&playlist);
diesel::delete(old_songs).execute(connection)?;
connection.transaction::<_, diesel::result::Error, _>(|connection| {
// Delete old content (if any)
let old_songs = PlaylistSong::belonging_to(&playlist);
diesel::delete(old_songs).execute(connection)?;
// Insert content
diesel::insert_into(playlist_songs::table)
.values(&new_songs)
.execute(&mut *connection)?; // TODO https://github.com/diesel-rs/diesel/issues/1822
Ok(())
})
.map_err(anyhow::Error::new)?;
// Insert content
diesel::insert_into(playlist_songs::table)
.values(&new_songs)
.execute(&mut *connection)?; // TODO https://github.com/diesel-rs/diesel/issues/1822
Ok(())
})?;
}
Ok(())
@ -162,8 +149,7 @@ impl Manager {
.filter(name.eq(owner))
.select((id,))
.first(&mut connection)
.optional()
.map_err(anyhow::Error::new)?
.optional()?
.ok_or(Error::UserNotFound)?
};
@ -174,8 +160,7 @@ impl Manager {
.select((id, owner))
.filter(name.eq(playlist_name).and(owner.eq(user.id)))
.get_result(&mut connection)
.optional()
.map_err(anyhow::Error::new)?
.optional()?
.ok_or(Error::PlaylistNotFound)?
};
@ -190,9 +175,7 @@ impl Manager {
"#,
);
let query = query.bind::<sql_types::Integer, _>(playlist.id);
songs = query
.get_results(&mut connection)
.map_err(anyhow::Error::new)?;
songs = query.get_results(&mut connection)?;
}
// Map real path to virtual paths
@ -213,18 +196,14 @@ impl Manager {
.filter(name.eq(owner))
.select((id,))
.first(&mut connection)
.optional()
.map_err(anyhow::Error::new)?
.optional()?
.ok_or(Error::UserNotFound)?
};
{
use self::playlists::dsl::*;
let q = Playlist::belonging_to(&user).filter(name.eq(playlist_name));
match diesel::delete(q)
.execute(&mut connection)
.map_err(anyhow::Error::new)?
{
match diesel::delete(q).execute(&mut connection)? {
0 => Err(Error::PlaylistNotFound),
_ => Ok(()),
}

View file

@ -20,14 +20,6 @@ pub enum Error {
IndexAlbumArtPatternInvalid,
#[error(transparent)]
Database(#[from] diesel::result::Error),
#[error("Unspecified")]
Unspecified,
}
impl From<anyhow::Error> for Error {
fn from(_: anyhow::Error) -> Self {
Error::Unspecified
}
}
#[derive(Clone, Default)]

View file

@ -16,14 +16,6 @@ pub enum Error {
DatabaseConnection(#[from] db::Error),
#[error(transparent)]
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)]
@ -123,16 +115,14 @@ impl Manager {
pub fn set_mount_dirs(&self, mount_dirs: &[MountDir]) -> Result<(), Error> {
let mut connection = self.db.connect()?;
connection
.transaction::<_, diesel::result::Error, _>(|connection| {
use self::mount_points::dsl::*;
diesel::delete(mount_points).execute(&mut *connection)?;
diesel::insert_into(mount_points)
.values(mount_dirs)
.execute(&mut *connection)?; // TODO https://github.com/diesel-rs/diesel/issues/1822
Ok(())
})
.map_err(anyhow::Error::new)?;
connection.transaction::<_, diesel::result::Error, _>(|connection| {
use self::mount_points::dsl::*;
diesel::delete(mount_points).execute(&mut *connection)?;
diesel::insert_into(mount_points)
.values(mount_dirs)
.execute(&mut *connection)?; // TODO https://github.com/diesel-rs/diesel/issues/1822
Ok(())
})?;
Ok(())
}
}

View file

@ -62,11 +62,11 @@ impl From<config::Error> for APIError {
impl From<playlist::Error> for APIError {
fn from(error: playlist::Error) -> APIError {
match error {
playlist::Error::Database(_) => APIError::Internal,
playlist::Error::DatabaseConnection(e) => e.into(),
playlist::Error::PlaylistNotFound => APIError::PlaylistNotFound,
playlist::Error::UserNotFound => APIError::UserNotFound,
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::IndexAlbumArtPatternInvalid => 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::Database(_) => APIError::Internal,
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 {
fn from(error: ddns::Error) -> APIError {
match error {
ddns::Error::Database(_) => APIError::Internal,
ddns::Error::DatabaseConnection(e) => e.into(),
ddns::Error::UpdateQueryFailed(_) => APIError::Internal,
ddns::Error::Database(_) => APIError::Internal,
ddns::Error::Unspecified => APIError::Unspecified,
}
}
}