168 lines
4.4 KiB
Rust
168 lines
4.4 KiB
Rust
use std::{error::Error, fmt};
|
|
|
|
use crate::VECTOR_ENGINE_PROVIDER;
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
pub enum AudioStatusHint {
|
|
BadRequest,
|
|
ServiceUnavailable,
|
|
BadGateway,
|
|
GatewayTimeout,
|
|
}
|
|
|
|
#[derive(Clone, Debug)]
|
|
pub enum AudioError {
|
|
InvalidConfig {
|
|
provider: &'static str,
|
|
message: String,
|
|
},
|
|
InvalidRequest {
|
|
provider: &'static str,
|
|
message: String,
|
|
},
|
|
Request {
|
|
provider: &'static str,
|
|
message: String,
|
|
endpoint: Option<String>,
|
|
timeout: bool,
|
|
connect: bool,
|
|
request: bool,
|
|
body: bool,
|
|
status_code: Option<u16>,
|
|
source: Option<String>,
|
|
},
|
|
Upstream {
|
|
provider: &'static str,
|
|
message: String,
|
|
upstream_status: u16,
|
|
raw_excerpt: String,
|
|
},
|
|
ResponseParse {
|
|
provider: &'static str,
|
|
message: String,
|
|
raw_excerpt: String,
|
|
},
|
|
MissingAudio {
|
|
provider: &'static str,
|
|
message: String,
|
|
},
|
|
}
|
|
|
|
impl AudioError {
|
|
pub fn provider(&self) -> &'static str {
|
|
match self {
|
|
Self::InvalidConfig { provider, .. }
|
|
| Self::InvalidRequest { provider, .. }
|
|
| Self::Request { provider, .. }
|
|
| Self::Upstream { provider, .. }
|
|
| Self::ResponseParse { provider, .. }
|
|
| Self::MissingAudio { provider, .. } => provider,
|
|
}
|
|
}
|
|
|
|
pub fn message(&self) -> &str {
|
|
match self {
|
|
Self::InvalidConfig { message, .. }
|
|
| Self::InvalidRequest { message, .. }
|
|
| Self::Request { message, .. }
|
|
| Self::Upstream { message, .. }
|
|
| Self::ResponseParse { message, .. }
|
|
| Self::MissingAudio { message, .. } => message,
|
|
}
|
|
}
|
|
|
|
pub fn status_hint(&self) -> AudioStatusHint {
|
|
match self {
|
|
Self::InvalidConfig { .. } => AudioStatusHint::ServiceUnavailable,
|
|
Self::InvalidRequest { .. } => AudioStatusHint::BadRequest,
|
|
Self::Request {
|
|
timeout,
|
|
status_code,
|
|
..
|
|
} if *timeout => AudioStatusHint::GatewayTimeout,
|
|
Self::Request { status_code, .. }
|
|
if status_code.is_some_and(|status| status >= 500) =>
|
|
{
|
|
AudioStatusHint::BadGateway
|
|
}
|
|
Self::Upstream { .. } | Self::ResponseParse { .. } | Self::MissingAudio { .. } => {
|
|
AudioStatusHint::BadGateway
|
|
}
|
|
Self::Request { .. } => AudioStatusHint::BadGateway,
|
|
}
|
|
}
|
|
|
|
pub fn invalid_config(message: impl Into<String>) -> Self {
|
|
Self::InvalidConfig {
|
|
provider: VECTOR_ENGINE_PROVIDER,
|
|
message: message.into(),
|
|
}
|
|
}
|
|
|
|
pub fn invalid_request(message: impl Into<String>) -> Self {
|
|
Self::InvalidRequest {
|
|
provider: VECTOR_ENGINE_PROVIDER,
|
|
message: message.into(),
|
|
}
|
|
}
|
|
|
|
pub fn request(
|
|
message: impl Into<String>,
|
|
endpoint: Option<String>,
|
|
timeout: bool,
|
|
connect: bool,
|
|
request: bool,
|
|
body: bool,
|
|
status_code: Option<u16>,
|
|
source: Option<String>,
|
|
) -> Self {
|
|
Self::Request {
|
|
provider: VECTOR_ENGINE_PROVIDER,
|
|
message: message.into(),
|
|
endpoint,
|
|
timeout,
|
|
connect,
|
|
request,
|
|
body,
|
|
status_code,
|
|
source,
|
|
}
|
|
}
|
|
|
|
pub fn upstream(
|
|
message: impl Into<String>,
|
|
upstream_status: u16,
|
|
raw_excerpt: impl Into<String>,
|
|
) -> Self {
|
|
Self::Upstream {
|
|
provider: VECTOR_ENGINE_PROVIDER,
|
|
message: message.into(),
|
|
upstream_status,
|
|
raw_excerpt: raw_excerpt.into(),
|
|
}
|
|
}
|
|
|
|
pub fn response_parse(message: impl Into<String>, raw_excerpt: impl Into<String>) -> Self {
|
|
Self::ResponseParse {
|
|
provider: VECTOR_ENGINE_PROVIDER,
|
|
message: message.into(),
|
|
raw_excerpt: raw_excerpt.into(),
|
|
}
|
|
}
|
|
|
|
pub fn missing_audio(message: impl Into<String>) -> Self {
|
|
Self::MissingAudio {
|
|
provider: VECTOR_ENGINE_PROVIDER,
|
|
message: message.into(),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl fmt::Display for AudioError {
|
|
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
formatter.write_str(self.message())
|
|
}
|
|
}
|
|
|
|
impl Error for AudioError {}
|