Skip to content

Commit

Permalink
Adopt HTTP API client 0.10.0 to produce more detailed errors
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelklishin committed Dec 26, 2024
1 parent 9faf269 commit 1f13dbf
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 33 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#![allow(clippy::result_large_err)]

use clap::ArgMatches;
use rabbitmq_http_client::commons;
use rabbitmq_http_client::commons::ExchangeType;
Expand Down
25 changes: 16 additions & 9 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@

use rabbitmq_http_client::error::Error as ApiClientError;
use rabbitmq_http_client::{blocking_api::HttpClientError, responses::HealthCheckFailureDetails};
use reqwest::blocking::Response;
use reqwest::{header::InvalidHeaderValue, StatusCode};
use reqwest::{
header::{HeaderMap, InvalidHeaderValue},
StatusCode,
};
use url::Url;

#[derive(thiserror::Error, Debug)]
pub enum CommandRunError {
Expand All @@ -24,12 +27,16 @@ pub enum CommandRunError {
#[error("API responded with a client error: status code of {status_code}")]
ClientError {
status_code: StatusCode,
response: Option<Response>,
url: Option<Url>,
body: Option<String>,
headers: Option<HeaderMap>,
},
#[error("API responded with a client error: status code of {status_code}")]
ServerError {
status_code: StatusCode,
response: Option<Response>,
url: Option<Url>,
body: Option<String>,
headers: Option<HeaderMap>,
},
#[error("Health check failed")]
HealthCheckFailed {
Expand All @@ -52,18 +59,18 @@ pub enum CommandRunError {
impl From<HttpClientError> for CommandRunError {
fn from(value: HttpClientError) -> Self {
match value {
ApiClientError::ClientErrorResponse { status_code, response, .. } => {
Self::ClientError { status_code, response }
ApiClientError::ClientErrorResponse { status_code, url, body, headers, .. } => {
Self::ClientError { status_code, url, body, headers }
},
ApiClientError::ServerErrorResponse { status_code, response, .. } => {
Self::ServerError { status_code, response }
ApiClientError::ServerErrorResponse { status_code, url, body, headers, .. } => {
Self::ServerError { status_code, url, body, headers }
},
ApiClientError::HealthCheckFailed { path, details, status_code } => {
Self::HealthCheckFailed { health_check_path: path, details, status_code }
},
ApiClientError::NotFound => Self::NotFound,
ApiClientError::MultipleMatchingBindings => Self::ConflictingOptions {
message: "multiple bindings match, cannot determing which binding to delete without explicitly provided binding properties".to_owned()
message: "multiple bindings match, cannot determine which binding to delete without explicitly provided binding properties".to_owned()
},
ApiClientError::InvalidHeaderValue { error } => {
Self::InvalidHeaderValue { error }
Expand Down
3 changes: 1 addition & 2 deletions src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,7 @@ impl ResultHandler {
Err(error) => match error {
ClientError::ClientErrorResponse {
status_code: http_code,
response: _,
backtrace: _,
..
} if http_code == StatusCode::NOT_FOUND => {
if self.idempotently {
self.exit_code = Some(ExitCode::Ok)
Expand Down
42 changes: 24 additions & 18 deletions src/tables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use rabbitmq_http_client::blocking_api::{HttpClientError, HttpClientResponse};
use rabbitmq_http_client::blocking_api::HttpClientError;
use rabbitmq_http_client::responses::{HealthCheckFailureDetails, Overview};
use reqwest::StatusCode;
use tabled::settings::Panel;
use tabled::{Table, Tabled};
use url::Url;

#[derive(Tabled)]
struct OverviewRow<'a> {
Expand Down Expand Up @@ -102,14 +103,16 @@ pub fn failure_details(error: &HttpClientError) -> Table {
match error {
HttpClientError::ClientErrorResponse {
status_code,
response,
backtrace: _,
} => generic_failed_request_details(status_code, response),
url,
body,
..
} => generic_failed_request_details(status_code, url, body),
HttpClientError::ServerErrorResponse {
status_code,
response,
backtrace: _,
} => generic_failed_request_details(status_code, response),
url,
body,
..
} => generic_failed_request_details(status_code, url, body),
HttpClientError::HealthCheckFailed {
status_code,
path,
Expand Down Expand Up @@ -240,10 +243,14 @@ pub fn failure_details(error: &HttpClientError) -> Table {

fn generic_failed_request_details(
status_code: &StatusCode,
response: &Option<HttpClientResponse>,
url: &Option<Url>,
body: &Option<String>,
) -> Table {
let status_code_s = status_code.to_string();
let mut data = vec![
let url_s = url.clone().unwrap().to_string();
let body_s = body.clone().unwrap_or("N/A".to_string());

let data = vec![
RowOfTwoStrings {
key: "result",
value: "request failed",
Expand All @@ -252,16 +259,15 @@ fn generic_failed_request_details(
key: "status_code",
value: status_code_s.as_str(),
},
RowOfTwoStrings {
key: "url",
value: url_s.as_str(),
},
RowOfTwoStrings {
key: "body",
value: body_s.as_str(),
},
];
match response {
None => (),
Some(ref val) => {
data.push(RowOfTwoStrings {
key: "request URL",
value: val.url().as_str(),
});
}
}

let tb = Table::builder(data);
tb.build()
Expand Down

0 comments on commit 1f13dbf

Please sign in to comment.