Skip to content

Commit 1bdfd52

Browse files
authored
Merge pull request #55 from bugsnag/add-spring-handled-exception-example
Add Spring @ExceptionHandler example
2 parents 5b78dc3 + c85e6cc commit 1bdfd52

3 files changed

Lines changed: 57 additions & 12 deletions

File tree

examples/spring-web/src/main/java/com/bugsnag/example/spring/web/ApplicationRestController.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,12 @@ public class ApplicationRestController {
2121
@Autowired
2222
private ApplicationContext applicationContext;
2323

24-
private final String links =
25-
"<a href=\"/send-handled-exception\">Send a handled exception to Bugsnag</a><br/>"
26-
+ "<a href=\"/send-handled-exception-info\">Send a handled exception to Bugsnag with INFO severity</a><br/>"
27-
+ "<a href=\"/send-handled-exception-with-metadata\">Send a handled exception to Bugsnag with custom MetaData</a><br/>"
28-
+ "<a href=\"/send-unhandled-exception\">Send an unhandled exception to Bugsnag</a><br/>"
29-
+ "<a href=\"/shutdown\">Shutdown the application</a><br/>";
24+
@Autowired
25+
private String exampleWebsiteLinks;
3026

3127
@RequestMapping("/")
3228
public String index() {
33-
return links;
29+
return exampleWebsiteLinks;
3430
}
3531

3632
@RequestMapping("/send-handled-exception")
@@ -42,7 +38,7 @@ public String sendHandledException() {
4238
bugsnag.notify(e);
4339
}
4440

45-
return links + "<br/>Sent a handled exception to Bugsnag";
41+
return exampleWebsiteLinks + "<br/>Sent a handled exception to Bugsnag";
4642
}
4743

4844
@RequestMapping("/send-handled-exception-info")
@@ -54,7 +50,7 @@ public String sendHandledExceptionInfo() {
5450
bugsnag.notify(e, Severity.INFO);
5551
}
5652

57-
return links + "<br/>Sent a handled exception to Bugsnag with INFO severity";
53+
return exampleWebsiteLinks + "<br/>Sent a handled exception to Bugsnag with INFO severity";
5854
}
5955

6056
@RequestMapping("/send-handled-exception-with-metadata")
@@ -70,7 +66,7 @@ public String sendHandledExceptionWithMetadata() {
7066
});
7167
}
7268

73-
return links + "<br/>Sent a handled exception to Bugsnag with custom MetaData";
69+
return exampleWebsiteLinks + "<br/>Sent a handled exception to Bugsnag with custom MetaData";
7470
}
7571

7672
@RequestMapping("/send-unhandled-exception")
@@ -90,11 +86,17 @@ public void run() {
9086
// Wait for unhandled exception thread to finish
9187
thread.join();
9288

93-
return links + "<br/>Sent an unhandled exception to Bugsnag";
89+
return exampleWebsiteLinks + "<br/>Sent an unhandled exception to Bugsnag";
90+
}
91+
92+
@RequestMapping("/send-spring-handled-exception")
93+
public String sendSpringHandledException() {
94+
LOGGER.info("Sending a Spring handled exception to Bugsnag");
95+
throw new RuntimeException("Spring handled exception");
9496
}
9597

9698
@RequestMapping("/shutdown")
97-
public void shutdown() throws InterruptedException {
99+
public void shutdown() {
98100
LOGGER.info("Shutting down application");
99101

100102
SpringApplication.exit(applicationContext);

examples/spring-web/src/main/java/com/bugsnag/example/spring/web/Config.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,15 @@ public Bugsnag bugsnag() {
3535

3636
return bugsnag;
3737
}
38+
39+
// The entire code for the example website
40+
@Bean
41+
public String exampleWebsiteLinks() {
42+
return "<a href=\"/send-handled-exception\">Send a handled exception to Bugsnag</a><br/>"
43+
+ "<a href=\"/send-handled-exception-info\">Send a handled exception to Bugsnag with INFO severity</a><br/>"
44+
+ "<a href=\"/send-handled-exception-with-metadata\">Send a handled exception to Bugsnag with custom MetaData</a><br/>"
45+
+ "<a href=\"/send-unhandled-exception\">Send an unhandled exception to Bugsnag</a><br/>"
46+
+ "<a href=\"/send-spring-handled-exception\">Send an exception handled by Spring @ExceptionHandler to Bugsnag</a><br/>"
47+
+ "<a href=\"/shutdown\">Shutdown the application</a><br/>";
48+
}
3849
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.bugsnag.example.spring.web;
2+
3+
import com.bugsnag.Bugsnag;
4+
import com.bugsnag.Severity;
5+
import org.apache.log4j.Logger;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.web.bind.annotation.ControllerAdvice;
8+
import org.springframework.web.bind.annotation.ExceptionHandler;
9+
import org.springframework.web.bind.annotation.ResponseBody;
10+
11+
// We can add an exception handler to a single controller or do it using a @ControllerAdvice across many controllers.
12+
@ControllerAdvice
13+
public class ControllerExceptionHandler {
14+
15+
private static final Logger LOGGER = Logger.getLogger(ControllerExceptionHandler.class);
16+
17+
@Autowired
18+
private Bugsnag bugsnag;
19+
20+
@Autowired
21+
private String exampleWebsiteLinks;
22+
23+
@ExceptionHandler(Exception.class)
24+
@ResponseBody
25+
public String handleException(Exception e) {
26+
LOGGER.info("Handling exception with Spring ExceptionHandler");
27+
bugsnag.notify(e, (report) -> {
28+
report.setSeverity(Severity.ERROR);
29+
});
30+
return exampleWebsiteLinks + "<br/>Sent a Spring handled exception to Bugsnag";
31+
}
32+
}

0 commit comments

Comments
 (0)