Merge the Errors into a single Enum.

This commit is contained in:
Jordan Petridis 2018-02-05 01:39:50 +02:00
parent 0892fe26ba
commit ede4c21e30
No known key found for this signature in database
GPG Key ID: CEABAD9F5683B9A6
6 changed files with 65 additions and 68 deletions

View File

@ -7,8 +7,7 @@ use diesel::r2d2::ConnectionManager;
use std::io;
use std::path::PathBuf;
use errors::DatabaseError;
use failure::Error;
use errors::DataError;
#[cfg(not(test))]
use xdg_dirs;
@ -58,7 +57,7 @@ fn init_pool(db_path: &str) -> Pool {
pool
}
fn run_migration_on(connection: &SqliteConnection) -> Result<(), DatabaseError> {
fn run_migration_on(connection: &SqliteConnection) -> Result<(), DataError> {
info!("Running DB Migrations...");
// embedded_migrations::run(connection)?;
embedded_migrations::run_with_output(connection, &mut io::stdout()).map_err(From::from)
@ -67,7 +66,7 @@ fn run_migration_on(connection: &SqliteConnection) -> Result<(), DatabaseError>
/// Reset the database into a clean state.
// Test share a Temp file db.
#[allow(dead_code)]
pub fn truncate_db() -> Result<(), DatabaseError> {
pub fn truncate_db() -> Result<(), DataError> {
let db = connection();
let con = db.get()?;
con.execute("DELETE FROM episode")?;

View File

@ -6,17 +6,16 @@ use diesel::prelude::*;
use diesel;
use diesel::dsl::exists;
use diesel::select;
use failure::Error;
use database::connection;
use errors::DataError;
use models::*;
// use errors::DatabaseError;
// Feel free to open a Merge request that manually replaces Result<T> if you feel bored.
use std::result;
type Result<T> = result::Result<T, Error>;
type DatabaseResult<T> = result::Result<T, DataError>;
pub fn get_sources() -> Result<Vec<Source>> {
pub fn get_sources() -> DatabaseResult<Vec<Source>> {
use schema::source::dsl::*;
let db = connection();
let con = db.get()?;
@ -27,7 +26,7 @@ pub fn get_sources() -> Result<Vec<Source>> {
.map_err(From::from)
}
pub fn get_podcasts() -> Result<Vec<Podcast>> {
pub fn get_podcasts() -> DatabaseResult<Vec<Podcast>> {
use schema::podcast::dsl::*;
let db = connection();
let con = db.get()?;
@ -38,7 +37,7 @@ pub fn get_podcasts() -> Result<Vec<Podcast>> {
.map_err(From::from)
}
pub fn get_episodes() -> Result<Vec<Episode>> {
pub fn get_episodes() -> DatabaseResult<Vec<Episode>> {
use schema::episode::dsl::*;
let db = connection();
let con = db.get()?;
@ -49,7 +48,7 @@ pub fn get_episodes() -> Result<Vec<Episode>> {
.map_err(From::from)
}
pub(crate) fn get_downloaded_episodes() -> Result<Vec<EpisodeCleanerQuery>> {
pub(crate) fn get_downloaded_episodes() -> DatabaseResult<Vec<EpisodeCleanerQuery>> {
use schema::episode::dsl::*;
let db = connection();
let con = db.get()?;
@ -61,7 +60,7 @@ pub(crate) fn get_downloaded_episodes() -> Result<Vec<EpisodeCleanerQuery>> {
.map_err(From::from)
}
// pub(crate) fn get_played_episodes() -> Result<Vec<Episode>> {
// pub(crate) fn get_played_episodes() -> DatabaseResult<Vec<Episode>> {
// use schema::episode::dsl::*;
// let db = connection();
@ -72,7 +71,7 @@ pub(crate) fn get_downloaded_episodes() -> Result<Vec<EpisodeCleanerQuery>> {
// .map_err(From::from)
// }
pub(crate) fn get_played_cleaner_episodes() -> Result<Vec<EpisodeCleanerQuery>> {
pub(crate) fn get_played_cleaner_episodes() -> DatabaseResult<Vec<EpisodeCleanerQuery>> {
use schema::episode::dsl::*;
let db = connection();
let con = db.get()?;
@ -84,7 +83,7 @@ pub(crate) fn get_played_cleaner_episodes() -> Result<Vec<EpisodeCleanerQuery>>
.map_err(From::from)
}
pub fn get_episode_from_rowid(ep_id: i32) -> Result<Episode> {
pub fn get_episode_from_rowid(ep_id: i32) -> DatabaseResult<Episode> {
use schema::episode::dsl::*;
let db = connection();
let con = db.get()?;
@ -95,7 +94,7 @@ pub fn get_episode_from_rowid(ep_id: i32) -> Result<Episode> {
.map_err(From::from)
}
pub fn get_episode_local_uri_from_id(ep_id: i32) -> Result<Option<String>> {
pub fn get_episode_local_uri_from_id(ep_id: i32) -> DatabaseResult<Option<String>> {
use schema::episode::dsl::*;
let db = connection();
let con = db.get()?;
@ -107,7 +106,7 @@ pub fn get_episode_local_uri_from_id(ep_id: i32) -> Result<Option<String>> {
.map_err(From::from)
}
pub fn get_episodes_widgets_with_limit(limit: u32) -> Result<Vec<EpisodeWidgetQuery>> {
pub fn get_episodes_widgets_with_limit(limit: u32) -> DatabaseResult<Vec<EpisodeWidgetQuery>> {
use schema::episode;
let db = connection();
let con = db.get()?;
@ -130,7 +129,7 @@ pub fn get_episodes_widgets_with_limit(limit: u32) -> Result<Vec<EpisodeWidgetQu
.map_err(From::from)
}
pub fn get_podcast_from_id(pid: i32) -> Result<Podcast> {
pub fn get_podcast_from_id(pid: i32) -> DatabaseResult<Podcast> {
use schema::podcast::dsl::*;
let db = connection();
let con = db.get()?;
@ -141,7 +140,7 @@ pub fn get_podcast_from_id(pid: i32) -> Result<Podcast> {
.map_err(From::from)
}
pub fn get_podcast_cover_from_id(pid: i32) -> Result<PodcastCoverQuery> {
pub fn get_podcast_cover_from_id(pid: i32) -> DatabaseResult<PodcastCoverQuery> {
use schema::podcast::dsl::*;
let db = connection();
let con = db.get()?;
@ -153,7 +152,7 @@ pub fn get_podcast_cover_from_id(pid: i32) -> Result<PodcastCoverQuery> {
.map_err(From::from)
}
pub fn get_pd_episodes(parent: &Podcast) -> Result<Vec<Episode>> {
pub fn get_pd_episodes(parent: &Podcast) -> DatabaseResult<Vec<Episode>> {
use schema::episode::dsl::*;
let db = connection();
let con = db.get()?;
@ -164,7 +163,7 @@ pub fn get_pd_episodes(parent: &Podcast) -> Result<Vec<Episode>> {
.map_err(From::from)
}
pub fn get_pd_episodeswidgets(parent: &Podcast) -> Result<Vec<EpisodeWidgetQuery>> {
pub fn get_pd_episodeswidgets(parent: &Podcast) -> DatabaseResult<Vec<EpisodeWidgetQuery>> {
use schema::episode::dsl::*;
let db = connection();
let con = db.get()?;
@ -177,7 +176,7 @@ pub fn get_pd_episodeswidgets(parent: &Podcast) -> Result<Vec<EpisodeWidgetQuery
.map_err(From::from)
}
pub fn get_pd_unplayed_episodes(parent: &Podcast) -> Result<Vec<Episode>> {
pub fn get_pd_unplayed_episodes(parent: &Podcast) -> DatabaseResult<Vec<Episode>> {
use schema::episode::dsl::*;
let db = connection();
let con = db.get()?;
@ -189,8 +188,8 @@ pub fn get_pd_unplayed_episodes(parent: &Podcast) -> Result<Vec<Episode>> {
.map_err(From::from)
}
// pub(crate) fn get_pd_episodes_limit(parent: &Podcast, limit: u32) -> Result<Vec<Episode>> {
// use schema::episode::dsl::*;
// pub(crate) fn get_pd_episodes_limit(parent: &Podcast, limit: u32) ->
// DatabaseResult<Vec<Episode>> { use schema::episode::dsl::*;
// let db = connection();
// let con = db.get()?;
@ -202,7 +201,7 @@ pub fn get_pd_unplayed_episodes(parent: &Podcast) -> Result<Vec<Episode>> {
// .map_err(From::from)
// }
pub fn get_source_from_uri(uri_: &str) -> Result<Source> {
pub fn get_source_from_uri(uri_: &str) -> DatabaseResult<Source> {
use schema::source::dsl::*;
let db = connection();
let con = db.get()?;
@ -213,7 +212,7 @@ pub fn get_source_from_uri(uri_: &str) -> Result<Source> {
.map_err(From::from)
}
pub fn get_source_from_id(id_: i32) -> Result<Source> {
pub fn get_source_from_id(id_: i32) -> DatabaseResult<Source> {
use schema::source::dsl::*;
let db = connection();
let con = db.get()?;
@ -224,7 +223,7 @@ pub fn get_source_from_id(id_: i32) -> Result<Source> {
.map_err(From::from)
}
pub fn get_podcast_from_source_id(sid: i32) -> Result<Podcast> {
pub fn get_podcast_from_source_id(sid: i32) -> DatabaseResult<Podcast> {
use schema::podcast::dsl::*;
let db = connection();
let con = db.get()?;
@ -235,7 +234,7 @@ pub fn get_podcast_from_source_id(sid: i32) -> Result<Podcast> {
.map_err(From::from)
}
pub fn get_episode_from_pk(title_: &str, pid: i32) -> Result<Episode> {
pub fn get_episode_from_pk(title_: &str, pid: i32) -> DatabaseResult<Episode> {
use schema::episode::dsl::*;
let db = connection();
let con = db.get()?;
@ -247,7 +246,10 @@ pub fn get_episode_from_pk(title_: &str, pid: i32) -> Result<Episode> {
.map_err(From::from)
}
pub(crate) fn get_episode_minimal_from_pk(title_: &str, pid: i32) -> Result<EpisodeMinimal> {
pub(crate) fn get_episode_minimal_from_pk(
title_: &str,
pid: i32,
) -> DatabaseResult<EpisodeMinimal> {
use schema::episode::dsl::*;
let db = connection();
let con = db.get()?;
@ -260,11 +262,11 @@ pub(crate) fn get_episode_minimal_from_pk(title_: &str, pid: i32) -> Result<Epis
.map_err(From::from)
}
pub(crate) fn remove_feed(pd: &Podcast) -> Result<()> {
pub(crate) fn remove_feed(pd: &Podcast) -> DatabaseResult<()> {
let db = connection();
let con = db.get()?;
con.transaction(|| -> Result<()> {
con.transaction(|| {
delete_source(&con, pd.source_id())?;
delete_podcast(&con, pd.id())?;
delete_podcast_episodes(&con, pd.id())?;
@ -291,7 +293,7 @@ fn delete_podcast_episodes(con: &SqliteConnection, parent_id: i32) -> QueryResul
diesel::delete(episode.filter(podcast_id.eq(parent_id))).execute(con)
}
pub fn source_exists(url: &str) -> Result<bool> {
pub fn source_exists(url: &str) -> DatabaseResult<bool> {
use schema::source::dsl::*;
let db = connection();
@ -302,7 +304,7 @@ pub fn source_exists(url: &str) -> Result<bool> {
.map_err(From::from)
}
pub(crate) fn podcast_exists(source_id_: i32) -> Result<bool> {
pub(crate) fn podcast_exists(source_id_: i32) -> DatabaseResult<bool> {
use schema::podcast::dsl::*;
let db = connection();
@ -314,7 +316,7 @@ pub(crate) fn podcast_exists(source_id_: i32) -> Result<bool> {
}
#[cfg_attr(rustfmt, rustfmt_skip)]
pub(crate) fn episode_exists(title_: &str, podcast_id_: i32) -> Result<bool> {
pub(crate) fn episode_exists(title_: &str, podcast_id_: i32) -> DatabaseResult<bool> {
use schema::episode::dsl::*;
let db = connection();
@ -325,7 +327,7 @@ pub(crate) fn episode_exists(title_: &str, podcast_id_: i32) -> Result<bool> {
.map_err(From::from)
}
pub(crate) fn index_new_episodes(eps: &[NewEpisode]) -> Result<()> {
pub(crate) fn index_new_episodes(eps: &[NewEpisode]) -> DatabaseResult<()> {
use schema::episode::dsl::*;
let db = connection();
let con = db.get()?;
@ -337,13 +339,13 @@ pub(crate) fn index_new_episodes(eps: &[NewEpisode]) -> Result<()> {
.map(|_| ())
}
pub fn update_none_to_played_now(parent: &Podcast) -> Result<usize> {
pub fn update_none_to_played_now(parent: &Podcast) -> DatabaseResult<usize> {
use schema::episode::dsl::*;
let db = connection();
let con = db.get()?;
let epoch_now = Utc::now().timestamp() as i32;
con.transaction(|| -> Result<usize> {
con.transaction(|| {
diesel::update(Episode::belonging_to(parent).filter(played.is_null()))
.set(played.eq(Some(epoch_now)))
.execute(&con)

View File

@ -20,7 +20,7 @@ struct IOError(io::Error);
// struct RSSError(rss::Error);
#[derive(Fail, Debug)]
pub enum DatabaseError {
pub enum DataError {
#[fail(display = "SQL Query failed: {}", _0)]
DieselResultError(#[cause] diesel::result::Error),
#[fail(display = "Database Migration error: {}", _0)]
@ -29,34 +29,6 @@ pub enum DatabaseError {
R2D2Error(#[cause] r2d2::Error),
#[fail(display = "R2D2 Pool error: {}", _0)]
R2D2PoolError(#[cause] r2d2::PoolError),
}
impl From<RunMigrationsError> for DatabaseError {
fn from(err: RunMigrationsError) -> Self {
DatabaseError::DieselMigrationError(err)
}
}
impl From<diesel::result::Error> for DatabaseError {
fn from(err: diesel::result::Error) -> Self {
DatabaseError::DieselResultError(err)
}
}
impl From<r2d2::Error> for DatabaseError {
fn from(err: r2d2::Error) -> Self {
DatabaseError::R2D2Error(err)
}
}
impl From<r2d2::PoolError> for DatabaseError {
fn from(err: r2d2::PoolError) -> Self {
DatabaseError::R2D2PoolError(err)
}
}
#[derive(Fail, Debug)]
pub enum HttpError {
#[fail(display = "Reqwest Error: {}", _0)]
ReqError(#[cause] reqwest::Error),
#[fail(display = "Hyper Error: {}", _0)]
@ -66,3 +38,27 @@ pub enum HttpError {
#[fail(display = "TLS Error: {}", _0)]
TLSError(#[cause] native_tls::Error),
}
impl From<RunMigrationsError> for DataError {
fn from(err: RunMigrationsError) -> Self {
DataError::DieselMigrationError(err)
}
}
impl From<diesel::result::Error> for DataError {
fn from(err: diesel::result::Error) -> Self {
DataError::DieselResultError(err)
}
}
impl From<r2d2::Error> for DataError {
fn from(err: r2d2::Error) -> Self {
DataError::R2D2Error(err)
}
}
impl From<r2d2::PoolError> for DataError {
fn from(err: r2d2::PoolError) -> Self {
DataError::R2D2PoolError(err)
}
}

View File

@ -120,7 +120,7 @@ impl NewEpisode {
#[allow(dead_code)]
pub(crate) fn to_episode(&self) -> Result<Episode, Error> {
self.index()?;
dbqueries::get_episode_from_pk(&self.title, self.podcast_id)
dbqueries::get_episode_from_pk(&self.title, self.podcast_id).map_err(From::from)
}
}

View File

@ -47,6 +47,6 @@ impl NewSource {
// Look out for when tryinto lands into stable.
pub(crate) fn to_source(&self) -> Result<Source, Error> {
self.insert_or_ignore()?;
dbqueries::get_source_from_uri(&self.uri)
dbqueries::get_source_from_uri(&self.uri).map_err(From::from)
}
}

View File

@ -7,7 +7,7 @@ pub enum DownloaderError {
#[fail(display = "Reqwest error: {}", _0)]
RequestError(reqwest::Error),
#[fail(display = "Data error: {}", _0)]
DataError(hammond_data::errors::DatabaseError),
DataError(hammond_data::errors::DataError),
#[fail(display = "Io error: {}", _0)]
IoError(io::Error),
}