feat: [desktop] Use thiserror and expect to properly handle errors instead of unwrap

This commit is contained in:
Florian Briand 2024-08-14 10:58:09 +02:00
parent d44c561427
commit 3f476c3114
Signed by: florian_briand
GPG Key ID: CC981B9E6B98E70B
2 changed files with 34 additions and 14 deletions

View File

@ -21,4 +21,5 @@ tokio = "1.39.1"
app = { path = "../app" } app = { path = "../app" }
http = "1.1.0" http = "1.1.0"
bytes = "1.6.1" bytes = "1.6.1"
thiserror = "1.0.63"

View File

@ -1,21 +1,30 @@
use axum::body::{to_bytes, Body};
use axum::Router;
use bytes::Bytes; use bytes::Bytes;
use http::{request, response, Request, Response}; use http::{request, response, Request, Response};
use std::path::PathBuf; use std::path::PathBuf;
use std::sync::Arc; use std::sync::Arc;
use axum::body::{to_bytes, Body};
use axum::Router;
use tauri::path::BaseDirectory; use tauri::path::BaseDirectory;
use tauri::Manager; use tauri::Manager;
use thiserror::Error;
use tokio::sync::{Mutex, MutexGuard}; use tokio::sync::{Mutex, MutexGuard};
use tower::{Service, ServiceExt}; 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( async fn process_tauri_request(
tauri_request: Request<Vec<u8>>, tauri_request: Request<Vec<u8>>,
mut router: MutexGuard<'_, Router>, mut router: MutexGuard<'_, Router>,
) -> Response<Vec<u8>> { ) -> Result<Response<Vec<u8>>, DesktopError> {
let (parts, body): (request::Parts, Vec<u8>) = tauri_request.into_parts(); let (parts, body): (request::Parts, Vec<u8>) = tauri_request.into_parts();
let axum_request: Request<Body> = Request::from_parts(parts, body.into()); let axum_request: Request<Body> = Request::from_parts(parts, body.into());
@ -23,17 +32,16 @@ async fn process_tauri_request(
.as_service() .as_service()
.ready() .ready()
.await .await
.expect("Failed to get ready service from router") .map_err(DesktopError::Infallible)?
.call(axum_request) .call(axum_request)
.await .await
.expect("Could not get response from router"); .map_err(DesktopError::Infallible)?;
let (parts, body): (response::Parts, Body) = axum_response.into_parts(); 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<Vec<u8>> = Response::from_parts(parts, body.into()); let tauri_response: Response<Vec<u8>> = Response::from_parts(parts, body.into());
Ok(tauri_response)
tauri_response
} }
#[cfg_attr(mobile, tauri::mobile_entry_point)] #[cfg_attr(mobile, tauri::mobile_entry_point)]
@ -43,7 +51,7 @@ pub fn run() {
let assets_path: PathBuf = app let assets_path: PathBuf = app
.path() .path()
.resolve("assets", BaseDirectory::Resource) .resolve("assets", BaseDirectory::Resource)
.expect("Path should be resolvable"); .expect("Assets path should be resolvable");
// Adds Axum router to application state // Adds Axum router to application state
// This makes it so we can retrieve it from any app instance (see bellow) // 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 // Spawn a new async task to process the request
tauri::async_runtime::spawn(async move { tauri::async_runtime::spawn(async move {
let router = router.lock().await; let router = router.lock().await;
let response = process_tauri_request(request, router).await; match process_tauri_request(request, router).await {
responder.respond(response); 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::<Vec<u8>>(body.into())
.expect("BAD_REQUEST response should be valid"),
)
}
}
}); });
}) })
.run(tauri::generate_context!()) .run(tauri::generate_context!())