Module errors

Source
Expand description

§Structured Error Types

This module provides structured error types for common scenarios in data fetching and provider operations. Using structured errors instead of generic String errors provides better error handling, debugging, and type safety.

§Examples

§Using ProviderError for general provider failures:

use dioxus_provider::errors::ProviderError;

#[provider]
async fn fetch_user(user_id: u32) -> Result<User, ProviderError> {
    if user_id == 0 {
        return Err(ProviderError::InvalidInput("User ID cannot be zero".to_string()));
    }
     
    let response = api_call(user_id).await
        .map_err(|e| ProviderError::ExternalService {
            service: "UserAPI".to_string(),
            error: e.to_string(),
        })?;
         
    Ok(response)
}

§Using custom domain-specific errors:

use dioxus_provider::errors::ProviderError;
use thiserror::Error;

#[derive(Error, Debug, Clone, PartialEq)]
pub enum UserError {
    #[error("User not found: {id}")]
    NotFound { id: u32 },
    #[error("User is suspended: {reason}")]
    Suspended { reason: String },
    #[error("Permission denied for user {user_id}")]
    PermissionDenied { user_id: u32 },
    #[error("Provider error: {0}")]
    Provider(#[from] ProviderError),
}

#[provider]
async fn fetch_user_profile(user_id: u32) -> Result<UserProfile, UserError> {
    // Implementation
}

Enums§

ApiError
Errors specific to API operations
DatabaseError
Errors specific to database operations
ProviderError
Common error types for provider operations
UserError
Errors specific to user operations

Type Aliases§

ApiResult
Convenience type alias for Results with ApiError
DatabaseResult
Convenience type alias for Results with DatabaseError
ProviderResult
Convenience type alias for Results with ProviderError
UserResult
Convenience type alias for Results with UserError