Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Structured errors

Manav Rathi69c0246ef40b
diff --git a/mobile/packages/rust/pubspec.yaml b/mobile/packages/rust/pubspec.yaml
index b5ef7aab3c..7c2477c943 100644
--- a/mobile/packages/rust/pubspec.yaml
+++ b/mobile/packages/rust/pubspec.yaml
@@ -11,6 +11,7 @@ dependencies:
flutter:
sdk: flutter
flutter_rust_bridge: 2.11.1
+ freezed_annotation: ^3.1.0
plugin_platform_interface: ^2.0.2
dev_dependencies:
@@ -21,6 +22,8 @@ dev_dependencies:
flutter_lints: ^5.0.0
integration_test:
sdk: flutter
+ build_runner: ^2.10.4
+ freezed: ^3.2.3
flutter:
plugin:
diff --git a/mobile/packages/rust/rust/src/api/http.rs b/mobile/packages/rust/rust/src/api/http.rs
index afee8b279b..790ddb5333 100644
--- a/mobile/packages/rust/rust/src/api/http.rs
+++ b/mobile/packages/rust/rust/src/api/http.rs
@@ -1,8 +1,40 @@
//! FRB bindings for the HTTP client
-use ente_core::http::HttpClient as CoreHttpClient;
+use ente_core::http::{Error as CoreError, HttpClient as CoreHttpClient};
use flutter_rust_bridge::frb;
+/// HTTP client errors.
+#[frb]
+pub enum HttpError {
+ /// Network error - connection failed, timeout, etc.
+ Network {
+ /// Error message.
+ message: String,
+ },
+ /// Server return an HTTP error status.
+ Http {
+ /// Error message.
+ message: String,
+ /// HTTP status code.
+ status: u16,
+ },
+ /// Failed to parse response.
+ Parse {
+ /// Error message.
+ message: String,
+ },
+}
+
+impl From<CoreError> for HttpError {
+ fn from(e: CoreError) -> Self {
+ match e {
+ CoreError::Network(msg) => HttpError::Network { message: msg },
+ CoreError::Http { status, message } => HttpError::Http { status, message },
+ CoreError::Parse(msg) => HttpError::Parse { message: msg },
+ }
+ }
+}
+
/// HTTP client fro making requests to the Ente API.
#[frb(opaque)]
pub struct HttpClient {
@@ -19,7 +51,7 @@ impl HttpClient {
}
/// GET request, returns response body as text.
- pub async fn get(&self, path: String) -> Result<String, String> {
- self.inner.get(&path).await.map_err(|e| e.to_string())
+ pub async fn get(&self, path: String) -> Result<String, HttpError> {
+ self.inner.get(&path).await.map_err(|e| e.into())
}
}
diff --git a/rust/core/src/http.rs b/rust/core/src/http.rs
index 4ef15ec8a1..75f2d1de98 100644
--- a/rust/core/src/http.rs
+++ b/rust/core/src/http.rs
@@ -6,13 +6,41 @@ use thiserror::Error;
/// HTTP client errors.
#[derive(Error, Debug)]
pub enum Error {
- /// A network or HTTP protocol error occurred during the request.
- #[error("HTTP request failed: {0}")]
- Request(#[from] reqwest::Error),
+ /// Network error - connection failed, timeout, etc.
+ #[error("Network error: {0}")]
+ Network(String),
+
+ /// Server retured an HTTP error status.
+ #[error("HTTP {status}: {message}")]
+ Http {
+ /// HTTP status code.
+ status: u16,
+ /// Error message or response body.
+ message: String,
+ },
/// Failed to parse JSON response.
#[error("JSON parse error: {0}")]
- Json(#[from] serde_json::Error),
+ Parse(String),
+}
+
+impl From<reqwest::Error> for Error {
+ fn from(e: reqwest::Error) -> Self {
+ if let Some(status) = e.status() {
+ Error::Http {
+ status: status.as_u16(),
+ message: e.to_string(),
+ }
+ } else {
+ Error::Network(e.to_string())
+ }
+ }
+}
+
+impl From<serde_json::Error> for Error {
+ fn from(e: serde_json::Error) -> Self {
+ Error::Parse(e.to_string())
+ }
}
/// Response from the /ping endpoint
@@ -51,6 +79,7 @@ impl HttpClient {
pub async fn get(&self, path: &str) -> Result<String, Error> {
let url = format!("{}{}", self.base_url, path);
let response = self.client.get(&url).send().await?;
+ let response = response.error_for_status()?;
let text = response.text().await?;
Ok(text)
}
@@ -72,4 +101,12 @@ mod tests {
let client = HttpClient::new("https://api.ente.io");
assert_eq!(client.base_url, "https://api.ente.io");
}
+
+ #[test]
+ fn test_parse_error_conversion() {
+ let bad_json = "not json";
+ let err: Result<PingResponse, Error> = serde_json::from_str(bad_json).map_err(|e| e.into());
+ println!("{:?}", err);
+ assert!(matches!(err, Err(Error::Parse(_))))
+ }
}
diff --git a/web/packages/wasm/src/http.rs b/web/packages/wasm/src/http.rs
index 3061ae45dd..3903da1c1a 100644
--- a/web/packages/wasm/src/http.rs
+++ b/web/packages/wasm/src/http.rs
@@ -1,8 +1,59 @@
//! WASM bindings for the HTTP client.
-use ente_core::http::HttpClient as CoreHttpClient;
+use ente_core::http::{Error as CoreError, HttpClient as CoreHttpClient};
use wasm_bindgen::prelude::*;
+/// HTTP client error.
+#[wasm_bindgen]
+pub struct HttpError {
+ code: String,
+ message: String,
+ status: Option<u16>,
+}
+
+#[wasm_bindgen]
+impl HttpError {
+ /// Error code: "network", "http", or "parse".
+ #[wasm_bindgen(getter)]
+ pub fn code(&self) -> String {
+ self.code.clone()
+ }
+
+ /// Error message.
+ #[wasm_bindgen(getter)]
+ pub fn message(&self) -> String {
+ self.message.clone()
+ }
+
+ /// HTTP status code (only for "http" errors).
+ #[wasm_bindgen(getter)]
+ pub fn status(&self) -> Option<u16> {
+ self.status
+ }
+}
+
+impl From<CoreError> for HttpError {
+ fn from(e: CoreError) -> Self {
+ match e {
+ CoreError::Network(message) => HttpError {
+ code: "network".to_string(),
+ message,
+ status: None,
+ },
+ CoreError::Http { status, message } => HttpError {
+ code: "http".to_string(),
+ message,
+ status: Some(status),
+ },
+ CoreError::Parse(message) => HttpError {
+ code: "parse".to_string(),
+ message,
+ status: None,
+ },
+ }
+ }
+}
+
/// HTTP client for making requests to the Ente API.
#[wasm_bindgen]
pub struct HttpClient {
@@ -20,10 +71,7 @@ impl HttpClient {
}
/// GET request, returns response body as text.
- pub async fn get(&self, path: &str) -> Result<String, JsError> {
- self.inner
- .get(path)
- .await
- .map_err(|e| JsError::new(&e.to_string()))
+ pub async fn get(&self, path: &str) -> Result<String, HttpError> {
+ self.inner.get(path).await.map_err(|e| e.into())
}
}