Skip to content

Commit ea0bac0

Browse files
committed
refactor: move error into utils/src/error.rs
1 parent 1d5bf05 commit ea0bac0

File tree

10 files changed

+86
-101
lines changed

10 files changed

+86
-101
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/cocoindex/src/builder/analyzed_flow.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::{ops::interface::FlowInstanceContext, prelude::*};
22

33
use super::{analyzer, plan};
4-
use crate::service::error::{SharedError, SharedResultExt, shared_ok};
4+
use cocoindex_utils::error::{SharedError, SharedResultExt, shared_ok};
55

66
pub struct AnalyzedFlow {
77
pub flow_instance: spec::FlowInstanceSpec,

rust/cocoindex/src/execution/memoization.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@ use std::{
77
sync::{Arc, Mutex},
88
};
99

10-
use crate::{
11-
base::{schema, value},
12-
service::error::{SharedError, SharedResultExtRef},
13-
};
10+
use crate::base::{schema, value};
11+
use cocoindex_utils::error::{SharedError, SharedResultExtRef};
1412
use cocoindex_utils::fingerprint::{Fingerprint, Fingerprinter};
1513

1614
#[derive(Debug, Clone, Serialize, Deserialize)]

rust/cocoindex/src/lib_context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ use crate::prelude::*;
44

55
use crate::builder::AnalyzedFlow;
66
use crate::execution::source_indexer::SourceIndexingContext;
7-
use crate::service::error::ApiError;
87
use crate::service::query_handler::{QueryHandler, QueryHandlerSpec};
98
use crate::settings;
109
use crate::setup::ObjectSetupChange;
1110
use axum::http::StatusCode;
11+
use cocoindex_utils::error::ApiError;
1212
use indicatif::MultiProgress;
1313
use sqlx::PgPool;
1414
use sqlx::postgres::{PgConnectOptions, PgPoolOptions};

rust/cocoindex/src/ops/factory_bases.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use std::hash::Hash;
55

66
use super::interface::*;
77
use super::registry::*;
8-
use crate::api_bail;
98
use crate::base::schema::*;
109
use crate::base::spec::*;
1110
use crate::builder::plan::AnalyzedValueMapping;

rust/cocoindex/src/prelude.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ pub(crate) use crate::builder::{self, exec_ctx, plan};
2323
pub(crate) use crate::execution;
2424
pub(crate) use crate::lib_context::{FlowContext, LibContext, get_lib_context, get_runtime};
2525
pub(crate) use crate::ops::interface;
26-
pub(crate) use crate::service::error::{ApiError, invariance_violation};
2726
pub(crate) use crate::setup;
2827
pub(crate) use crate::setup::AuthRegistry;
29-
pub(crate) use crate::{api_bail, api_error};
3028
pub(crate) use cocoindex_utils as utils;
29+
pub(crate) use cocoindex_utils::error::{ApiError, invariance_violation};
30+
pub(crate) use cocoindex_utils::{api_bail, api_error};
3131
pub(crate) use cocoindex_utils::{batching, concur_control, http, retryable};
3232

3333
pub(crate) use anyhow::{anyhow, bail};

rust/cocoindex/src/service/error.rs

Lines changed: 0 additions & 91 deletions
This file was deleted.

rust/cocoindex/src/service/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
pub(crate) mod error;
21
pub(crate) mod flows;
32
pub(crate) mod query_handler;

rust/utils/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ anyhow = { workspace = true }
1111
async-trait = { workspace = true }
1212
log = { workspace = true }
1313

14+
# Web framework
15+
axum = { workspace = true }
16+
1417
# Serialization
1518
serde = { workspace = true }
1619
serde_json = { workspace = true }

rust/utils/src/error.rs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
use anyhow;
2+
use axum::{
3+
Json,
4+
http::StatusCode,
5+
response::{IntoResponse, Response},
6+
};
7+
use serde::Serialize;
28
use std::{
39
error::Error,
410
fmt::{Debug, Display},
@@ -134,3 +140,73 @@ impl<'a, T> SharedResultExtRef<'a, T> for &'a Result<T, SharedError> {
134140
pub fn invariance_violation() -> anyhow::Error {
135141
anyhow::anyhow!("Invariance violation")
136142
}
143+
144+
// API Error types for HTTP responses
145+
146+
#[derive(Debug)]
147+
pub struct ApiError {
148+
pub err: anyhow::Error,
149+
pub status_code: StatusCode,
150+
}
151+
152+
impl ApiError {
153+
pub fn new(message: &str, status_code: StatusCode) -> Self {
154+
Self {
155+
err: anyhow::anyhow!("{}", message),
156+
status_code,
157+
}
158+
}
159+
}
160+
161+
impl Display for ApiError {
162+
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
163+
Display::fmt(&self.err, f)
164+
}
165+
}
166+
167+
impl Error for ApiError {
168+
fn source(&self) -> Option<&(dyn Error + 'static)> {
169+
self.err.source()
170+
}
171+
}
172+
173+
#[derive(Serialize)]
174+
struct ErrorResponse {
175+
error: String,
176+
}
177+
178+
impl IntoResponse for ApiError {
179+
fn into_response(self) -> Response {
180+
log::debug!("Internal server error:\n{:?}", self.err);
181+
let error_response = ErrorResponse {
182+
error: format!("{:?}", self.err),
183+
};
184+
(self.status_code, Json(error_response)).into_response()
185+
}
186+
}
187+
188+
impl From<anyhow::Error> for ApiError {
189+
fn from(err: anyhow::Error) -> ApiError {
190+
if err.is::<ApiError>() {
191+
return err.downcast::<ApiError>().unwrap();
192+
}
193+
Self {
194+
err,
195+
status_code: StatusCode::INTERNAL_SERVER_ERROR,
196+
}
197+
}
198+
}
199+
200+
#[macro_export]
201+
macro_rules! api_bail {
202+
( $fmt:literal $(, $($arg:tt)*)?) => {
203+
return Err($crate::error::ApiError::new(&format!($fmt $(, $($arg)*)?), axum::http::StatusCode::BAD_REQUEST).into())
204+
};
205+
}
206+
207+
#[macro_export]
208+
macro_rules! api_error {
209+
( $fmt:literal $(, $($arg:tt)*)?) => {
210+
$crate::error::ApiError::new(&format!($fmt $(, $($arg)*)?), axum::http::StatusCode::BAD_REQUEST)
211+
};
212+
}

0 commit comments

Comments
 (0)