Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion rust/cocoindex/src/builder/analyzed_flow.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{ops::interface::FlowInstanceContext, prelude::*};

use super::{analyzer, plan};
use crate::service::error::{SharedError, SharedResultExt, shared_ok};
use cocoindex_utils::error::{SharedError, SharedResultExt, shared_ok};

pub struct AnalyzedFlow {
pub flow_instance: spec::FlowInstanceSpec,
Expand Down
6 changes: 2 additions & 4 deletions rust/cocoindex/src/execution/memoization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ use std::{
sync::{Arc, Mutex},
};

use crate::{
base::{schema, value},
service::error::{SharedError, SharedResultExtRef},
};
use crate::base::{schema, value};
use cocoindex_utils::error::{SharedError, SharedResultExtRef};
use cocoindex_utils::fingerprint::{Fingerprint, Fingerprinter};

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down
2 changes: 1 addition & 1 deletion rust/cocoindex/src/lib_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ use crate::prelude::*;

use crate::builder::AnalyzedFlow;
use crate::execution::source_indexer::SourceIndexingContext;
use crate::service::error::ApiError;
use crate::service::query_handler::{QueryHandler, QueryHandlerSpec};
use crate::settings;
use crate::setup::ObjectSetupChange;
use axum::http::StatusCode;
use cocoindex_utils::error::ApiError;
use indicatif::MultiProgress;
use sqlx::PgPool;
use sqlx::postgres::{PgConnectOptions, PgPoolOptions};
Expand Down
1 change: 0 additions & 1 deletion rust/cocoindex/src/ops/factory_bases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use std::hash::Hash;

use super::interface::*;
use super::registry::*;
use crate::api_bail;
use crate::base::schema::*;
use crate::base::spec::*;
use crate::builder::plan::AnalyzedValueMapping;
Expand Down
4 changes: 2 additions & 2 deletions rust/cocoindex/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ pub(crate) use crate::builder::{self, exec_ctx, plan};
pub(crate) use crate::execution;
pub(crate) use crate::lib_context::{FlowContext, LibContext, get_lib_context, get_runtime};
pub(crate) use crate::ops::interface;
pub(crate) use crate::service::error::{ApiError, invariance_violation};
pub(crate) use crate::setup;
pub(crate) use crate::setup::AuthRegistry;
pub(crate) use crate::{api_bail, api_error};
pub(crate) use cocoindex_utils as utils;
pub(crate) use cocoindex_utils::error::{ApiError, invariance_violation};
pub(crate) use cocoindex_utils::{api_bail, api_error};
pub(crate) use cocoindex_utils::{batching, concur_control, http, retryable};

pub(crate) use anyhow::{anyhow, bail};
Expand Down
91 changes: 0 additions & 91 deletions rust/cocoindex/src/service/error.rs

This file was deleted.

1 change: 0 additions & 1 deletion rust/cocoindex/src/service/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
pub(crate) mod error;
pub(crate) mod flows;
pub(crate) mod query_handler;
3 changes: 3 additions & 0 deletions rust/utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ anyhow = { workspace = true }
async-trait = { workspace = true }
log = { workspace = true }

# Web framework
axum = { workspace = true }

# Serialization
serde = { workspace = true }
serde_json = { workspace = true }
Expand Down
76 changes: 76 additions & 0 deletions rust/utils/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
use anyhow;
use axum::{
Json,
http::StatusCode,
response::{IntoResponse, Response},
};
use serde::Serialize;
use std::{
error::Error,
fmt::{Debug, Display},
Expand Down Expand Up @@ -134,3 +140,73 @@ impl<'a, T> SharedResultExtRef<'a, T> for &'a Result<T, SharedError> {
pub fn invariance_violation() -> anyhow::Error {
anyhow::anyhow!("Invariance violation")
}

// API Error types for HTTP responses

#[derive(Debug)]
pub struct ApiError {
pub err: anyhow::Error,
pub status_code: StatusCode,
}

impl ApiError {
pub fn new(message: &str, status_code: StatusCode) -> Self {
Self {
err: anyhow::anyhow!("{}", message),
status_code,
}
}
}

impl Display for ApiError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
Display::fmt(&self.err, f)
}
}

impl Error for ApiError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
self.err.source()
}
}

#[derive(Serialize)]
struct ErrorResponse {
error: String,
}

impl IntoResponse for ApiError {
fn into_response(self) -> Response {
log::debug!("Internal server error:\n{:?}", self.err);
let error_response = ErrorResponse {
error: format!("{:?}", self.err),
};
(self.status_code, Json(error_response)).into_response()
}
}

impl From<anyhow::Error> for ApiError {
fn from(err: anyhow::Error) -> ApiError {
if err.is::<ApiError>() {
return err.downcast::<ApiError>().unwrap();
}
Self {
err,
status_code: StatusCode::INTERNAL_SERVER_ERROR,
}
}
}

#[macro_export]
macro_rules! api_bail {
( $fmt:literal $(, $($arg:tt)*)?) => {
return Err($crate::error::ApiError::new(&format!($fmt $(, $($arg)*)?), axum::http::StatusCode::BAD_REQUEST).into())
};
}

#[macro_export]
macro_rules! api_error {
( $fmt:literal $(, $($arg:tt)*)?) => {
$crate::error::ApiError::new(&format!($fmt $(, $($arg)*)?), axum::http::StatusCode::BAD_REQUEST)
};
}
Loading