diff options
author | Dennis Kobert <dennis@kobert.dev> | 2020-03-03 16:16:03 +0100 |
---|---|---|
committer | Dennis Kobert <dennis@kobert.dev> | 2020-03-03 16:16:03 +0100 |
commit | 9f8f49283d5932de91d189cd5a72309807ece00b (patch) | |
tree | a0db9fd1c9a1d41fa594151da584bbb60758bca4 /src/errors.rs | |
parent | aee5ff0b295b3af68dfd0b78643ab5b7f41429a7 (diff) |
Restructure the project
Diffstat (limited to 'src/errors.rs')
-rw-r--r-- | src/errors.rs | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/src/errors.rs b/src/errors.rs new file mode 100644 index 0000000..d9893ba --- /dev/null +++ b/src/errors.rs @@ -0,0 +1,64 @@ +use rocket::http::ContentType; +use rocket::request::Request; +use rocket::response::{self, Responder, Response}; +use rspotify::spotify::client::ApiError; +use rspotify::spotify::oauth2::SpotifyOAuth; +use std::io::Cursor; +use std::sync::{MutexGuard, PoisonError}; +use tokio_postgres::error::Error as DbError; + +#[derive(Debug)] +pub enum Error { + Postgres(DbError), + Spotify(ApiError), + Misc(String), +} +impl From<DbError> for Error { + fn from(error: DbError) -> Self { + Error::Postgres(error) + } +} +impl From<ApiError> for Error { + fn from(error: ApiError) -> Self { + Error::Spotify(error) + } +} +impl From<&str> for Error { + fn from(error: &str) -> Self { + Error::Misc(error.to_owned()) + } +} +impl From<String> for Error { + fn from(error: String) -> Self { + Error::Misc(error) + } +} +impl<'a> From<PoisonError<MutexGuard<'a, HashMap<String, SpotifyOAuth>>>> for Error { + fn from(error: PoisonError<MutexGuard<'a, HashMap<String, SpotifyOAuth>>>) -> Self { + Error::Misc(format!("failed to lock the client mutex: {:?}", error)) + } +} +impl<'a> From<PoisonError<MutexGuard<'a, postgres::Client>>> for Error { + fn from(error: PoisonError<MutexGuard<'a, postgres::Client>>) -> Self { + Error::Misc(format!("failed to lock the client mutex: {:?}", error)) + } +} +impl<'a> From<std::option::NoneError> for Error { + fn from(error: std::option::NoneError) -> Self { + Error::Misc(format!("tried to unwrap none at: {:?}", error)) + } +} +impl<'a> Responder<'a> for Error { + fn respond_to(self, _: &Request) -> response::Result<'a> { + let response = match self { + Error::Postgres(e) => format!("DB Error: {:?}", e), + Error::Spotify(e) => format!("Spotify Error: {:?}", e), + Error::Misc(e) => format!("Error: {}", e), + }; + Response::build() + .header(ContentType::Plain) + .status(rocket::http::Status::raw(500)) + .sized_body(Cursor::new(response)) + .ok() + } +} |