Skip to content

Commit

Permalink
consolidate error types
Browse files Browse the repository at this point in the history
  • Loading branch information
Centeno448 committed Oct 3, 2024
1 parent 5d91d16 commit 901cf13
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 29 deletions.
19 changes: 13 additions & 6 deletions backend-rs/src/api/flake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ pub async fn get_flake(
async fn get_flakes_by_ids(
flake_ids: Vec<&i32>,
pool: &Pool<Postgres>,
) -> Result<Vec<FlakeRelease>, sqlx::Error> {
) -> Result<Vec<FlakeRelease>, AppError> {
if flake_ids.is_empty() {
return Ok(vec![]);
}
Expand All @@ -113,12 +113,16 @@ async fn get_flakes_by_ids(
WHERE release.id IN ({param_string})",
);

let releases: Vec<FlakeRelease> = sqlx::query_as(&query).fetch_all(pool).await?;
let releases: Vec<FlakeRelease> =
sqlx::query_as(&query)
.fetch_all(pool)
.await
.context("Failed to fetch flakes by id from database")?;

Ok(releases)
}

async fn get_flakes(pool: &Pool<Postgres>) -> Result<Vec<FlakeRelease>, sqlx::Error> {
async fn get_flakes(pool: &Pool<Postgres>) -> Result<Vec<FlakeRelease>, AppError> {
let releases: Vec<FlakeRelease> = sqlx::query_as(
"SELECT release.id AS id, \
githubowner.name AS owner, \
Expand All @@ -132,7 +136,8 @@ async fn get_flakes(pool: &Pool<Postgres>) -> Result<Vec<FlakeRelease>, sqlx::Er
ORDER BY release.created_at DESC LIMIT 100",
)
.fetch_all(pool)
.await?;
.await
.context("Failed to fetch flakes from database")?;

Ok(releases)
}
Expand All @@ -157,9 +162,11 @@ async fn search_flakes(opensearch: &OpenSearch, q: &String) -> Result<HashMap<i3
}
}))
.send()
.await?
.await
.context("Failed to send opensearch request")?
.json::<Value>()
.await?;
.await
.context("Failed to decode opensearch response as json")?;

// TODO: Remove this unwrap, use fold or map to create the HashMap
let mut hits: HashMap<i32, f64> = HashMap::new();
Expand Down
26 changes: 3 additions & 23 deletions backend-rs/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,17 @@ pub struct AppState {
pub pool: PgPool,
}

pub enum AppError {
OpenSearchError(opensearch::Error),
SqlxError(sqlx::Error),
UnexpectedError(anyhow::Error),
}

impl From<opensearch::Error> for AppError {
fn from(value: opensearch::Error) -> Self {
AppError::OpenSearchError(value)
}
}

impl From<sqlx::Error> for AppError {
fn from(value: sqlx::Error) -> Self {
AppError::SqlxError(value)
}
}
pub struct AppError(anyhow::Error);

impl From<anyhow::Error> for AppError {
fn from(value: anyhow::Error) -> Self {
AppError::UnexpectedError(value)
AppError(value)
}
}

impl IntoResponse for AppError {
fn into_response(self) -> axum::response::Response {
let body = match self {
AppError::OpenSearchError(error) => error.to_string(),
AppError::SqlxError(error) => error.to_string(),
AppError::UnexpectedError(error) => error.to_string(),
};
let body = self.0.to_string();
(StatusCode::INTERNAL_SERVER_ERROR, Json(body)).into_response()
}
}

0 comments on commit 901cf13

Please sign in to comment.