From 58e2903177b02444a4e74c9018d7ca6823f38e9c Mon Sep 17 00:00:00 2001 From: Liu Hancheng Date: Fri, 4 Mar 2022 10:46:59 +0800 Subject: [PATCH] first try --- meilisearch-lib/src/document_formats.rs | 28 ++++++++++++++++++------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/meilisearch-lib/src/document_formats.rs b/meilisearch-lib/src/document_formats.rs index cfd73038e..6b018e683 100644 --- a/meilisearch-lib/src/document_formats.rs +++ b/meilisearch-lib/src/document_formats.rs @@ -1,4 +1,5 @@ -use std::fmt; +use std::borrow::Borrow; +use std::fmt::{self, Debug, Display}; use std::io::{self, BufRead, BufReader, BufWriter, Cursor, Read, Seek, Write}; use meilisearch_error::{internal_error, Code, ErrorCode}; @@ -23,16 +24,27 @@ impl fmt::Display for PayloadType { } } -#[derive(thiserror::Error, Debug)] +#[derive(Debug)] pub enum DocumentFormatError { - #[error("An internal error has occurred. `{0}`.")] Internal(Box), - #[error("The `{1}` payload provided is malformed. `{0}`.")] - MalformedPayload( - Box, - PayloadType, - ), + MalformedPayload(Box, PayloadType), } +impl Display for DocumentFormatError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::Internal(e) => write!(f, "An internal error has occurred. `{}`.", e), + Self::MalformedPayload(me, b) => match me.borrow() { + milli::documents::Error::JsonError(_) => write!( + f, + "The `{}` payload provided is malformed. line: {}", + b, "TODO" + ), + _ => write!(f, "The `{}` payload provided is malformed. line: {}", b, me), + }, + } + } +} +impl std::error::Error for DocumentFormatError {} impl From<(PayloadType, milli::documents::Error)> for DocumentFormatError { fn from((ty, error): (PayloadType, milli::documents::Error)) -> Self {