Skip to content

Commit e2f3d60

Browse files
faabianrmaibin
authored andcommitted
Enable stacktraces and causal chain of causes. Add a couple of tests to validate stacktrace and cause are included in responses (eugenp#6315)
1 parent f894b40 commit e2f3d60

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

spring-boot-libraries/src/main/java/com/baeldung/boot/problem/advice/ExceptionHandler.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,12 @@
55

66
@ControllerAdvice
77
public class ExceptionHandler implements ProblemHandling {
8+
9+
// The causal chain of causes is disabled by default,
10+
// but we can easily enable it by overriding the behavior:
11+
@Override
12+
public boolean isCausalChainsEnabled() {
13+
return true;
14+
}
815

916
}

spring-boot-libraries/src/main/java/com/baeldung/boot/problem/configuration/ProblemDemoConfiguration.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ public class ProblemDemoConfiguration {
1212

1313
@Bean
1414
public ObjectMapper objectMapper() {
15-
return new ObjectMapper().registerModules(new ProblemModule(), new ConstraintViolationProblemModule());
15+
// In this example, stack traces support is enabled by default.
16+
// If you want to disable stack traces just use new ProblemModule() instead of new ProblemModule().withStackTraces()
17+
return new ObjectMapper().registerModules(new ProblemModule().withStackTraces(), new ConstraintViolationProblemModule());
1618
}
1719
}

spring-boot-libraries/src/test/java/com/baeldung/boot/problem/controller/ProblemDemoControllerIntegrationTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.baeldung.boot.problem.controller;
22

3+
import static org.hamcrest.CoreMatchers.containsString;
34
import static org.hamcrest.CoreMatchers.equalTo;
5+
import static org.hamcrest.CoreMatchers.notNullValue;
46
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
57
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
68
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
@@ -71,5 +73,29 @@ public void whenMakeDeleteCall_thenReturnForbiddenProblemResponse() throws Excep
7173
.andExpect(jsonPath("$.detail", equalTo("You can't delete this task")))
7274
.andExpect(status().isForbidden());
7375
}
76+
77+
@Test
78+
public void whenMakeGetCallWithInvalidIdFormat_thenReturnBadRequestResponseWithStackTrace() throws Exception {
79+
mockMvc.perform(get("/tasks/invalid-id").contentType(MediaType.APPLICATION_PROBLEM_JSON_VALUE))
80+
.andDo(print())
81+
.andExpect(jsonPath("$.title", equalTo("Bad Request")))
82+
.andExpect(jsonPath("$.status", equalTo(400)))
83+
.andExpect(jsonPath("$.stacktrace", notNullValue()))
84+
.andExpect(status().isBadRequest());
85+
}
86+
87+
@Test
88+
public void whenMakeGetCallWithInvalidIdFormat_thenReturnBadRequestResponseWithCause() throws Exception {
89+
mockMvc.perform(get("/tasks/invalid-id").contentType(MediaType.APPLICATION_PROBLEM_JSON_VALUE))
90+
.andDo(print())
91+
.andExpect(jsonPath("$.title", equalTo("Bad Request")))
92+
.andExpect(jsonPath("$.status", equalTo(400)))
93+
.andExpect(jsonPath("$.cause", notNullValue()))
94+
.andExpect(jsonPath("$.cause.title", equalTo("Internal Server Error")))
95+
.andExpect(jsonPath("$.cause.status", equalTo(500)))
96+
.andExpect(jsonPath("$.cause.detail", containsString("For input string:")))
97+
.andExpect(jsonPath("$.cause.stacktrace", notNullValue()))
98+
.andExpect(status().isBadRequest());
99+
}
74100

75101
}

0 commit comments

Comments
 (0)