mod private
{
use error_tools::Error;
use std::fmt;
pub type Result< T > = core::result::Result< T, HuggingFaceError >;
#[ derive( Debug, Clone ) ]
pub enum HuggingFaceError
{
Api( ApiErrorWrap ),
Http( String ),
Authentication( String ),
Validation( String ),
RateLimit( String ),
ModelUnavailable( String ),
Stream( String ),
Serialization( String ),
InvalidArgument( String ),
Generic( String ),
}
impl fmt::Display for HuggingFaceError
{
#[ inline ]
fn fmt( &self, f : &mut fmt::Formatter< '_ > ) -> fmt::Result
{
match self
{
HuggingFaceError::Api( e ) => write!( f, "API error : {e}" ),
HuggingFaceError::Http( msg ) => write!( f, "HTTP error : {msg}" ),
HuggingFaceError::Authentication( msg ) => write!( f, "Authentication error : {msg}" ),
HuggingFaceError::Validation( msg ) => write!( f, "Validation error : {msg}" ),
HuggingFaceError::RateLimit( msg ) => write!( f, "Rate limit error : {msg}" ),
HuggingFaceError::ModelUnavailable( msg ) => write!( f, "Model unavailable : {msg}" ),
HuggingFaceError::Stream( msg ) => write!( f, "Stream error : {msg}" ),
HuggingFaceError::Serialization( msg ) => write!( f, "Serialization error : {msg}" ),
HuggingFaceError::InvalidArgument( msg ) => write!( f, "Invalid argument : {msg}" ),
HuggingFaceError::Generic( msg ) => write!( f, "Generic error : {msg}" ),
}
}
}
impl std::error::Error for HuggingFaceError
{}
#[ derive( Debug, Clone ) ]
pub struct ApiErrorWrap
{
pub message : String,
pub error_type : Option< String >,
pub status_code : Option< u16 >,
}
impl ApiErrorWrap
{
#[ inline ]
#[ must_use ]
pub fn new( message : impl Into< String > ) -> Self
{
Self
{
message : message.into(),
error_type : None,
status_code : None,
}
}
#[ inline ]
#[ must_use ]
pub fn with_error_type( mut self, error_type : impl Into< String > ) -> Self
{
self.error_type = Some( error_type.into() );
self
}
#[ inline ]
#[ must_use ]
pub fn with_status_code( mut self, status_code : u16 ) -> Self
{
self.status_code = Some( status_code );
self
}
}
impl fmt::Display for ApiErrorWrap
{
#[ inline ]
fn fmt( &self, f : &mut fmt::Formatter< '_ > ) -> fmt::Result
{
if let Some( error_type ) = &self.error_type
{
write!( f, "[{error_type}] {}", self.message )?;
}
else
{
write!( f, "{}", self.message )?;
}
if let Some( status_code ) = self.status_code
{
write!( f, " (HTTP {status_code})" )?;
}
Ok( () )
}
}
#[ cfg( feature = "client" ) ]
#[ inline ]
#[ must_use ]
pub fn map_deserialization_error( e : &reqwest::Error ) -> HuggingFaceError
{
HuggingFaceError::Serialization( e.to_string() )
}
impl From< Error > for HuggingFaceError
{
#[ inline ]
fn from( e : Error ) -> Self
{
HuggingFaceError::Generic( e.to_string() )
}
}
}
crate::mod_interface!
{
exposed use private::HuggingFaceError;
exposed use private::ApiErrorWrap;
exposed use private::Result;
#[ cfg( feature = "client" ) ]
exposed use private::map_deserialization_error;
}