fix(server): catch panic for context parsing (#10912)

fix AF-2335
fix CLOUD-173
This commit is contained in:
darkskygit
2025-03-17 09:44:57 +00:00
parent b401012d85
commit 92db9a693a
4 changed files with 129 additions and 71 deletions

View File

@@ -1,4 +1,8 @@
use std::{io::Cursor, path::PathBuf};
use std::{
io::Cursor,
panic::{catch_unwind, AssertUnwindSafe},
path::PathBuf,
};
use path_ext::PathExt;
@@ -81,16 +85,28 @@ impl Doc {
fn from_loader(
file_path: &str,
loader: impl Loader,
loader: impl Loader + 'static,
splitter: impl TextSplitter + 'static,
) -> Result<Doc, LoaderError> {
let name = file_path.to_string();
let chunks = Self::get_chunks_from_loader(loader, splitter)?;
let chunks = catch_unwind(AssertUnwindSafe(|| {
Self::get_chunks_from_loader(loader, splitter)
}))
.map_err(|e| {
LoaderError::Other(match e.downcast::<String>() {
Ok(v) => *v,
Err(e) => match e.downcast::<&str>() {
Ok(v) => v.to_string(),
_ => "Unknown Source of Error".to_owned(),
},
})
})??;
Ok(Self { name, chunks })
}
fn get_chunks_from_loader(
loader: impl Loader,
loader: impl Loader + 'static,
splitter: impl TextSplitter + 'static,
) -> Result<Vec<Chunk>, LoaderError> {
let docs = loader.load_and_split(splitter)?;