feat(core): add error message to HTTP API rejection, closes #2003 (#2004)

This commit is contained in:
Lucas Fernandes Nogueira 2021-06-17 11:33:53 -03:00 committed by GitHub
parent 1886a9885e
commit 2a5ba7fe77
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 11 deletions

View File

@ -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.

View File

@ -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),

View File

@ -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?,
))
}
}
}