2021-10-28 17:18:32 +08:00
use std ::collections ::BTreeSet ;
2021-06-14 22:58:38 +08:00
use std ::convert ::Infallible ;
2023-01-19 19:25:18 +08:00
use std ::fmt ::Write ;
2022-05-03 02:10:25 +08:00
use std ::{ io , str } ;
2021-06-09 23:05:46 +08:00
2021-06-17 00:33:33 +08:00
use heed ::{ Error as HeedError , MdbError } ;
2021-06-14 22:58:38 +08:00
use rayon ::ThreadPoolBuildError ;
2024-07-04 23:48:03 +08:00
use rhai ::EvalAltResult ;
2022-06-15 21:36:27 +08:00
use serde_json ::Value ;
2022-05-03 02:10:25 +08:00
use thiserror ::Error ;
2021-06-10 23:31:08 +08:00
2022-06-20 19:48:02 +08:00
use crate ::documents ::{ self , DocumentsBatchCursorError } ;
2024-04-24 22:40:12 +08:00
use crate ::thread_pool_no_abort ::PanicCatched ;
2022-06-15 21:36:27 +08:00
use crate ::{ CriterionError , DocumentId , FieldId , Object , SortError } ;
2021-06-09 23:05:46 +08:00
2021-09-02 21:57:40 +08:00
pub fn is_reserved_keyword ( keyword : & str ) -> bool {
2022-10-29 00:22:26 +08:00
[ " _geo " , " _geoDistance " , " _geoPoint " , " _geoRadius " , " _geoBoundingBox " ] . contains ( & keyword )
2021-09-02 21:57:40 +08:00
}
2022-05-03 02:10:25 +08:00
#[ derive(Error, Debug) ]
2021-06-09 23:05:46 +08:00
pub enum Error {
2022-05-03 02:10:25 +08:00
#[ error( " internal: {0}. " ) ]
InternalError ( #[ from ] InternalError ) ,
#[ error(transparent) ]
IoError ( #[ from ] io ::Error ) ,
#[ error(transparent) ]
UserError ( #[ from ] UserError ) ,
2021-06-09 23:05:46 +08:00
}
2022-05-03 02:10:25 +08:00
#[ derive(Error, Debug) ]
2021-06-09 23:05:46 +08:00
pub enum InternalError {
2022-05-03 02:10:25 +08:00
#[ error( " {} " , HeedError::DatabaseClosing) ]
2021-06-14 22:58:38 +08:00
DatabaseClosing ,
2022-05-03 02:10:25 +08:00
#[ error( " Missing {} in the {db_name} database. " , key.unwrap_or( " key " )) ]
2021-06-10 23:31:08 +08:00
DatabaseMissingEntry { db_name : & 'static str , key : Option < & 'static str > } ,
2024-05-15 21:02:26 +08:00
#[ error( " Missing {key} in the fieldids weights mapping. " ) ]
FieldidsWeightsMapMissingEntry { key : FieldId } ,
2022-05-03 02:10:25 +08:00
#[ error(transparent) ]
FieldIdMapMissingEntry ( #[ from ] FieldIdMapMissingEntry ) ,
#[ error( " Missing {key} in the field id mapping. " ) ]
2022-03-24 00:28:41 +08:00
FieldIdMappingMissingEntry { key : FieldId } ,
2022-05-03 02:10:25 +08:00
#[ error(transparent) ]
Fst ( #[ from ] fst ::Error ) ,
2022-06-20 19:48:02 +08:00
#[ error(transparent) ]
DocumentsError ( #[ from ] documents ::Error ) ,
2024-04-22 21:50:43 +08:00
#[ error( " Invalid compression type have been specified to grenad " ) ]
2021-06-14 22:58:38 +08:00
GrenadInvalidCompressionType ,
2024-04-22 21:50:43 +08:00
#[ error( " Invalid grenad file with an invalid version format " ) ]
2022-02-16 22:28:48 +08:00
GrenadInvalidFormatVersion ,
2024-04-22 21:50:43 +08:00
#[ error( " Invalid merge while processing {process} " ) ]
2021-06-10 23:31:08 +08:00
IndexingMergingKeys { process : & 'static str } ,
2022-05-03 02:10:25 +08:00
#[ error(transparent) ]
RayonThreadPool ( #[ from ] ThreadPoolBuildError ) ,
2024-04-24 22:40:12 +08:00
#[ error(transparent) ]
PanicInThreadPool ( #[ from ] PanicCatched ) ,
2022-05-03 02:10:25 +08:00
#[ error(transparent) ]
SerdeJson ( #[ from ] serde_json ::Error ) ,
#[ error(transparent) ]
Serialization ( #[ from ] SerializationError ) ,
#[ error(transparent) ]
Store ( #[ from ] MdbError ) ,
#[ error(transparent) ]
Utf8 ( #[ from ] str ::Utf8Error ) ,
2024-04-22 21:50:43 +08:00
#[ error( " An indexation process was explicitly aborted " ) ]
2022-10-05 23:41:07 +08:00
AbortedIndexation ,
2024-04-22 21:50:43 +08:00
#[ error( " The matching words list contains at least one invalid member " ) ]
2023-03-02 01:52:14 +08:00
InvalidMatchingWords ,
2023-12-08 00:03:10 +08:00
#[ error(transparent) ]
ArroyError ( #[ from ] arroy ::Error ) ,
2023-12-13 04:19:48 +08:00
#[ error(transparent) ]
VectorEmbeddingError ( #[ from ] crate ::vector ::Error ) ,
2022-05-03 02:10:25 +08:00
}
#[ derive(Error, Debug) ]
2021-06-10 21:55:22 +08:00
pub enum SerializationError {
2022-05-03 02:10:25 +08:00
#[ error( " {} " , match .db_name {
Some ( name ) = > format! ( " decoding from the {name} database failed " ) ,
None = > " decoding failed " . to_string ( ) ,
} ) ]
2021-06-10 21:55:22 +08:00
Decoding { db_name : Option < & 'static str > } ,
2022-05-03 02:10:25 +08:00
#[ error( " {} " , match .db_name {
Some ( name ) = > format! ( " encoding into the {name} database failed " ) ,
None = > " encoding failed " . to_string ( ) ,
} ) ]
2021-06-10 21:55:22 +08:00
Encoding { db_name : Option < & 'static str > } ,
2022-05-03 02:10:25 +08:00
#[ error( " number is not a valid finite number " ) ]
2021-06-10 21:55:22 +08:00
InvalidNumberSerialization ,
2021-06-09 23:05:46 +08:00
}
2022-05-03 02:10:25 +08:00
#[ derive(Error, Debug) ]
2021-06-09 23:05:46 +08:00
pub enum FieldIdMapMissingEntry {
2022-05-03 02:10:25 +08:00
#[ error( " unknown field id {field_id} coming from the {process} process " ) ]
2021-06-15 17:10:50 +08:00
FieldId { field_id : FieldId , process : & 'static str } ,
2022-05-03 02:10:25 +08:00
#[ error( " unknown field name {field_name} coming from the {process} process " ) ]
2021-06-15 17:10:50 +08:00
FieldName { field_name : String , process : & 'static str } ,
2021-06-09 23:05:46 +08:00
}
2022-05-03 02:10:25 +08:00
#[ derive(Error, Debug) ]
2021-06-09 23:05:46 +08:00
pub enum UserError {
2022-05-03 02:10:25 +08:00
#[ error( " A document cannot contain more than 65,535 fields. " ) ]
2021-06-10 21:55:22 +08:00
AttributeLimitReached ,
2022-05-03 02:10:25 +08:00
#[ error(transparent) ]
CriterionError ( #[ from ] CriterionError ) ,
#[ error( " Maximum number of documents reached. " ) ]
2021-06-10 21:55:22 +08:00
DocumentLimitReached ,
2022-05-03 02:10:25 +08:00
#[ error(
" Document identifier `{}` is invalid. \
A document identifier can be of type integer or string , \
only composed of alphanumeric characters ( a - z A - Z 0 - 9 ) , hyphens ( - ) and underscores ( _ ) . " , .document_id.to_string()
) ]
2021-06-10 23:31:08 +08:00
InvalidDocumentId { document_id : Value } ,
2023-01-19 19:25:18 +08:00
#[ error( " Invalid facet distribution, {} " , format_invalid_filter_distribution(.invalid_facets_name, .valid_facets_name)) ]
InvalidFacetsDistribution {
invalid_facets_name : BTreeSet < String > ,
valid_facets_name : BTreeSet < String > ,
} ,
2022-05-03 02:10:25 +08:00
#[ error(transparent) ]
InvalidGeoField ( #[ from ] GeoError ) ,
2023-06-14 22:34:09 +08:00
#[ error( " Invalid vector dimensions: expected: `{}`, found: `{}`. " , .expected, .found) ]
InvalidVectorDimensions { expected : usize , found : usize } ,
2023-12-13 06:39:01 +08:00
#[ error( " The `_vectors` field in the document with id: `{document_id}` is not an object. Was expecting an object with a key for each embedder with manually provided vectors, but instead got `{value}` " ) ]
2024-05-14 17:42:26 +08:00
InvalidVectorsMapType { document_id : String , value : Value } ,
2024-06-27 17:01:52 +08:00
#[ error( " Bad embedder configuration in the document with id: `{document_id}`. {error} " ) ]
InvalidVectorsEmbedderConf { document_id : String , error : deserr ::errors ::JsonError } ,
2022-05-03 02:10:25 +08:00
#[ error( " {0} " ) ]
2021-11-05 00:24:55 +08:00
InvalidFilter ( String ) ,
2023-06-14 22:34:09 +08:00
#[ error( " Invalid type for filter subexpression: expected: {}, found: {1}. " , .0.join( " , " )) ]
2023-03-07 17:02:04 +08:00
InvalidFilterExpression ( & 'static [ & 'static str ] , Value ) ,
2022-05-03 02:10:25 +08:00
#[ error( " Attribute `{}` is not sortable. {} " ,
. field ,
match . valid_fields . is_empty ( ) {
true = > " This index does not have configured sortable attributes. " . to_string ( ) ,
2023-07-24 17:20:07 +08:00
false = > format! ( " Available sortable attributes are: ` {} {} `. " ,
valid_fields . iter ( ) . map ( AsRef ::as_ref ) . collect ::< Vec < & str > > ( ) . join ( " , " ) ,
. hidden_fields . then_some ( " , <..hidden-attributes> " ) . unwrap_or ( " " ) ,
2022-05-03 02:10:25 +08:00
) ,
}
) ]
2023-07-24 17:20:07 +08:00
InvalidSortableAttribute { field : String , valid_fields : BTreeSet < String > , hidden_fields : bool } ,
2024-06-12 04:03:39 +08:00
#[ error( " Attribute `{}` is not filterable and thus, cannot be used as distinct attribute. {} " ,
. field ,
match . valid_fields . is_empty ( ) {
true = > " This index does not have configured filterable attributes. " . to_string ( ) ,
false = > format! ( " Available filterable attributes are: ` {} {} `. " ,
valid_fields . iter ( ) . map ( AsRef ::as_ref ) . collect ::< Vec < & str > > ( ) . join ( " , " ) ,
. hidden_fields . then_some ( " , <..hidden-attributes> " ) . unwrap_or ( " " ) ,
) ,
}
) ]
InvalidDistinctAttribute { field : String , valid_fields : BTreeSet < String > , hidden_fields : bool } ,
2023-06-22 20:28:32 +08:00
#[ error( " Attribute `{}` is not facet-searchable. {} " ,
2023-04-27 00:09:24 +08:00
. field ,
match . valid_fields . is_empty ( ) {
2023-06-22 20:28:32 +08:00
true = > " This index does not have configured facet-searchable attributes. To make it facet-searchable add it to the `filterableAttributes` index settings. " . to_string ( ) ,
2023-07-24 17:20:07 +08:00
false = > format! ( " Available facet-searchable attributes are: ` {} {} `. To make it facet-searchable add it to the `filterableAttributes` index settings. " ,
valid_fields . iter ( ) . map ( AsRef ::as_ref ) . collect ::< Vec < & str > > ( ) . join ( " , " ) ,
. hidden_fields . then_some ( " , <..hidden-attributes> " ) . unwrap_or ( " " ) ,
2023-04-27 00:09:24 +08:00
) ,
}
) ]
2023-07-24 17:20:07 +08:00
InvalidFacetSearchFacetName {
field : String ,
valid_fields : BTreeSet < String > ,
hidden_fields : bool ,
} ,
2023-06-20 23:04:59 +08:00
#[ error( " Attribute `{}` is not searchable. Available searchable attributes are: `{}{}`. " ,
. field ,
. valid_fields . iter ( ) . map ( AsRef ::as_ref ) . collect ::< Vec < & str > > ( ) . join ( " , " ) ,
. hidden_fields . then_some ( " , <..hidden-attributes> " ) . unwrap_or ( " " ) ,
) ]
InvalidSearchableAttribute {
field : String ,
valid_fields : BTreeSet < String > ,
hidden_fields : bool ,
} ,
2023-11-23 01:21:19 +08:00
#[ error( " an environment is already opened with different options " ) ]
2022-08-10 22:25:24 +08:00
InvalidLmdbOpenOptions ,
2023-05-16 16:57:26 +08:00
#[ error( " You must specify where `sort` is listed in the rankingRules setting to use the sort parameter at search time. " ) ]
2021-09-07 16:37:57 +08:00
SortRankingRuleMissing ,
2022-05-03 02:10:25 +08:00
#[ error( " The database file is in an invalid state. " ) ]
2021-06-14 22:58:38 +08:00
InvalidStoreFile ,
2022-05-03 02:10:25 +08:00
#[ error( " Maximum database size has been reached. " ) ]
2021-06-23 19:56:13 +08:00
MaxDatabaseSizeReached ,
2022-05-03 02:10:25 +08:00
#[ error( " Document doesn't have a `{}` attribute: `{}`. " , .primary_key, serde_json::to_string(.document).unwrap()) ]
2021-10-26 23:49:35 +08:00
MissingDocumentId { primary_key : String , document : Object } ,
2022-06-15 23:58:52 +08:00
#[ error( " Document have too many matching `{}` attribute: `{}`. " , .primary_key, serde_json::to_string(.document).unwrap()) ]
TooManyDocumentIds { primary_key : String , document : Object } ,
2023-01-05 17:40:09 +08:00
#[ error( " The primary key inference failed as the engine did not find any field ending with `id` in its name. Please specify the primary key manually using the `primaryKey` query parameter. " ) ]
2022-12-20 00:37:44 +08:00
NoPrimaryKeyCandidateFound ,
2024-01-16 22:27:24 +08:00
#[ error( " The primary key inference failed as the engine found {} fields ending with `id` in their names: '{}' and '{}'. Please specify the primary key manually using the `primaryKey` query parameter. " , .candidates.len(), .candidates.first().unwrap(), .candidates.get(1).unwrap()) ]
2022-12-20 00:37:44 +08:00
MultiplePrimaryKeyCandidatesFound { candidates : Vec < String > } ,
2022-05-03 02:10:25 +08:00
#[ error( " There is no more space left on the device. Consider increasing the size of the disk/partition. " ) ]
2021-06-10 21:55:22 +08:00
NoSpaceLeftOnDevice ,
2022-05-03 02:10:25 +08:00
#[ error( " Index already has a primary key: `{0}`. " ) ]
2021-10-26 23:49:35 +08:00
PrimaryKeyCannotBeChanged ( String ) ,
2022-05-03 02:10:25 +08:00
#[ error(transparent) ]
2021-06-14 22:58:38 +08:00
SerdeJson ( serde_json ::Error ) ,
2022-05-03 02:10:25 +08:00
#[ error(transparent) ]
SortError ( #[ from ] SortError ) ,
#[ error( " An unknown internal document id have been used: `{document_id}`. " ) ]
2021-06-14 22:58:38 +08:00
UnknownInternalDocumentId { document_id : DocumentId } ,
2022-05-03 02:10:25 +08:00
#[ error( " `minWordSizeForTypos` setting is invalid. `oneTypo` and `twoTypos` fields should be between `0` and `255`, and `twoTypos` should be greater or equals to `oneTypo` but found `oneTypo: {0}` and twoTypos: {1}`. " ) ]
2022-03-31 20:15:02 +08:00
InvalidMinTypoWordLenSetting ( u8 , u8 ) ,
2023-11-15 22:46:37 +08:00
#[ error(transparent) ]
VectorEmbeddingError ( #[ from ] crate ::vector ::Error ) ,
#[ error(transparent) ]
MissingDocumentField ( #[ from ] crate ::prompt ::error ::RenderPromptError ) ,
#[ error(transparent) ]
InvalidPrompt ( #[ from ] crate ::prompt ::error ::NewPromptError ) ,
2023-12-21 00:01:22 +08:00
#[ error( " `.embedders.{0}.documentTemplate`: Invalid template: {1}. " ) ]
2023-11-15 22:46:37 +08:00
InvalidPromptForEmbeddings ( String , crate ::prompt ::error ::NewPromptError ) ,
2023-12-13 04:19:48 +08:00
#[ error( " Too many embedders in the configuration. Found {0}, but limited to 256. " ) ]
TooManyEmbedders ( usize ) ,
2024-03-28 22:26:21 +08:00
#[ error( " Cannot find embedder with name `{0}`. " ) ]
2023-12-13 04:19:48 +08:00
InvalidEmbedder ( String ) ,
2023-12-13 06:39:01 +08:00
#[ error( " Too many vectors for document with id {0}: found {1}, but limited to 256. " ) ]
TooManyVectors ( String , usize ) ,
2023-12-21 00:01:22 +08:00
#[ error( " `.embedders.{embedder_name}`: Field `{field}` unavailable for source `{source_}` (only available for sources: {}). Available fields: {} " ,
allowed_sources_for_field
. iter ( )
. map ( | accepted | format! ( " ` {} ` " , accepted ) )
. collect ::< Vec < String > > ( )
. join ( " , " ) ,
allowed_fields_for_source
. iter ( )
. map ( | accepted | format! ( " ` {} ` " , accepted ) )
. collect ::< Vec < String > > ( )
. join ( " , " )
) ]
InvalidFieldForSource {
embedder_name : String ,
source_ : crate ::vector ::settings ::EmbedderSource ,
field : & 'static str ,
allowed_fields_for_source : & 'static [ & 'static str ] ,
allowed_sources_for_field : & 'static [ crate ::vector ::settings ::EmbedderSource ] ,
} ,
#[ error( " `.embedders.{embedder_name}.model`: Invalid model `{model}` for OpenAI. Supported models: {:?} " , crate::vector::openai::EmbeddingModel::supported_models()) ]
InvalidOpenAiModel { embedder_name : String , model : String } ,
#[ error( " `.embedders.{embedder_name}`: Missing field `{field}` (note: this field is mandatory for source {source_}) " ) ]
MissingFieldForSource {
field : & 'static str ,
source_ : crate ::vector ::settings ::EmbedderSource ,
embedder_name : String ,
} ,
2024-02-07 17:39:19 +08:00
#[ error( " `.embedders.{embedder_name}.dimensions`: Model `{model}` does not support overriding its native dimensions of {expected_dimensions}. Found {dimensions} " ) ]
InvalidOpenAiModelDimensions {
embedder_name : String ,
model : & 'static str ,
dimensions : usize ,
expected_dimensions : usize ,
} ,
2024-02-07 18:48:47 +08:00
#[ error( " `.embedders.{embedder_name}.dimensions`: Model `{model}` does not support overriding its dimensions to a value higher than {max_dimensions}. Found {dimensions} " ) ]
InvalidOpenAiModelDimensionsMax {
embedder_name : String ,
model : & 'static str ,
dimensions : usize ,
max_dimensions : usize ,
} ,
#[ error( " `.embedders.{embedder_name}.dimensions`: `dimensions` cannot be zero " ) ]
InvalidSettingsDimensions { embedder_name : String } ,
2024-09-19 00:13:37 +08:00
#[ error(
" `.embedders.{embedder_name}.binaryQuantized`: Cannot disable the binary quantization "
) ]
InvalidDisableBinaryQuantization { embedder_name : String } ,
2024-08-27 23:47:02 +08:00
#[ error( " `.embedders.{embedder_name}.documentTemplateMaxBytes`: `documentTemplateMaxBytes` cannot be zero " ) ]
InvalidSettingsDocumentTemplateMaxBytes { embedder_name : String } ,
2024-03-25 17:05:58 +08:00
#[ error( " `.embedders.{embedder_name}.url`: could not parse `{url}`: {inner_error} " ) ]
InvalidUrl { embedder_name : String , inner_error : url ::ParseError , url : String } ,
2024-07-04 23:48:03 +08:00
#[ error( " Document editions cannot modify a document's primary key " ) ]
DocumentEditionCannotModifyPrimaryKey ,
#[ error( " Document editions must keep documents as objects " ) ]
DocumentEditionDocumentMustBeObject ,
#[ error( " Document edition runtime error encountered while running the function: {0} " ) ]
DocumentEditionRuntimeError ( Box < EvalAltResult > ) ,
#[ error( " Document edition runtime error encountered while compiling the function: {0} " ) ]
DocumentEditionCompilationError ( rhai ::ParseError ) ,
2024-07-16 16:20:20 +08:00
#[ error( " {0} " ) ]
DocumentEmbeddingError ( String ) ,
2023-12-13 04:19:48 +08:00
}
impl From < crate ::vector ::Error > for Error {
fn from ( value : crate ::vector ::Error ) -> Self {
match value . fault ( ) {
FaultSource ::User = > Error ::UserError ( value . into ( ) ) ,
2024-07-16 16:20:20 +08:00
FaultSource ::Runtime = > Error ::UserError ( value . into ( ) ) ,
2023-12-13 04:19:48 +08:00
FaultSource ::Bug = > Error ::InternalError ( value . into ( ) ) ,
2024-07-16 16:20:20 +08:00
FaultSource ::Undecided = > Error ::UserError ( value . into ( ) ) ,
2023-12-13 04:19:48 +08:00
}
}
2021-06-10 21:55:22 +08:00
}
2023-12-08 00:03:10 +08:00
impl From < arroy ::Error > for Error {
fn from ( value : arroy ::Error ) -> Self {
match value {
arroy ::Error ::Heed ( heed ) = > heed . into ( ) ,
arroy ::Error ::Io ( io ) = > io . into ( ) ,
arroy ::Error ::InvalidVecDimension { expected , received } = > {
Error ::UserError ( UserError ::InvalidVectorDimensions { expected , found : received } )
}
arroy ::Error ::DatabaseFull
| arroy ::Error ::InvalidItemAppend
| arroy ::Error ::UnmatchingDistance { .. }
2024-06-20 21:59:32 +08:00
| arroy ::Error ::NeedBuild ( _ )
| arroy ::Error ::MissingKey { .. }
| arroy ::Error ::MissingMetadata ( _ ) = > {
2023-12-08 00:03:10 +08:00
Error ::InternalError ( InternalError ::ArroyError ( value ) )
}
}
}
}
2022-05-03 02:10:25 +08:00
#[ derive(Error, Debug) ]
2022-05-03 01:19:50 +08:00
pub enum GeoError {
2022-06-15 00:12:15 +08:00
#[ error( " The `_geo` field in the document with the id: `{document_id}` is not an object. Was expecting an object with the `_geo.lat` and `_geo.lng` fields but instead got `{value}`. " ) ]
NotAnObject { document_id : Value , value : Value } ,
2023-01-24 19:20:50 +08:00
#[ error( " The `_geo` field in the document with the id: `{document_id}` contains the following unexpected fields: `{value}`. " ) ]
UnexpectedExtraFields { document_id : Value , value : Value } ,
2022-06-15 00:12:15 +08:00
#[ error( " Could not find latitude nor longitude in the document with the id: `{document_id}`. Was expecting `_geo.lat` and `_geo.lng` fields. " ) ]
MissingLatitudeAndLongitude { document_id : Value } ,
2022-05-03 02:10:25 +08:00
#[ error( " Could not find latitude in the document with the id: `{document_id}`. Was expecting a `_geo.lat` field. " ) ]
2022-05-03 01:19:50 +08:00
MissingLatitude { document_id : Value } ,
2022-05-03 02:10:25 +08:00
#[ error( " Could not find longitude in the document with the id: `{document_id}`. Was expecting a `_geo.lng` field. " ) ]
2022-05-03 01:19:50 +08:00
MissingLongitude { document_id : Value } ,
2022-07-11 22:36:23 +08:00
#[ error( " Could not parse latitude nor longitude in the document with the id: `{document_id}`. Was expecting finite numbers but instead got `{lat}` and `{lng}`. " ) ]
2022-06-15 00:12:15 +08:00
BadLatitudeAndLongitude { document_id : Value , lat : Value , lng : Value } ,
2022-07-11 22:36:23 +08:00
#[ error( " Could not parse latitude in the document with the id: `{document_id}`. Was expecting a finite number but instead got `{value}`. " ) ]
2022-05-03 01:19:50 +08:00
BadLatitude { document_id : Value , value : Value } ,
2022-07-11 22:36:23 +08:00
#[ error( " Could not parse longitude in the document with the id: `{document_id}`. Was expecting a finite number but instead got `{value}`. " ) ]
2022-05-03 01:19:50 +08:00
BadLongitude { document_id : Value , value : Value } ,
}
2023-01-19 19:25:18 +08:00
fn format_invalid_filter_distribution (
invalid_facets_name : & BTreeSet < String > ,
valid_facets_name : & BTreeSet < String > ,
) -> String {
if valid_facets_name . is_empty ( ) {
return " this index does not have configured filterable attributes. " . into ( ) ;
}
let mut result = String ::new ( ) ;
match invalid_facets_name . len ( ) {
0 = > ( ) ,
1 = > write! (
result ,
" attribute `{}` is not filterable. " ,
invalid_facets_name . first ( ) . unwrap ( )
)
. unwrap ( ) ,
_ = > write! (
result ,
" attributes `{}` are not filterable. " ,
invalid_facets_name . iter ( ) . map ( AsRef ::as_ref ) . collect ::< Vec < & str > > ( ) . join ( " , " )
)
. unwrap ( ) ,
} ;
match valid_facets_name . len ( ) {
1 = > write! (
result ,
" The available filterable attribute is `{}`. " ,
valid_facets_name . first ( ) . unwrap ( )
)
. unwrap ( ) ,
_ = > write! (
result ,
" The available filterable attributes are `{}`. " ,
valid_facets_name . iter ( ) . map ( AsRef ::as_ref ) . collect ::< Vec < & str > > ( ) . join ( " , " )
)
. unwrap ( ) ,
}
result
}
2022-05-03 02:10:25 +08:00
/// A little macro helper to autogenerate From implementation that needs two `Into`.
/// Given the following parameters: `error_from_sub_error!(FieldIdMapMissingEntry => InternalError)`
/// the macro will create the following code:
/// ```ignore
/// impl From<FieldIdMapMissingEntry> for Error {
/// fn from(error: FieldIdMapMissingEntry) -> Error {
/// Error::from(InternalError::from(error))
/// }
/// }
/// ```
macro_rules ! error_from_sub_error {
( ) = > { } ;
( $sub :ty = > $intermediate :ty ) = > {
impl From < $sub > for Error {
fn from ( error : $sub ) -> Error {
Error ::from ( < $intermediate > ::from ( error ) )
}
}
} ;
( $( $sub :ty = > $intermediate :ty $(, ) ? ) , + ) = > {
$( error_from_sub_error! ( $sub = > $intermediate ) ; ) +
} ;
2021-06-10 21:55:22 +08:00
}
2022-05-03 02:10:25 +08:00
error_from_sub_error! {
FieldIdMapMissingEntry = > InternalError ,
fst ::Error = > InternalError ,
2022-06-20 19:48:02 +08:00
documents ::Error = > InternalError ,
2022-05-03 02:10:25 +08:00
str ::Utf8Error = > InternalError ,
ThreadPoolBuildError = > InternalError ,
SerializationError = > InternalError ,
GeoError = > UserError ,
CriterionError = > UserError ,
2022-05-04 20:11:03 +08:00
}
2021-06-17 00:33:33 +08:00
impl < E > From < grenad ::Error < E > > for Error
where
Error : From < E > ,
{
2021-06-14 22:58:38 +08:00
fn from ( error : grenad ::Error < E > ) -> Error {
match error {
grenad ::Error ::Io ( error ) = > Error ::IoError ( error ) ,
grenad ::Error ::Merge ( error ) = > Error ::from ( error ) ,
grenad ::Error ::InvalidCompressionType = > {
Error ::InternalError ( InternalError ::GrenadInvalidCompressionType )
2021-06-17 00:33:33 +08:00
}
2022-02-16 22:28:48 +08:00
grenad ::Error ::InvalidFormatVersion = > {
Error ::InternalError ( InternalError ::GrenadInvalidFormatVersion )
}
2021-06-14 22:58:38 +08:00
}
}
}
2022-06-16 18:03:43 +08:00
impl From < DocumentsBatchCursorError > for Error {
fn from ( error : DocumentsBatchCursorError ) -> Error {
2022-06-20 19:48:02 +08:00
match error {
DocumentsBatchCursorError ::Grenad ( e ) = > Error ::from ( e ) ,
2022-06-21 20:41:19 +08:00
DocumentsBatchCursorError ::SerdeJson ( e ) = > Error ::from ( InternalError ::from ( e ) ) ,
2022-06-20 19:48:02 +08:00
}
2022-06-16 18:03:43 +08:00
}
}
2021-06-14 22:58:38 +08:00
impl From < Infallible > for Error {
fn from ( _error : Infallible ) -> Error {
unreachable! ( )
}
}
2021-06-10 21:55:22 +08:00
impl From < HeedError > for Error {
fn from ( error : HeedError ) -> Error {
use self ::Error ::* ;
use self ::InternalError ::* ;
use self ::SerializationError ::* ;
use self ::UserError ::* ;
2021-06-09 23:05:46 +08:00
2021-06-10 21:55:22 +08:00
match error {
HeedError ::Io ( error ) = > Error ::from ( error ) ,
2021-06-15 23:20:33 +08:00
HeedError ::Mdb ( MdbError ::MapFull ) = > UserError ( MaxDatabaseSizeReached ) ,
2021-06-10 21:55:22 +08:00
HeedError ::Mdb ( MdbError ::Invalid ) = > UserError ( InvalidStoreFile ) ,
2021-06-14 22:58:38 +08:00
HeedError ::Mdb ( error ) = > InternalError ( Store ( error ) ) ,
2023-11-23 01:21:19 +08:00
// TODO use the encoding
HeedError ::Encoding ( _ ) = > InternalError ( Serialization ( Encoding { db_name : None } ) ) ,
HeedError ::Decoding ( _ ) = > InternalError ( Serialization ( Decoding { db_name : None } ) ) ,
2021-06-10 21:55:22 +08:00
HeedError ::DatabaseClosing = > InternalError ( DatabaseClosing ) ,
2023-11-23 01:21:19 +08:00
HeedError ::BadOpenOptions { .. } = > UserError ( InvalidLmdbOpenOptions ) ,
2021-06-10 21:55:22 +08:00
}
}
2021-06-09 23:05:46 +08:00
}
2021-06-10 23:31:08 +08:00
2023-11-15 22:46:37 +08:00
#[ derive(Debug, Clone, Copy) ]
pub enum FaultSource {
User ,
Runtime ,
Bug ,
Undecided ,
}
impl std ::fmt ::Display for FaultSource {
fn fmt ( & self , f : & mut std ::fmt ::Formatter < '_ > ) -> std ::fmt ::Result {
let s = match self {
FaultSource ::User = > " user error " ,
FaultSource ::Runtime = > " runtime error " ,
FaultSource ::Bug = > " coding error " ,
FaultSource ::Undecided = > " error " ,
} ;
f . write_str ( s )
}
}
2022-03-05 03:34:03 +08:00
#[ test ]
fn conditionally_lookup_for_error_message ( ) {
let prefix = " Attribute `name` is not sortable. " ;
let messages = vec! [
( BTreeSet ::new ( ) , " This index does not have configured sortable attributes. " ) ,
( BTreeSet ::from ( [ " age " . to_string ( ) ] ) , " Available sortable attributes are: `age`. " ) ,
] ;
for ( list , suffix ) in messages {
2023-07-24 17:20:07 +08:00
let err = UserError ::InvalidSortableAttribute {
field : " name " . to_string ( ) ,
valid_fields : list ,
hidden_fields : false ,
} ;
2022-03-05 03:34:03 +08:00
assert_eq! ( err . to_string ( ) , format! ( " {} {} " , prefix , suffix ) ) ;
}
}