brawl_rs/errors.rs
1//! Error types for the Brawl API client.
2//!
3//! This module provides error handling types that can occur when interacting
4//! with the Brawl Stars API.
5
6use serde::Deserialize;
7use std::fmt::Formatter;
8
9/// An error returned by the Brawl Stars API.
10///
11/// This struct contains error information returned by the API servers,
12/// including a reason code and a descriptive message.
13#[derive(Debug, Deserialize)]
14pub struct ApiError {
15 pub reason: Option<String>,
16 pub message: Option<String>,
17 #[serde(rename = "type")]
18 pub kind: Option<String>,
19}
20
21/// Error type for all operations in the Brawl API client.
22///
23/// This enum represents all possible error conditions that can occur
24/// when using this library:
25///
26/// - **Api** - Error returned by the Brawl Stars API (e.g., invalid player tag)
27/// - **Network** - Network communication error (e.g., connection timeout)
28/// - **Serialization** - JSON serialization/deserialization error
29///
30#[derive(Debug)]
31pub enum BrawlError {
32 /// An error response from the Brawl Stars API
33 Api(ApiError),
34 /// A network/HTTP communication error
35 Network(reqwest::Error),
36 /// A JSON serialization or deserialization error
37 Serialization(reqwest::Error),
38}
39
40impl std::fmt::Display for BrawlError {
41 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
42 match self {
43 BrawlError::Api(e) => {
44 let reason = e.reason.as_deref().unwrap_or("unknown reason");
45 let message = e.message.as_deref().unwrap_or("no message provided");
46 let type_ = e.kind.as_deref().unwrap_or("unknown type");
47
48 write!(f, "API Error: {} ({}) [{}]", reason, message, type_)
49 }
50 BrawlError::Network(e) => write!(f, "Network Error: {e}"),
51 BrawlError::Serialization(e) => write!(f, "Serialization Error: {}", e),
52 }
53 }
54}
55
56impl std::error::Error for BrawlError {}