diff --git a/crates/desktop/Cargo.toml b/crates/desktop/Cargo.toml index 0ddbb50..59e5d2f 100644 --- a/crates/desktop/Cargo.toml +++ b/crates/desktop/Cargo.toml @@ -21,4 +21,5 @@ tokio = "1.39.1" app = { path = "../app" } http = "1.1.0" bytes = "1.6.1" +thiserror = "1.0.63" diff --git a/crates/desktop/src/lib.rs b/crates/desktop/src/lib.rs index 3e7ca06..ac8bc77 100644 --- a/crates/desktop/src/lib.rs +++ b/crates/desktop/src/lib.rs @@ -1,21 +1,30 @@ +use axum::body::{to_bytes, Body}; +use axum::Router; use bytes::Bytes; use http::{request, response, Request, Response}; use std::path::PathBuf; use std::sync::Arc; - -use axum::body::{to_bytes, Body}; - -use axum::Router; - use tauri::path::BaseDirectory; use tauri::Manager; +use thiserror::Error; use tokio::sync::{Mutex, MutexGuard}; use tower::{Service, ServiceExt}; +#[derive(Error, Debug)] +pub enum DesktopError { + #[error("Axum error:\n{0}")] + Axum(#[from] axum::Error), + #[error("Infallible error")] + Infallible(#[from] std::convert::Infallible), +} + +/// Process requests sent to Tauri (with the `axum://` protocol) and handle them with Axum +/// When an error occurs, this function is expected to panic, which should result in a 500 error +/// being sent to the client, so we let the client handle the error recovering async fn process_tauri_request( tauri_request: Request>, mut router: MutexGuard<'_, Router>, -) -> Response> { +) -> Result>, DesktopError> { let (parts, body): (request::Parts, Vec) = tauri_request.into_parts(); let axum_request: Request = Request::from_parts(parts, body.into()); @@ -23,17 +32,16 @@ async fn process_tauri_request( .as_service() .ready() .await - .expect("Failed to get ready service from router") + .map_err(DesktopError::Infallible)? .call(axum_request) .await - .expect("Could not get response from router"); + .map_err(DesktopError::Infallible)?; let (parts, body): (response::Parts, Body) = axum_response.into_parts(); - let body: Bytes = to_bytes(body, usize::MAX).await.unwrap_or_default(); + let body: Bytes = to_bytes(body, usize::MAX).await?; let tauri_response: Response> = Response::from_parts(parts, body.into()); - - tauri_response + Ok(tauri_response) } #[cfg_attr(mobile, tauri::mobile_entry_point)] @@ -43,7 +51,7 @@ pub fn run() { let assets_path: PathBuf = app .path() .resolve("assets", BaseDirectory::Resource) - .expect("Path should be resolvable"); + .expect("Assets path should be resolvable"); // Adds Axum router to application state // This makes it so we can retrieve it from any app instance (see bellow) @@ -60,8 +68,19 @@ pub fn run() { // Spawn a new async task to process the request tauri::async_runtime::spawn(async move { let router = router.lock().await; - let response = process_tauri_request(request, router).await; - responder.respond(response); + match process_tauri_request(request, router).await { + Ok(response) => responder.respond(response), + Err(err) => { + let body = format!("Failed to process an axum:// request:\n{}", err); + responder.respond( + http::Response::builder() + .status(http::StatusCode::BAD_REQUEST) + .header(http::header::CONTENT_TYPE, "text/plain") + .body::>(body.into()) + .expect("BAD_REQUEST response should be valid"), + ) + } + } }); }) .run(tauri::generate_context!())