chore (backend-rs): improve error messages
refs: https://rust-lang.github.io/api-guidelines/interoperability.html https://github.com/rust-lang/project-error-handling/issues/27
This commit is contained in:
parent
1cc24b9c25
commit
9e6e7733a3
16 changed files with 83 additions and 66 deletions
|
@ -15,11 +15,11 @@ pub enum Category {
|
|||
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
pub enum Error {
|
||||
#[error("Redis error: {0}")]
|
||||
#[error("failed to execute Redis command")]
|
||||
Redis(#[from] RedisError),
|
||||
#[error("Redis connection error: {0}")]
|
||||
#[error("bad Redis connection")]
|
||||
RedisConn(#[from] RedisConnError),
|
||||
#[error("Failed to encode the data: {0}")]
|
||||
#[error("failed to encode data for Redis")]
|
||||
Encode(#[from] rmp_serde::encode::Error),
|
||||
}
|
||||
|
||||
|
|
|
@ -81,9 +81,9 @@ async fn init_conn_pool() -> Result<(), RedisError> {
|
|||
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
pub enum RedisConnError {
|
||||
#[error("Failed to initialize Redis connection pool: {0}")]
|
||||
#[error("failed to initialize Redis connection pool")]
|
||||
Redis(RedisError),
|
||||
#[error("Redis connection pool error: {0}")]
|
||||
#[error("bad Redis connection pool")]
|
||||
Bb8Pool(RunError<RedisError>),
|
||||
}
|
||||
|
||||
|
|
|
@ -8,6 +8,7 @@ pub struct Acct {
|
|||
}
|
||||
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
#[doc = "Error type to indicate a string-to-[`Acct`] conversion failure"]
|
||||
#[error("failed to convert string '{0}' into acct")]
|
||||
pub struct InvalidAcctString(String);
|
||||
|
||||
|
|
|
@ -9,17 +9,18 @@ use serde::Deserialize;
|
|||
/// Errors that can occur while fetching NodeInfo from a remote server
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
pub enum Error {
|
||||
#[error("HTTP client aquisition error: {0}")]
|
||||
#[error("failed to acquire an HTTP client")]
|
||||
HttpClient(#[from] http_client::Error),
|
||||
#[error("HTTP error: {0}")]
|
||||
#[error("HTTP request failed")]
|
||||
Http(#[from] isahc::Error),
|
||||
#[error("Bad status: {0}")]
|
||||
#[doc = "bad HTTP status"]
|
||||
#[error("bad HTTP status ({0})")]
|
||||
BadStatus(String),
|
||||
#[error("Failed to parse response body as text: {0}")]
|
||||
#[error("failed to parse HTTP response body as text")]
|
||||
Response(#[from] std::io::Error),
|
||||
#[error("Failed to parse response body as json: {0}")]
|
||||
#[error("failed to parse HTTP response body as json")]
|
||||
Json(#[from] serde_json::Error),
|
||||
#[error("No nodeinfo provided")]
|
||||
#[error("nodeinfo is missing")]
|
||||
MissingNodeinfo,
|
||||
}
|
||||
|
||||
|
|
|
@ -154,9 +154,10 @@ pub async fn nodeinfo_2_0() -> Result<Nodeinfo20, DbErr> {
|
|||
#[cfg(feature = "napi")]
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
pub enum Error {
|
||||
#[error("Database error: {0}")]
|
||||
#[doc = "database error"]
|
||||
#[error(transparent)]
|
||||
Db(#[from] DbErr),
|
||||
#[error("Failed to serialize nodeinfo into JSON: {0}")]
|
||||
#[error("failed to serialize nodeinfo into JSON")]
|
||||
Json(#[from] serde_json::Error),
|
||||
}
|
||||
|
||||
|
|
|
@ -4,11 +4,12 @@
|
|||
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
pub enum Error {
|
||||
#[error("Idna error: {0}")]
|
||||
#[doc = "UTS #46 process has failed"]
|
||||
#[error(transparent)]
|
||||
Idna(#[from] idna::Errors),
|
||||
#[error("Url parse error: {0}")]
|
||||
#[error("failed to parse a URL")]
|
||||
UrlParse(#[from] url::ParseError),
|
||||
#[error("Hostname is missing")]
|
||||
#[error("hostname is missing")]
|
||||
NoHostname,
|
||||
}
|
||||
|
||||
|
|
|
@ -7,23 +7,26 @@ use tokio::sync::Mutex;
|
|||
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
pub enum Error {
|
||||
#[error("Redis cache error: {0}")]
|
||||
#[error("Redis cache operation has failed")]
|
||||
Cache(#[from] cache::Error),
|
||||
#[error("HTTP client aquisition error: {0}")]
|
||||
#[error("failed to acquire an HTTP client")]
|
||||
HttpClient(#[from] http_client::Error),
|
||||
#[error("Isahc error: {0}")]
|
||||
#[error("HTTP request failed")]
|
||||
Isahc(#[from] isahc::Error),
|
||||
#[error("HTTP error: {0}")]
|
||||
Http(String),
|
||||
#[error("Image decoding error: {0}")]
|
||||
#[doc = "bad HTTP status"]
|
||||
#[error("bad HTTP status ({0})")]
|
||||
BadStatus(String),
|
||||
#[error("failed to decode an image")]
|
||||
Image(#[from] ImageError),
|
||||
#[error("Image decoding error: {0}")]
|
||||
#[error("failed to decode an image")]
|
||||
Io(#[from] std::io::Error),
|
||||
#[error("Exif extraction error: {0}")]
|
||||
#[error("failed to extract the exif data")]
|
||||
Exif(#[from] nom_exif::Error),
|
||||
#[error("Emoji meta attempt limit exceeded: {0}")]
|
||||
#[doc = "too many fetch attempts"]
|
||||
#[error("too many fetch attempts for {0}")]
|
||||
TooManyAttempts(String),
|
||||
#[error("Unsupported image type: {0}")]
|
||||
#[doc = "unsupported image type"]
|
||||
#[error("unsupported image type ({0})")]
|
||||
UnsupportedImage(String),
|
||||
}
|
||||
|
||||
|
@ -75,7 +78,11 @@ pub async fn get_image_size_from_url(url: &str) -> Result<ImageSize, Error> {
|
|||
if !response.status().is_success() {
|
||||
tracing::info!("status: {}", response.status());
|
||||
tracing::debug!("response body: {:#?}", response.body());
|
||||
return Err(Error::Http(format!("Failed to get image from {}", url)));
|
||||
return Err(Error::BadStatus(format!(
|
||||
"{} returned {}",
|
||||
url,
|
||||
response.status()
|
||||
)));
|
||||
}
|
||||
|
||||
let image_bytes = response.bytes().await?;
|
||||
|
|
|
@ -6,17 +6,18 @@ use serde::Deserialize;
|
|||
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
pub enum Error {
|
||||
#[error("Cache error: {0}")]
|
||||
#[error("Redis cache operation has failed")]
|
||||
Cache(#[from] cache::Error),
|
||||
#[error("Isahc error: {0}")]
|
||||
#[error("HTTP request failed")]
|
||||
Isahc(#[from] isahc::Error),
|
||||
#[error("HTTP client aquisition error: {0}")]
|
||||
#[error("failed to acquire an HTTP client")]
|
||||
HttpClient(#[from] http_client::Error),
|
||||
#[error("HTTP error: {0}")]
|
||||
Http(String),
|
||||
#[error("Response parsing error: {0}")]
|
||||
#[doc = "firefish.dev returned bad HTTP status"]
|
||||
#[error("firefish.dev returned bad HTTP status ({0})")]
|
||||
BadStatus(String),
|
||||
#[error("failed to parse the HTTP response")]
|
||||
Io(#[from] std::io::Error),
|
||||
#[error("Failed to deserialize JSON: {0}")]
|
||||
#[error("failed to parse the HTTP response as JSON")]
|
||||
Json(#[from] serde_json::Error),
|
||||
}
|
||||
|
||||
|
@ -36,9 +37,7 @@ async fn get_latest_version() -> Result<String, Error> {
|
|||
if !response.status().is_success() {
|
||||
tracing::info!("status: {}", response.status());
|
||||
tracing::debug!("response body: {:#?}", response.body());
|
||||
return Err(Error::Http(
|
||||
"Failed to fetch version from Firefish GitLab".to_string(),
|
||||
));
|
||||
return Err(Error::BadStatus(response.status().to_string()));
|
||||
}
|
||||
|
||||
let res_parsed: Response = serde_json::from_str(&response.text().await?)?;
|
||||
|
|
|
@ -17,12 +17,12 @@ pub fn hash_password(password: &str) -> Result<String, password_hash::errors::Er
|
|||
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
pub enum Error {
|
||||
#[error("An error occured while bcrypt verification: {0}")]
|
||||
#[error("failed to verify password against bcrypt hash")]
|
||||
Bcrypt(#[from] bcrypt::BcryptError),
|
||||
#[error("Invalid argon2 password hash: {0}")]
|
||||
InvalidArgon2Hash(#[from] password_hash::Error),
|
||||
#[error("An error occured while argon2 verification: {0}")]
|
||||
#[error("failed to verify password against argon2 hash")]
|
||||
Argon2(#[from] argon2::Error),
|
||||
#[error("invalid argon2 password hash")]
|
||||
InvalidArgon2Hash(#[from] password_hash::Error),
|
||||
}
|
||||
|
||||
/// Checks whether the given password and hash match.
|
||||
|
|
|
@ -55,9 +55,10 @@ pub fn count_reactions(reactions: &HashMap<String, u32>) -> HashMap<String, u32>
|
|||
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
pub enum Error {
|
||||
#[error("Idna error: {0}")]
|
||||
#[doc = "UTS #46 process has failed"]
|
||||
#[error(transparent)]
|
||||
Idna(#[from] idna::Errors),
|
||||
#[error("Database error: {0}")]
|
||||
#[error(transparent)]
|
||||
Db(#[from] DbErr),
|
||||
}
|
||||
|
||||
|
|
|
@ -8,9 +8,10 @@ use sea_orm::{prelude::*, QuerySelect};
|
|||
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
pub enum AntennaCheckError {
|
||||
#[error("Database error: {0}")]
|
||||
#[doc = "database error"]
|
||||
#[error(transparent)]
|
||||
Db(#[from] DbErr),
|
||||
#[error("Cache error: {0}")]
|
||||
#[error("Redis cache operation has failed")]
|
||||
Cache(#[from] cache::Error),
|
||||
}
|
||||
|
||||
|
|
|
@ -15,19 +15,21 @@ use sea_orm::prelude::*;
|
|||
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
pub enum Error {
|
||||
#[error("Database error: {0}")]
|
||||
#[doc = "database error"]
|
||||
#[error(transparent)]
|
||||
Db(#[from] DbErr),
|
||||
#[error("Cache error: {0}")]
|
||||
#[error("Redis cache operation has failed")]
|
||||
Cache(#[from] cache::Error),
|
||||
#[error("Redis error: {0}")]
|
||||
#[error("failed to execute a Redis command")]
|
||||
Redis(#[from] RedisError),
|
||||
#[error("Redis connection error: {0}")]
|
||||
#[error("bad Redis connection")]
|
||||
RedisConn(#[from] RedisConnError),
|
||||
#[error("Invalid ID: {0}")]
|
||||
#[doc = "provided string is not a valid Firefish ID"]
|
||||
#[error(transparent)]
|
||||
InvalidId(#[from] InvalidIdError),
|
||||
#[error("Stream error: {0}")]
|
||||
#[error("Redis stream operation has failed")]
|
||||
Stream(#[from] stream::Error),
|
||||
#[error("Failed to check if the note should be added to antenna: {0}")]
|
||||
#[error("failed to check if the note should be added to antenna")]
|
||||
AntennaCheck(#[from] AntennaCheckError),
|
||||
}
|
||||
|
||||
|
|
|
@ -9,15 +9,17 @@ use web_push::*;
|
|||
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
pub enum Error {
|
||||
#[error("Database error: {0}")]
|
||||
#[doc = "database error"]
|
||||
#[error(transparent)]
|
||||
Db(#[from] DbErr),
|
||||
#[error("Web Push error: {0}")]
|
||||
#[error("web push has failed")]
|
||||
WebPush(#[from] WebPushError),
|
||||
#[error("Failed to (de)serialize an object: {0}")]
|
||||
#[error("failed to (de)serialize an object")]
|
||||
Serialize(#[from] serde_json::Error),
|
||||
#[error("Invalid content: {0}")]
|
||||
#[doc = "provided content is invalid"]
|
||||
#[error("invalid content ({0})")]
|
||||
InvalidContent(String),
|
||||
#[error("HTTP client aquisition error: {0}")]
|
||||
#[error("failed to acquire an HTTP client")]
|
||||
HttpClient(#[from] http_client::Error),
|
||||
}
|
||||
|
||||
|
|
|
@ -62,14 +62,14 @@ pub enum ChatEvent {
|
|||
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
pub enum Error {
|
||||
#[error("Redis error: {0}")]
|
||||
#[error("failed to execute a Redis command")]
|
||||
Redis(#[from] RedisError),
|
||||
#[error("Redis connection error: {0}")]
|
||||
#[error("bad Redis connection")]
|
||||
RedisConn(#[from] RedisConnError),
|
||||
#[error("Json (de)serialization error: {0}")]
|
||||
#[error("failed to (de)serialize object")]
|
||||
Json(#[from] serde_json::Error),
|
||||
#[error("Value error: {0}")]
|
||||
Value(String),
|
||||
#[error("invalid content")]
|
||||
InvalidContent,
|
||||
}
|
||||
|
||||
pub async fn publish_to_stream(
|
||||
|
@ -104,7 +104,7 @@ pub async fn publish_to_stream(
|
|||
value.unwrap_or_else(|| "null".to_string()),
|
||||
)
|
||||
} else {
|
||||
value.ok_or(Error::Value("Invalid streaming message".to_string()))?
|
||||
value.ok_or(Error::InvalidContent)?
|
||||
};
|
||||
|
||||
redis_conn()
|
||||
|
|
|
@ -7,9 +7,9 @@ use std::time::Duration;
|
|||
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
pub enum Error {
|
||||
#[error("Isahc error: {0}")]
|
||||
#[error("HTTP request failed")]
|
||||
Isahc(#[from] isahc::Error),
|
||||
#[error("Url parse error: {0}")]
|
||||
#[error("invalid URL")]
|
||||
UrlParse(#[from] isahc::http::uri::InvalidUri),
|
||||
}
|
||||
|
||||
|
|
|
@ -46,7 +46,8 @@ fn create_id(datetime: &NaiveDateTime) -> String {
|
|||
}
|
||||
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
#[error("Invalid ID: {id}")]
|
||||
#[doc = "Error type to indicate invalid Firefish ID"]
|
||||
#[error("'{id}' is not a valid Firefish ID")]
|
||||
pub struct InvalidIdError {
|
||||
id: String,
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue