posthog514client_rs/
error.rs1use std::fmt;
4use thiserror::Error;
5
6#[derive(Debug, Error, Clone)]
8#[non_exhaustive]
9pub enum PostHogError {
10 #[error("failed to send event to PostHog: {message}")]
12 SendEvent {
13 message: String,
14 #[source]
15 source: Option<SendEventErrorKind>,
16 },
17
18 #[error("invalid client configuration: {message}")]
20 Configuration {
21 message: String,
22 #[source]
23 source: Option<ConfigErrorKind>,
24 },
25
26 #[error("serialization error: {message}")]
28 Serialization {
29 message: String,
30 #[source]
31 source: Option<SerializationErrorKind>,
32 },
33}
34
35#[derive(Debug, Error, Clone)]
37#[non_exhaustive]
38pub enum SendEventErrorKind {
39 #[error("network error: {0}")]
41 Network(String),
42
43 #[error("rate limited by PostHog API")]
45 RateLimited,
46
47 #[error("authentication failed: invalid API key")]
49 Authentication,
50}
51
52#[derive(Debug, Error, Clone)]
54#[non_exhaustive]
55pub enum ConfigErrorKind {
56 #[error("invalid API key format")]
58 InvalidApiKey,
59
60 #[error("invalid PostHog URL: {0}")]
62 InvalidUrl(String),
63}
64
65#[derive(Debug, Error, Clone)]
67#[non_exhaustive]
68pub enum SerializationErrorKind {
69 #[error("JSON error: {0}")]
71 Json(String),
72
73 #[error("invalid property value: {0}")]
75 InvalidPropertyValue(String),
76}
77
78impl PostHogError {
79 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 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 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}