semantic_search/
error.rs

1//! # Error module
2//!
3//! Possible errors.
4
5use base64::DecodeError;
6use doc_for::doc_impl;
7use reqwest::{Error as ReqwestError, header::InvalidHeaderValue};
8use std::array::TryFromSliceError;
9use thiserror::Error;
10
11/// Possible errors.
12#[doc_impl(strip = 1, doc_for = false, gen_attr = "error({doc})")]
13#[derive(Debug, Error)]
14pub enum SenseError {
15    /// Embedding must be 1024-dimensional.
16    DimensionMismatch,
17    /// Malformed API key.
18    MalformedApiKey,
19    /// Request failed.
20    RequestFailed {
21        /// Source of the error.
22        source: ReqwestError,
23    },
24    /// Invalid header value.
25    InvalidHeaderValue,
26    /// Base64 decoding failed.
27    Base64DecodingFailed,
28}
29
30impl From<ReqwestError> for SenseError {
31    /// Error when request fails.
32    fn from(error: ReqwestError) -> Self {
33        Self::RequestFailed { source: error }
34    }
35}
36
37impl From<TryFromSliceError> for SenseError {
38    /// Error when casting slice to array (length mismatch).
39    fn from(_: TryFromSliceError) -> Self {
40        Self::DimensionMismatch
41    }
42}
43
44impl From<Vec<u8>> for SenseError {
45    /// Error when casting `Vec<u8>` to array (length mismatch).
46    fn from(_: Vec<u8>) -> Self {
47        Self::DimensionMismatch
48    }
49}
50
51impl From<Vec<f32>> for SenseError {
52    /// Error when casting `Vec<f32>` to array (length mismatch).
53    fn from(_: Vec<f32>) -> Self {
54        Self::DimensionMismatch
55    }
56}
57
58impl From<InvalidHeaderValue> for SenseError {
59    /// Error when header value is invalid.
60    fn from(_: InvalidHeaderValue) -> Self {
61        Self::InvalidHeaderValue
62    }
63}
64
65impl From<DecodeError> for SenseError {
66    /// Error when base64 decoding fails.
67    fn from(_: DecodeError) -> Self {
68        Self::Base64DecodingFailed
69    }
70}