use std::fmt;
#[derive(Debug)]
pub enum SDKError {
Authentication(AuthenticationError),
Api(ApiError),
Network(NetworkError),
Streaming(StreamingError),
Tool(ToolError),
Validation(ValidationError),
}
impl fmt::Display for SDKError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Authentication(e) => write!(f, "Authentication error: {e}"),
Self::Api(e) => write!(f, "API error: {e}"),
Self::Network(e) => write!(f, "Network error: {e}"),
Self::Streaming(e) => write!(f, "Streaming error: {e}"),
Self::Tool(e) => write!(f, "Tool error: {e}"),
Self::Validation(e) => write!(f, "Validation error: {e}"),
}
}
}
impl std::error::Error for SDKError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Authentication(e) => Some(e),
Self::Api(e) => Some(e),
Self::Network(e) => Some(e),
Self::Streaming(e) => Some(e),
Self::Tool(e) => Some(e),
Self::Validation(e) => Some(e),
}
}
}
macro_rules! impl_from_for_sdk_error {
($variant:ident, $err_type:ty) => {
impl From<$err_type> for SDKError {
fn from(err: $err_type) -> Self {
Self::$variant(err)
}
}
};
}
impl_from_for_sdk_error!(Authentication, AuthenticationError);
impl_from_for_sdk_error!(Api, ApiError);
impl_from_for_sdk_error!(Network, NetworkError);
impl_from_for_sdk_error!(Streaming, StreamingError);
impl_from_for_sdk_error!(Tool, ToolError);
impl_from_for_sdk_error!(Validation, ValidationError);
#[derive(Debug)]
pub struct AuthenticationError {
pub message: String,
}
impl AuthenticationError {
#[must_use]
pub fn new(message: impl Into<String>) -> Self {
Self {
message: message.into(),
}
}
}
impl fmt::Display for AuthenticationError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.message)
}
}
impl std::error::Error for AuthenticationError {}
#[derive(Debug)]
pub struct ApiError {
pub message: String,
pub status_code: Option<u16>,
}
impl ApiError {
#[must_use]
pub fn new(message: impl Into<String>) -> Self {
Self {
message: message.into(),
status_code: None,
}
}
#[must_use]
pub fn with_status(message: impl Into<String>, status_code: u16) -> Self {
Self {
message: message.into(),
status_code: Some(status_code),
}
}
}
impl fmt::Display for ApiError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(code) = self.status_code {
write!(f, "[HTTP {}] {}", code, self.message)
} else {
write!(f, "{}", self.message)
}
}
}
impl std::error::Error for ApiError {}
#[derive(Debug)]
pub struct NetworkError {
pub message: String,
}
impl NetworkError {
#[must_use]
pub fn new(message: impl Into<String>) -> Self {
Self {
message: message.into(),
}
}
}
impl fmt::Display for NetworkError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.message)
}
}
impl std::error::Error for NetworkError {}
#[derive(Debug)]
pub struct StreamingError {
pub message: String,
}
impl StreamingError {
#[must_use]
pub fn new(message: impl Into<String>) -> Self {
Self {
message: message.into(),
}
}
}
impl fmt::Display for StreamingError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.message)
}
}
impl std::error::Error for StreamingError {}
#[derive(Debug)]
pub struct ToolError {
pub message: String,
}
impl ToolError {
#[must_use]
pub fn new(message: impl Into<String>) -> Self {
Self {
message: message.into(),
}
}
}
impl fmt::Display for ToolError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.message)
}
}
impl std::error::Error for ToolError {}
#[derive(Debug)]
pub struct ValidationError {
pub message: String,
}
impl ValidationError {
#[must_use]
pub fn new(message: impl Into<String>) -> Self {
Self {
message: message.into(),
}
}
}
impl fmt::Display for ValidationError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.message)
}
}
impl std::error::Error for ValidationError {}
#[cfg(test)]
mod tests;