Skip to content

Commit

Permalink
backend: refactor error handling (#552)
Browse files Browse the repository at this point in the history
* backend: refactor error logging

* use non zero

* extract rspotify not found error

* disable frame filtering
  • Loading branch information
vnghia authored Dec 6, 2024
1 parent a739759 commit 8778431
Show file tree
Hide file tree
Showing 46 changed files with 663 additions and 346 deletions.
1 change: 1 addition & 0 deletions .github/workflows/coverage.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ env:
CARGO_TERM_COLOR: always
RUST_BACKTRACE: full
RUST_LOG: debug
COLORBT_SHOW_HIDDEN: 1
VCPKG_BINARY_SOURCES: "clear;x-gha,readwrite"
DATABASE_URL: "postgres://postgres:postgres@localhost:5432/postgres"
AWS_ACCESS_KEY_ID: minioadmin
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ env:
CARGO_TERM_COLOR: always
RUST_BACKTRACE: full
RUST_LOG: debug
COLORBT_SHOW_HIDDEN: 1
VCPKG_BINARY_SOURCES: "clear;x-gha,readwrite"
DATABASE_URL: "postgres://postgres:postgres@localhost:5432/postgres"
AWS_ACCESS_KEY_ID: minioadmin
Expand Down
41 changes: 31 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ wildcard_imports = { level = "allow", priority = 0 }
[workspace.dependencies]
bitcode = { version = "0.6.3", features = ["serde"] }
bon = { version = "3.1.1" }
color-eyre = { version = "0.6.3" }
color-eyre = { version = "0.6.3", default-features = false, features = [
"capture-spantrace",
] }
concat-string = { version = "1.0.1" }
const_format = { version = "0.2.33", features = ["fmt"] }
convert_case = { version = "0.6.0" }
Expand All @@ -45,7 +47,7 @@ reqwest = { version = "0.12.9", default-features = false, features = [
"native-tls",
] }
rstest = { version = "0.23.0" }
thiserror = { version = "1.0.69" }
thiserror = { version = "2.0.4" }
url = { version = "2.5.4" }

# Dev
Expand Down
1 change: 1 addition & 0 deletions nghe-backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ tower-http = { version = "0.6.2", features = [
"compression-zstd",
"trace",
] }
tracing-error = { version = "0.2.1" }
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
typed-path = { git = "https://github.com/vnghia/typed-path", rev = "bd796e64b3cee53181a3fc2f15245f5bf731bd8c" }
unicode-normalization = { version = "0.1.24" }
Expand Down
9 changes: 5 additions & 4 deletions nghe-backend/src/config/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use educe::Educe;
use itertools::Itertools;
use serde::{Deserialize, Serialize};

use crate::{database, Error};
use crate::{database, error, Error};

#[derive(Debug, Clone, Serialize, Deserialize, Educe)]
#[educe(Default)]
Expand All @@ -24,7 +24,7 @@ impl Index {
.iter()
.map(|prefix| prefix.as_ref().strip_suffix(' '))
.collect::<Option<Vec<_>>>()
.ok_or_else(|| Error::ConfigIndexIgnorePrefixEndWithoutSpace)?
.ok_or_else(|| error::Kind::InvalidIndexIgnorePrefixesFormat)?
.iter()
.join(" "))
}
Expand All @@ -50,8 +50,9 @@ mod split {
where
S: Serializer,
{
serializer
.serialize_str(&Index::merge(prefixes).map_err(|e| S::Error::custom(e.to_string()))?)
serializer.serialize_str(&Index::merge(prefixes).map_err(|_| {
S::Error::custom(error::Kind::InvalidIndexIgnorePrefixesFormat.to_string())
})?)
}

pub fn deserialize<'de, D>(deserializer: D) -> Result<Vec<String>, D::Error>
Expand Down
12 changes: 5 additions & 7 deletions nghe-backend/src/database/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use diesel_async::RunQueryDsl;

use super::Database;
use crate::orm::configs;
use crate::Error;
use crate::{error, Error};

pub trait Config {
const KEY: &'static str;
Expand Down Expand Up @@ -43,16 +43,14 @@ impl Database {
.await?;

if C::ENCRYPTED {
String::from_utf8(
self.decrypt(
config.byte.ok_or_else(|| Error::DatabaseInvalidConfigFormat(C::KEY))?,
)?,
)
String::from_utf8(self.decrypt(
config.byte.ok_or_else(|| error::Kind::InvalidDatabaseConfigFomat(C::KEY))?,
)?)
.map_err(Error::from)
} else {
config
.text
.ok_or_else(|| Error::DatabaseInvalidConfigFormat(C::KEY))
.ok_or_else(|| error::Kind::InvalidDatabaseConfigFomat(C::KEY).into())
.map(Cow::into_owned)
}
}
Expand Down
10 changes: 7 additions & 3 deletions nghe-backend/src/database/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use diesel_async::pooled_connection::{deadpool, AsyncDieselConnectionManager};
use diesel_async::AsyncPgConnection;
use libaes::Cipher;

use crate::Error;
use crate::{error, Error};

type Connection = AsyncDieselConnectionManager<AsyncPgConnection>;
type Pool = deadpool::Pool<AsyncPgConnection>;
Expand All @@ -30,7 +30,7 @@ impl Database {
}

pub async fn get(&self) -> Result<deadpool::Object<AsyncPgConnection>, Error> {
self.pool.get().await.map_err(|_| Error::CheckoutConnectionPool)
self.pool.get().await.map_err(Error::from)
}

pub fn encrypt(&self, data: impl AsRef<[u8]>) -> Vec<u8> {
Expand All @@ -55,7 +55,11 @@ impl Database {
let iv = &data[..Self::IV_LEN];

let output = Cipher::new_128(key).cbc_decrypt(iv, cipher_text);
if output.is_empty() { Err(Error::DecryptDatabaseValue) } else { Ok(output) }
if output.is_empty() {
error::Kind::DatabaseValueDecryptionFailed.into()
} else {
Ok(output)
}
}
}

Expand Down
Loading

0 comments on commit 8778431

Please sign in to comment.