Skip to content

Commit 47218ad

Browse files
committed
handled no response body generation case in logger
1 parent 1c514cd commit 47218ad

File tree

1 file changed

+26
-22
lines changed
  • src/test/java/io/github/mfaisalkhatri/api/restfulecommerce

1 file changed

+26
-22
lines changed

src/test/java/io/github/mfaisalkhatri/api/restfulecommerce/Logger.java

+26-22
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public Logger (final APIResponse response) {
1717
this.log = LogManager.getLogger (getClass ());
1818
}
1919

20-
public void logResponseDetails() {
20+
public void logResponseDetails () {
2121
String responseBody = this.response.text ();
2222
if (StringUtils.isNotBlank (responseBody)) {
2323
this.log.info ("Logging Response Details....\n responseHeaders: {}, \nstatusCode: {}, \nresponseBody: {}",
@@ -27,31 +27,35 @@ public void logResponseDetails() {
2727
}
2828

2929
private String prettyPrintJson (final String text) {
30-
try {
31-
final ObjectMapper objectMapper = new ObjectMapper ();
32-
final Object jsonObject = objectMapper.readValue (text, Object.class);
33-
return objectMapper.writerWithDefaultPrettyPrinter ()
34-
.writeValueAsString (jsonObject);
35-
} catch (final JsonProcessingException e) {
36-
this.log.error ("Failed to pretty print JSON: {}", e.getMessage (), e);
37-
return text;
30+
if (StringUtils.isNotBlank (text)) {
31+
try {
32+
final ObjectMapper objectMapper = new ObjectMapper ();
33+
final Object jsonObject = objectMapper.readValue (text, Object.class);
34+
return objectMapper.writerWithDefaultPrettyPrinter ()
35+
.writeValueAsString (jsonObject);
36+
} catch (final JsonProcessingException e) {
37+
this.log.error ("Failed to pretty print JSON: {}", e.getMessage (), e);
38+
return text;
39+
}
3840
}
41+
return "No response body generated!";
3942
}
4043

41-
// private String prettyPrintJson (final String text) {
42-
// String prettyPrintJson = "";
43-
// if (text != null && !text.isBlank() && !text.isEmpty()) {
44-
// try {
44+
// private String prettyPrintJson (final String text) {
45+
// String prettyPrintJson = "";
46+
// if (text != null && !text.isBlank() && !text.isEmpty()) {
47+
// try {
4548
//
46-
// final ObjectMapper objectMapper = new ObjectMapper ();
47-
// final Object jsonObject = objectMapper.readValue (text, Object.class);
48-
// prettyPrintJson = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonObject);
49-
// return prettyPrintJson;
50-
// } catch (final JsonProcessingException e) {
51-
// this.log.error ("Error Printing Pretty Json : {}", e.getMessage ());
49+
// final ObjectMapper objectMapper = new ObjectMapper ();
50+
// final Object jsonObject = objectMapper.readValue (text, Object.class);
51+
// prettyPrintJson = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonObject);
52+
// return prettyPrintJson;
53+
// } catch (final JsonProcessingException e) {
54+
// this.log.error ("Error Printing Pretty Json : {}", e.getMessage ());
55+
// }
5256
// }
57+
// this.log.info ("No response body generated!");
58+
// return null;
5359
// }
54-
// this.log.info ("No response body generated!");
55-
// return null;
56-
// }
5760
}
61+

0 commit comments

Comments
 (0)