semantic_search/
error.rs

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