status.rs: Make json output more verbose (#1367)

* status.rs: Make json output more verbose

Currently if the status response has no status code, json output
contains only a text field which gives no real information about
the cause of the problem. The patch adds field with more detailed
information when the status response contains some details.

Signed-off-by: Norbert Kamiński <norbert.kaminski@3mdeb.com>

* cli.rs: Test parsing of error details in JSON format

Some network error such as SSL has no status code but it can be
identified by error status details. This patch adds a test case to
verify if the error details are parsed properly in the json format.

Signed-off-by: Norbert Kamiński <norbert.kaminski@3mdeb.com>

---------

Signed-off-by: Norbert Kamiński <norbert.kaminski@3mdeb.com>
This commit is contained in:
Norbert Kamiński 2024-01-30 23:58:18 +01:00 committed by GitHub
parent 54e63861b8
commit 2a95944ef5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 42 additions and 0 deletions

View file

@ -0,0 +1,8 @@
# Test detailed JSON output error
This file is used to test if the error details are parsed properly in the json
format.
[The website](https://expired.badssl.com/) produce SSL expired certificate
error. Such network error has no status code but it can be identified by error
status details.

View file

@ -133,6 +133,36 @@ mod cli {
Ok(())
}
#[test]
fn test_detailed_json_output_on_error() -> Result<()> {
let test_path = fixtures_path().join("TEST_DETAILED_JSON_OUTPUT_ERROR.md");
let mut cmd = main_command();
cmd.arg("--format")
.arg("json")
.arg(&test_path)
.assert()
.failure()
.code(2);
let output = cmd.output()?;
// Check that the output is valid JSON
assert!(serde_json::from_slice::<Value>(&output.stdout).is_ok());
// Parse site error status from the fail_map
let output_json = serde_json::from_slice::<Value>(&output.stdout).unwrap();
let site_error_status = &output_json["fail_map"][&test_path.to_str().unwrap()][0]["status"];
assert_eq!(
"error:0A000086:SSL routines:tls_post_process_server_certificate:\
certificate verify failed:../ssl/statem/statem_clnt.c:1883: \
(certificate has expired)",
site_error_status["details"]
);
Ok(())
}
#[test]
fn test_exclude_all_private() -> Result<()> {
test_json_output!(

View file

@ -68,6 +68,10 @@ impl Serialize for Status {
s = serializer.serialize_struct("Status", 2)?;
s.serialize_field("text", &self.to_string())?;
s.serialize_field("code", &code.as_u16())?;
} else if let Some(details) = self.details() {
s = serializer.serialize_struct("Status", 2)?;
s.serialize_field("text", &self.to_string())?;
s.serialize_field("details", &details.to_string())?;
} else {
s = serializer.serialize_struct("Status", 1)?;
s.serialize_field("text", &self.to_string())?;