From 2a5ba7fe770baffa5b127d0418aecb190712dfb1 Mon Sep 17 00:00:00 2001 From: Lucas Fernandes Nogueira Date: Thu, 17 Jun 2021 11:33:53 -0300 Subject: [PATCH] feat(core): add error message to HTTP API rejection, closes #2003 (#2004) --- .changes/http-error-message.md | 5 +++++ core/tauri/src/api/error.rs | 3 +++ core/tauri/src/api/http.rs | 34 +++++++++++++++++++++++----------- 3 files changed, 31 insertions(+), 11 deletions(-) create mode 100644 .changes/http-error-message.md diff --git a/.changes/http-error-message.md b/.changes/http-error-message.md new file mode 100644 index 000000000..5d59ba17b --- /dev/null +++ b/.changes/http-error-message.md @@ -0,0 +1,5 @@ +--- +"tauri": patch +--- + +Add new `Http` variant to the `tauri::api::Error` enum, including status code and error message on the HTTP API error response. diff --git a/core/tauri/src/api/error.rs b/core/tauri/src/api/error.rs index 89e280276..edceeddba 100644 --- a/core/tauri/src/api/error.rs +++ b/core/tauri/src/api/error.rs @@ -32,6 +32,9 @@ pub enum Error { #[cfg(feature = "reqwest-client")] #[error("Network Error: {0}")] Network(#[from] reqwest::Error), + /// HTTP request error. First parameter is the response status code, and the second is the response text. + #[error("HTTP Error: status code {0} and response `{1}`")] + Http(u16, String), /// HTTP method error. #[error("{0}")] HttpMethod(#[from] http::method::InvalidMethod), diff --git a/core/tauri/src/api/http.rs b/core/tauri/src/api/http.rs index 4eaa3d048..d77a82ae9 100644 --- a/core/tauri/src/api/http.rs +++ b/core/tauri/src/api/http.rs @@ -118,12 +118,18 @@ impl Client { request_builder.send()? }; - let response = response.error_for_status()?; - Ok(Response( - request.response_type.unwrap_or(ResponseType::Json), - response, - request.url, - )) + if response.is_success() { + Ok(Response( + request.response_type.unwrap_or(ResponseType::Json), + response, + request.url, + )) + } else { + Err(super::Error::Http( + response.status().as_u16(), + response.text()?, + )) + } } } @@ -184,11 +190,17 @@ impl Client { request_builder.send().await? }; - let response = response.error_for_status()?; - Ok(Response( - request.response_type.unwrap_or(ResponseType::Json), - response, - )) + if response.status().is_success() { + Ok(Response( + request.response_type.unwrap_or(ResponseType::Json), + response, + )) + } else { + Err(super::Error::Http( + response.status().as_u16(), + response.text().await?, + )) + } } }