posthog514client_rs/
error.rs

1//! Error types for the PostHog client
2
3use std::fmt;
4use thiserror::Error;
5
6/// Represents errors that can occur when using the PostHog client
7#[derive(Debug, Error, Clone)]
8#[non_exhaustive]
9pub enum PostHogError {
10    /// Failed to send an event to PostHog
11    #[error("failed to send event to PostHog: {message}")]
12    SendEvent {
13        message: String,
14        #[source]
15        source: Option<SendEventErrorKind>,
16    },
17
18    /// Failed to create or validate client configuration
19    #[error("invalid client configuration: {message}")]
20    Configuration {
21        message: String,
22        #[source]
23        source: Option<ConfigErrorKind>,
24    },
25
26    /// Failed to serialize or deserialize data
27    #[error("serialization error: {message}")]
28    Serialization {
29        message: String,
30        #[source]
31        source: Option<SerializationErrorKind>,
32    },
33}
34
35/// Specific kinds of errors that can occur when sending events
36#[derive(Debug, Error, Clone)]
37#[non_exhaustive]
38pub enum SendEventErrorKind {
39    /// Network-related errors
40    #[error("network error: {0}")]
41    Network(String),
42
43    /// Rate limiting errors
44    #[error("rate limited by PostHog API")]
45    RateLimited,
46
47    /// Authentication errors
48    #[error("authentication failed: invalid API key")]
49    Authentication,
50}
51
52/// Configuration-related error kinds
53#[derive(Debug, Error, Clone)]
54#[non_exhaustive]
55pub enum ConfigErrorKind {
56    /// Invalid API key format
57    #[error("invalid API key format")]
58    InvalidApiKey,
59
60    /// Invalid PostHog instance URL
61    #[error("invalid PostHog URL: {0}")]
62    InvalidUrl(String),
63}
64
65/// Serialization-related error kinds
66#[derive(Debug, Error, Clone)]
67#[non_exhaustive]
68pub enum SerializationErrorKind {
69    /// JSON serialization errors
70    #[error("JSON error: {0}")]
71    Json(String),
72
73    /// Invalid event property value
74    #[error("invalid property value: {0}")]
75    InvalidPropertyValue(String),
76}
77
78impl PostHogError {
79    /// Creates a new SendEvent error
80    pub(crate) fn send_event<T: fmt::Display>(
81        message: T,
82        source: Option<SendEventErrorKind>,
83    ) -> Self {
84        Self::SendEvent {
85            message: message.to_string(),
86            source,
87        }
88    }
89
90    /// Creates a new Configuration error
91    pub(crate) fn configuration<T: fmt::Display>(
92        message: T,
93        source: Option<ConfigErrorKind>,
94    ) -> Self {
95        Self::Configuration {
96            message: message.to_string(),
97            source,
98        }
99    }
100
101    /// Creates a new Serialization error
102    pub(crate) fn serialization<T: fmt::Display>(
103        message: T,
104        source: Option<SerializationErrorKind>,
105    ) -> Self {
106        Self::Serialization {
107            message: message.to_string(),
108            source,
109        }
110    }
111}