baserow_rs/error.rs
1use tracing::{error, warn};
2
3/// Errors that can occur during token-based authentication
4///
5/// These errors represent failures that may occur when attempting to
6/// authenticate using API tokens or JWT tokens.
7///
8/// # Example
9/// ```no_run
10/// use baserow_rs::{ConfigBuilder, Baserow, error::TokenAuthError, api::client::BaserowClient};
11///
12/// #[tokio::main]
13/// async fn main() {
14/// let config = ConfigBuilder::new()
15/// .base_url("https://api.baserow.io")
16/// .email("user@example.com")
17/// .password("password")
18/// .build();
19///
20/// let baserow = Baserow::with_configuration(config);
21/// match baserow.token_auth().await {
22/// Ok(client) => println!("Authentication successful"),
23/// Err(TokenAuthError::MissingCredentials(field)) => {
24/// println!("Missing required field: {}", field)
25/// }
26/// Err(e) => println!("Authentication failed: {}", e),
27/// }
28/// }
29/// ```
30#[derive(Debug, thiserror::Error)]
31pub enum TokenAuthError {
32 #[error("Authentication failed: Missing required {0} credentials")]
33 MissingCredentials(&'static str),
34 #[error("Token authentication failed: {0}")]
35 AuthenticationFailed(String),
36 #[error("Network error during authentication: {0}")]
37 NetworkError(#[from] reqwest::Error),
38}
39
40impl TokenAuthError {
41 pub(crate) fn log(&self) {
42 match self {
43 Self::MissingCredentials(field) => {
44 warn!(error = %self, field = %field, "Token authentication failed due to missing credentials");
45 }
46 Self::AuthenticationFailed(msg) => {
47 error!(error = %self, details = %msg, "Token authentication failed");
48 }
49 Self::NetworkError(e) => {
50 error!(error = %self, network_error = %e, "Token authentication failed due to network error");
51 }
52 }
53 }
54}
55
56/// Errors that can occur during file uploads
57///
58/// These errors represent various failures that may occur when
59/// uploading files to Baserow, either directly or via URL.
60///
61/// # Example
62/// ```no_run
63/// use baserow_rs::{ConfigBuilder, Baserow, error::FileUploadError, api::client::BaserowClient};
64/// use std::fs::File;
65///
66/// #[tokio::main]
67/// async fn main() {
68/// let config = ConfigBuilder::new()
69/// .base_url("https://api.baserow.io")
70/// .api_key("your-api-key")
71/// .build();
72///
73/// let baserow = Baserow::with_configuration(config);
74/// let file = File::open("image.jpg").unwrap();
75///
76/// match baserow.upload_file(file, "image.jpg".to_string()).await {
77/// Ok(uploaded) => println!("File uploaded: {}", uploaded.url),
78/// Err(FileUploadError::FileReadError(e)) => {
79/// println!("Failed to read file: {}", e)
80/// }
81/// Err(e) => println!("Upload failed: {}", e),
82/// }
83/// }
84/// ```
85#[derive(Debug, thiserror::Error)]
86pub enum FileUploadError {
87 #[error("File upload failed: Unable to read file - {0}")]
88 FileReadError(#[from] std::io::Error),
89 #[error("File upload failed: Network error - {0}")]
90 UploadError(#[from] reqwest::Error),
91 #[error("File upload failed: Invalid content type provided")]
92 InvalidContentType,
93 #[error("File upload failed: Server responded with unexpected status code {0}")]
94 UnexpectedStatusCode(reqwest::StatusCode),
95 #[error("File upload failed: Invalid URL provided - {0}")]
96 InvalidURL(String),
97}
98
99impl FileUploadError {
100 pub(crate) fn log(&self) {
101 match self {
102 Self::FileReadError(e) => {
103 error!(error = %self, io_error = %e, "File upload failed due to file read error");
104 }
105 Self::UploadError(e) => {
106 error!(error = %self, network_error = %e, "File upload failed due to network error");
107 }
108 Self::InvalidContentType => {
109 warn!(error = %self, "File upload failed due to invalid content type");
110 }
111 Self::UnexpectedStatusCode(status) => {
112 error!(error = %self, status_code = %status, "File upload failed with unexpected status code");
113 }
114 Self::InvalidURL(url) => {
115 warn!(error = %self, url = %url, "File upload failed due to invalid URL");
116 }
117 }
118 }
119}