Fixed play command
This commit is contained in:
96
src/error.rs
Normal file
96
src/error.rs
Normal file
@@ -0,0 +1,96 @@
|
||||
use std::fmt;
|
||||
use diesel::result::Error as DieselError;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
pub type SirenResult<T> = Result<T, Error>;
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub struct Error {
|
||||
pub status: u16,
|
||||
pub message: String,
|
||||
}
|
||||
|
||||
impl Error {
|
||||
pub fn new(error_status_code: u16, error_message: String) -> Self {
|
||||
Self {
|
||||
status: error_status_code,
|
||||
message: error_message,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Error {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
f.write_str(self.message.as_str())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<std::io::Error> for Error {
|
||||
fn from(error: std::io::Error) -> Self {
|
||||
Self::new(500, format!("Unknown io error: {}", error))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<std::string::FromUtf8Error> for Error {
|
||||
fn from(error: std::string::FromUtf8Error) -> Self {
|
||||
Self::new(500, format!("Unknown from utf8 error: {}", error))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<DieselError> for Error {
|
||||
fn from(error: DieselError) -> Self {
|
||||
match error {
|
||||
DieselError::DatabaseError(kind, err) => match kind {
|
||||
diesel::result::DatabaseErrorKind::UniqueViolation => {
|
||||
Self::new(409, err.message().to_string())
|
||||
}
|
||||
_ => Self::new(500, err.message().to_string()),
|
||||
},
|
||||
DieselError::NotFound => Self::new(404, "The record was not found".to_string()),
|
||||
DieselError::SerializationError(err) => Self::new(422, err.to_string()),
|
||||
err => Self::new(500, format!("Unknown database error: {}", err)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<reqwest::Error> for Error {
|
||||
fn from(error: reqwest::Error) -> Self {
|
||||
Self::new(500, format!("Unknown reqwest error: {}", error))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<serde_json::Error> for Error {
|
||||
fn from(error: serde_json::Error) -> Self {
|
||||
Self::new(500, format!("Unknown serde_json error: {}", error))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<serenity::Error> for Error {
|
||||
fn from(error: serenity::Error) -> Self {
|
||||
Self::new(500, format!("Unknown serenity error: {}", error))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<redis::RedisError> for Error {
|
||||
fn from(error: redis::RedisError) -> Self {
|
||||
Self::new(500, format!("Unknown redis error: {}", error))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<uuid::Error> for Error {
|
||||
fn from(error: uuid::Error) -> Self {
|
||||
Self::new(500, format!("Unknown uuid error: {}", error))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<std::env::VarError> for Error {
|
||||
fn from(error: std::env::VarError) -> Self {
|
||||
Self::new(500, format!("Unknown env error: {}", error))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<songbird::error::JoinError> for Error {
|
||||
fn from(error: songbird::error::JoinError) -> Self {
|
||||
Self::new(500, format!("Unable to join channel: {}", error))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user