handle most io error instead of tagging everything as an internal

This commit is contained in:
Tamo 2022-12-19 20:50:40 +01:00
parent 19ee9a828f
commit d8fb506c92
No known key found for this signature in database
GPG Key ID: 20CD8020AFA88D69
5 changed files with 64 additions and 17 deletions

2
Cargo.lock generated
View File

@ -2374,6 +2374,7 @@ dependencies = [
"csv", "csv",
"either", "either",
"enum-iterator", "enum-iterator",
"file-store",
"flate2", "flate2",
"fst", "fst",
"insta", "insta",
@ -2386,6 +2387,7 @@ dependencies = [
"serde", "serde",
"serde_json", "serde_json",
"tar", "tar",
"tempfile",
"thiserror", "thiserror",
"time", "time",
"tokio", "tokio",

View File

@ -114,17 +114,17 @@ impl ErrorCode for Error {
Error::Dump(e) => e.error_code(), Error::Dump(e) => e.error_code(),
Error::Milli(e) => e.error_code(), Error::Milli(e) => e.error_code(),
Error::ProcessBatchPanicked => Code::Internal, Error::ProcessBatchPanicked => Code::Internal,
// TODO: TAMO: are all these errors really internal? Error::Heed(e) => e.error_code(),
Error::Heed(_) => Code::Internal, Error::HeedTransaction(e) => e.error_code(),
Error::FileStore(_) => Code::Internal, Error::FileStore(e) => e.error_code(),
Error::IoError(_) => Code::Internal, Error::IoError(e) => e.error_code(),
Error::Persist(_) => Code::Internal, Error::Persist(e) => e.error_code(),
// Irrecoverable errors
Error::Anyhow(_) => Code::Internal, Error::Anyhow(_) => Code::Internal,
Error::CorruptedTaskQueue => Code::Internal, Error::CorruptedTaskQueue => Code::Internal,
Error::CorruptedDump => Code::Internal, Error::CorruptedDump => Code::Internal,
Error::TaskDatabaseUpdate(_) => Code::Internal, Error::TaskDatabaseUpdate(_) => Code::Internal,
Error::CreateBatch(_) => Code::Internal, Error::CreateBatch(_) => Code::Internal,
Error::HeedTransaction(_) => Code::Internal,
} }
} }
} }

View File

@ -10,6 +10,7 @@ anyhow = "1.0.65"
csv = "1.1.6" csv = "1.1.6"
either = { version = "1.6.1", features = ["serde"] } either = { version = "1.6.1", features = ["serde"] }
enum-iterator = "1.1.3" enum-iterator = "1.1.3"
file-store = { path = "../file-store" }
flate2 = "1.0.24" flate2 = "1.0.24"
fst = "0.4.7" fst = "0.4.7"
memmap2 = "0.5.7" memmap2 = "0.5.7"
@ -20,6 +21,7 @@ roaring = { version = "0.10.0", features = ["serde"] }
serde = { version = "1.0.145", features = ["derive"] } serde = { version = "1.0.145", features = ["derive"] }
serde_json = "1.0.85" serde_json = "1.0.85"
tar = "0.4.38" tar = "0.4.38"
tempfile = "3.3.0"
thiserror = "1.0.30" thiserror = "1.0.30"
time = { version = "0.3.7", features = ["serde-well-known", "formatting", "parsing", "macros"] } time = { version = "0.3.7", features = ["serde-well-known", "formatting", "parsing", "macros"] }
tokio = "1.0" tokio = "1.0"

View File

@ -13,7 +13,6 @@ use serde::{Deserialize, Deserializer};
use serde_json::error::Category; use serde_json::error::Category;
use crate::error::{Code, ErrorCode}; use crate::error::{Code, ErrorCode};
use crate::internal_error;
type Result<T> = std::result::Result<T, DocumentFormatError>; type Result<T> = std::result::Result<T, DocumentFormatError>;
@ -36,6 +35,7 @@ impl fmt::Display for PayloadType {
#[derive(Debug)] #[derive(Debug)]
pub enum DocumentFormatError { pub enum DocumentFormatError {
Io(io::Error),
Internal(Box<dyn std::error::Error + Send + Sync + 'static>), Internal(Box<dyn std::error::Error + Send + Sync + 'static>),
MalformedPayload(Error, PayloadType), MalformedPayload(Error, PayloadType),
} }
@ -43,6 +43,7 @@ pub enum DocumentFormatError {
impl Display for DocumentFormatError { impl Display for DocumentFormatError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self { match self {
Self::Io(e) => write!(f, "{e}"),
Self::Internal(e) => write!(f, "An internal error has occurred: `{}`.", e), Self::Internal(e) => write!(f, "An internal error has occurred: `{}`.", e),
Self::MalformedPayload(me, b) => match me.borrow() { Self::MalformedPayload(me, b) => match me.borrow() {
Error::Json(se) => { Error::Json(se) => {
@ -91,17 +92,22 @@ impl From<(PayloadType, Error)> for DocumentFormatError {
} }
} }
impl From<io::Error> for DocumentFormatError {
fn from(error: io::Error) -> Self {
Self::Io(error)
}
}
impl ErrorCode for DocumentFormatError { impl ErrorCode for DocumentFormatError {
fn error_code(&self) -> Code { fn error_code(&self) -> Code {
match self { match self {
DocumentFormatError::Io(e) => e.error_code(),
DocumentFormatError::Internal(_) => Code::Internal, DocumentFormatError::Internal(_) => Code::Internal,
DocumentFormatError::MalformedPayload(_, _) => Code::MalformedPayload, DocumentFormatError::MalformedPayload(_, _) => Code::MalformedPayload,
} }
} }
} }
internal_error!(DocumentFormatError: io::Error);
/// Reads CSV from input and write an obkv batch to writer. /// Reads CSV from input and write an obkv batch to writer.
pub fn read_csv(file: &File, writer: impl Write + Seek) -> Result<u64> { pub fn read_csv(file: &File, writer: impl Write + Seek) -> Result<u64> {
let mut builder = DocumentsBatchBuilder::new(writer); let mut builder = DocumentsBatchBuilder::new(writer);

View File

@ -1,4 +1,4 @@
use std::fmt; use std::{fmt, io};
use actix_web::http::StatusCode; use actix_web::http::StatusCode;
use actix_web::{self as aweb, HttpResponseBuilder}; use actix_web::{self as aweb, HttpResponseBuilder};
@ -113,6 +113,11 @@ impl fmt::Display for ErrorType {
#[derive(Serialize, Deserialize, Debug, Clone, Copy)] #[derive(Serialize, Deserialize, Debug, Clone, Copy)]
pub enum Code { pub enum Code {
// error related to your setup
IoError,
NoSpaceLeftOnDevice,
TooManyOpenFiles,
// index related error // index related error
CreateIndex, CreateIndex,
IndexAlreadyExists, IndexAlreadyExists,
@ -145,7 +150,6 @@ pub enum Code {
InvalidToken, InvalidToken,
MissingAuthorizationHeader, MissingAuthorizationHeader,
MissingMasterKey, MissingMasterKey,
NoSpaceLeftOnDevice,
DumpNotFound, DumpNotFound,
InvalidTaskDateFilter, InvalidTaskDateFilter,
InvalidTaskStatusesFilter, InvalidTaskStatusesFilter,
@ -188,6 +192,15 @@ impl Code {
use Code::*; use Code::*;
match self { match self {
// related to the setup
IoError => ErrCode::invalid("io_error", StatusCode::UNPROCESSABLE_ENTITY),
TooManyOpenFiles => {
ErrCode::invalid("too_many_open_files", StatusCode::UNPROCESSABLE_ENTITY)
}
NoSpaceLeftOnDevice => {
ErrCode::invalid("no_space_left_on_device", StatusCode::UNPROCESSABLE_ENTITY)
}
// index related errors // index related errors
// create index is thrown on internal error while creating an index. // create index is thrown on internal error while creating an index.
CreateIndex => { CreateIndex => {
@ -266,9 +279,6 @@ impl Code {
ErrCode::invalid("missing_task_filters", StatusCode::BAD_REQUEST) ErrCode::invalid("missing_task_filters", StatusCode::BAD_REQUEST)
} }
DumpNotFound => ErrCode::invalid("dump_not_found", StatusCode::NOT_FOUND), DumpNotFound => ErrCode::invalid("dump_not_found", StatusCode::NOT_FOUND),
NoSpaceLeftOnDevice => {
ErrCode::internal("no_space_left_on_device", StatusCode::INTERNAL_SERVER_ERROR)
}
PayloadTooLarge => ErrCode::invalid("payload_too_large", StatusCode::PAYLOAD_TOO_LARGE), PayloadTooLarge => ErrCode::invalid("payload_too_large", StatusCode::PAYLOAD_TOO_LARGE),
RetrieveDocument => { RetrieveDocument => {
ErrCode::internal("unretrievable_document", StatusCode::BAD_REQUEST) ErrCode::internal("unretrievable_document", StatusCode::BAD_REQUEST)
@ -380,7 +390,7 @@ impl ErrorCode for milli::Error {
match self { match self {
Error::InternalError(_) => Code::Internal, Error::InternalError(_) => Code::Internal,
Error::IoError(_) => Code::Internal, Error::IoError(e) => e.error_code(),
Error::UserError(ref error) => { Error::UserError(ref error) => {
match error { match error {
// TODO: wait for spec for new error codes. // TODO: wait for spec for new error codes.
@ -415,13 +425,28 @@ impl ErrorCode for milli::Error {
} }
} }
impl ErrorCode for file_store::Error {
fn error_code(&self) -> Code {
match self {
Self::IoError(e) => e.error_code(),
Self::PersistError(e) => e.error_code(),
}
}
}
impl ErrorCode for tempfile::PersistError {
fn error_code(&self) -> Code {
self.error.error_code()
}
}
impl ErrorCode for HeedError { impl ErrorCode for HeedError {
fn error_code(&self) -> Code { fn error_code(&self) -> Code {
match self { match self {
HeedError::Mdb(MdbError::MapFull) => Code::DatabaseSizeLimitReached, HeedError::Mdb(MdbError::MapFull) => Code::DatabaseSizeLimitReached,
HeedError::Mdb(MdbError::Invalid) => Code::InvalidStore, HeedError::Mdb(MdbError::Invalid) => Code::InvalidStore,
HeedError::Io(_) HeedError::Io(e) => e.error_code(),
| HeedError::Mdb(_) HeedError::Mdb(_)
| HeedError::Encoding | HeedError::Encoding
| HeedError::Decoding | HeedError::Decoding
| HeedError::InvalidDatabaseTyping | HeedError::InvalidDatabaseTyping
@ -431,6 +456,18 @@ impl ErrorCode for HeedError {
} }
} }
impl ErrorCode for io::Error {
fn error_code(&self) -> Code {
match self.raw_os_error() {
Some(5) => Code::IoError,
Some(24) => Code::TooManyOpenFiles,
Some(28) => Code::NoSpaceLeftOnDevice,
e => todo!("missed something asshole {:?}", e),
// e => Code::Internal,
}
}
}
#[cfg(feature = "test-traits")] #[cfg(feature = "test-traits")]
mod strategy { mod strategy {
use proptest::strategy::Strategy; use proptest::strategy::Strategy;