Skip to main content

spoo_me/
errors.rs

1use std::fmt::Display;
2
3use thiserror::Error;
4
5/// Errors that can occur when sending requests (client validation or HTTP errors).
6#[derive(Debug, Error)]
7pub enum ValidationError {
8    /// Password does not meet format requirements.
9    InvalidPasswordFormat(String),
10    /// Alias does not meet format requirements
11    InvalidAliasFormat(String),
12    /// URL does not meet format requirements.
13    InvalidUrlFormat(String),
14    /// Max-clicks must be a positive integer.
15    InvalidMaxClicks(u32),
16    /// Emoji sequence is invalid
17    InvalidEmojiSequence(String),
18}
19
20impl Display for ValidationError {
21    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
22        match self {
23            ValidationError::InvalidPasswordFormat(msg) => {
24                write!(f, "Invalid password format: {}", msg)
25            }
26            ValidationError::InvalidAliasFormat(msg) => write!(f, "Invalid alias format: {}", msg),
27            ValidationError::InvalidUrlFormat(msg) => write!(f, "Invalid URL format: {}", msg),
28            ValidationError::InvalidMaxClicks(value) => {
29                write!(f, "Max-clicks must be a positive integer, got: {}", value)
30            }
31            ValidationError::InvalidEmojiSequence(seq) => {
32                write!(f, "Invalid emoji sequence: {}", seq)
33            }
34        }
35    }
36}
37
38/// Errors that can occur when interacting with the spoo.me API.
39#[derive(Debug, Error)]
40pub enum ApiError {
41    /// The URL does not match the expected format.
42    UrlError,
43    /// The alias is already in use or invalid.
44    AliasError,
45    /// The password provided is incorrect.
46    PasswordError,
47    /// The max clicks value is invalid.
48    MaxClicksError,
49    /// The emoji sequence is already in use or invalid.
50    EmojiError,
51    /// The rate limit for the API has been exceeded.
52    RateLimitExceeded,
53    /// Other unexpected errors from the API.
54    Other(String),
55}
56
57impl Display for ApiError {
58    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
59        match self {
60            ApiError::UrlError => write!(f, "Invalid URL format"),
61            ApiError::AliasError => write!(f, "Alias already in use or invalid"),
62            ApiError::PasswordError => write!(f, "Incorrect password provided"),
63            ApiError::MaxClicksError => write!(f, "Invalid max clicks value"),
64            ApiError::EmojiError => write!(f, "Invalid or already used emoji sequence"),
65            ApiError::RateLimitExceeded => write!(f, "Rate limit exceeded for the API"),
66            ApiError::Other(msg) => write!(f, "API error: {}", msg),
67        }
68    }
69}
70
71/// Errors that can occur when using the URL shortener client.
72#[derive(Debug, Error)]
73pub enum UrlShortenerError {
74    /// Validation errors related to the request parameters.
75    Validation(ValidationError),
76    /// Errors returned by the spoo.me API.
77    Api(ApiError),
78    /// Errors related to the HTTP request, such as connection issues or timeouts.
79    Http(reqwest::Error),
80    /// Errors related to JSON serialization or deserialization.
81    Json(serde_json::Error),
82    /// Other unexpected status codes or errors.
83    Other(String),
84}
85
86impl Display for UrlShortenerError {
87    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
88        match self {
89            UrlShortenerError::Validation(err) => write!(f, "Validation error: {}", err),
90            UrlShortenerError::Api(err) => write!(f, "API error: {:?}", err),
91            UrlShortenerError::Http(err) => write!(f, "HTTP error: {}", err),
92            UrlShortenerError::Json(err) => write!(f, "JSON error: {}", err),
93            UrlShortenerError::Other(msg) => write!(f, "Other error: {}", msg),
94        }
95    }
96}