Implémentation d'une première approche de gestion des erreurs #52
@ -21,4 +21,5 @@ tokio = "1.39.1"
|
||||
app = { path = "../app" }
|
||||
http = "1.1.0"
|
||||
bytes = "1.6.1"
|
||||
thiserror = "1.0.63"
|
||||
|
||||
|
@ -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<Vec<u8>>,
|
||||
mut router: MutexGuard<'_, Router>,
|
||||
) -> Response<Vec<u8>> {
|
||||
) -> Result<Response<Vec<u8>>, DesktopError> {
|
||||
let (parts, body): (request::Parts, Vec<u8>) = tauri_request.into_parts();
|
||||
let axum_request: Request<Body> = 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<Vec<u8>> = 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::<Vec<u8>>(body.into())
|
||||
.expect("BAD_REQUEST response should be valid"),
|
||||
)
|
||||
}
|
||||
}
|
||||
});
|
||||
})
|
||||
.run(tauri::generate_context!())
|
||||
|
Loading…
Reference in New Issue
Block a user