Skip to content

Commit

Permalink
status.rs: Make json output more verbose (#1367)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* 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 <[email protected]>

---------

Signed-off-by: Norbert Kamiński <[email protected]>
  • Loading branch information
Norbert Kamiński authored Jan 30, 2024
1 parent 54e6386 commit 2a95944
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
8 changes: 8 additions & 0 deletions fixtures/TEST_DETAILED_JSON_OUTPUT_ERROR.md
Original file line number Diff line number Diff line change
@@ -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.
30 changes: 30 additions & 0 deletions lychee-bin/tests/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand Down
4 changes: 4 additions & 0 deletions lychee-lib/src/types/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())?;
Expand Down

0 comments on commit 2a95944

Please sign in to comment.