Skip to content

Commit bc697ff

Browse files
committed
Display server response on 4xx-5xx
Signed-off-by: Didier Wenzek <[email protected]>
1 parent 7f90e3e commit bc697ff

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

crates/core/tedge/src/cli/http/command.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::cli::http::cli::Content;
22
use crate::command::Command;
33
use crate::log::MaybeFancy;
4+
use anyhow::anyhow;
45
use anyhow::Error;
56
use hyper::http::HeaderValue;
67
use reqwest::blocking;
@@ -74,10 +75,27 @@ impl HttpCommand {
7475
}
7576

7677
fn send(request: blocking::RequestBuilder) -> Result<(), Error> {
77-
let http_result = request.send()?;
78-
let mut http_response = http_result.error_for_status()?;
79-
http_response.copy_to(&mut std::io::stdout())?;
80-
Ok(())
78+
let mut http_result = request.send()?;
79+
let status = http_result.status();
80+
if status.is_success() {
81+
http_result.copy_to(&mut std::io::stdout())?;
82+
Ok(())
83+
} else {
84+
let kind = if status.is_client_error() {
85+
"HTTP client error"
86+
} else if status.is_server_error() {
87+
"HTTP server error"
88+
} else {
89+
"HTTP error"
90+
};
91+
let error = format!(
92+
"{kind}: {} {}\n{}",
93+
status.as_u16(),
94+
status.canonical_reason().unwrap_or(""),
95+
http_result.text().unwrap_or("".to_string())
96+
);
97+
Err(anyhow!(error))?
98+
}
8199
}
82100
}
83101

tests/RobotFramework/tests/tedge/tedge_http.robot

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ Accessing file-transfer from a child device
4343
Execute Command tedge http delete /tedge/file-transfer/target
4444
Execute Command tedge http get /tedge/file-transfer/target exp_exit_code=1
4545

46+
Displaying server errors
47+
${error_msg}= Execute Command
48+
... tedge http post /tedge/entity-store/v1/entities '{"@topic-id": "device/a//", "@type": "child-device", "@parent": "device/unknown//"}' 2>&1
49+
... exp_exit_code=1
50+
Should Contain ${error_msg} 400 Bad Request
51+
Should Contain ${error_msg} Specified parent "device/unknown//" does not exist in the store
52+
4653

4754
*** Keywords ***
4855
Setup Child Device

0 commit comments

Comments
 (0)