|
| 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.boot.SpringApplication; |
| 8 | +import org.springframework.context.ApplicationContext; |
| 9 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 10 | +import org.springframework.web.bind.annotation.RestController; |
| 11 | + |
| 12 | +@RestController |
| 13 | +public class ApplicationRestController { |
| 14 | + |
| 15 | + private static final Logger LOGGER = Logger.getLogger(ApplicationRestController.class); |
| 16 | + |
| 17 | + // Inject the bugsnag notifier bean defined in Config.java |
| 18 | + @Autowired |
| 19 | + private Bugsnag bugsnag; |
| 20 | + |
| 21 | + @Autowired |
| 22 | + private ApplicationContext applicationContext; |
| 23 | + |
| 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/>"; |
| 30 | + |
| 31 | + @RequestMapping("/") |
| 32 | + public String index() { |
| 33 | + return links; |
| 34 | + } |
| 35 | + |
| 36 | + @RequestMapping("/send-handled-exception") |
| 37 | + public String sendHandledException() { |
| 38 | + LOGGER.info("Sending a handled exception to Bugsnag"); |
| 39 | + try { |
| 40 | + throw new RuntimeException("Handled exception - default severity"); |
| 41 | + } catch (RuntimeException e) { |
| 42 | + bugsnag.notify(e); |
| 43 | + } |
| 44 | + |
| 45 | + return links + "<br/>Sent a handled exception to Bugsnag"; |
| 46 | + } |
| 47 | + |
| 48 | + @RequestMapping("/send-handled-exception-info") |
| 49 | + public String sendHandledExceptionInfo() { |
| 50 | + LOGGER.info("Sending a handled exception to Bugsnag with INFO severity"); |
| 51 | + try { |
| 52 | + throw new RuntimeException("Handled exception - INFO severity"); |
| 53 | + } catch (RuntimeException e) { |
| 54 | + bugsnag.notify(e, Severity.INFO); |
| 55 | + } |
| 56 | + |
| 57 | + return links + "<br/>Sent a handled exception to Bugsnag with INFO severity"; |
| 58 | + } |
| 59 | + |
| 60 | + @RequestMapping("/send-handled-exception-with-metadata") |
| 61 | + public String sendHandledExceptionWithMetadata() { |
| 62 | + LOGGER.info("Sending a handled exception to Bugsnag with custom MetaData"); |
| 63 | + try { |
| 64 | + throw new RuntimeException("Handled exception - custom metadata"); |
| 65 | + } catch (RuntimeException e) { |
| 66 | + bugsnag.notify(e, (report) -> { |
| 67 | + report.setSeverity(Severity.WARNING); |
| 68 | + report.addToTab("report", "something", "that happened"); |
| 69 | + report.setContext("the context"); |
| 70 | + }); |
| 71 | + } |
| 72 | + |
| 73 | + return links + "<br/>Sent a handled exception to Bugsnag with custom MetaData"; |
| 74 | + } |
| 75 | + |
| 76 | + @RequestMapping("/send-unhandled-exception") |
| 77 | + public String sendUnhandledException() throws InterruptedException { |
| 78 | + // Test an unhanded exception from a different thread as shutdown hooks |
| 79 | + // won't be called if executed from this thread |
| 80 | + LOGGER.info("Sending an unhandled exception to Bugsnag"); |
| 81 | + Thread thread = new Thread() { |
| 82 | + @Override |
| 83 | + public void run() { |
| 84 | + throw new RuntimeException("Unhandled exception"); |
| 85 | + } |
| 86 | + }; |
| 87 | + |
| 88 | + thread.start(); |
| 89 | + |
| 90 | + // Wait for unhandled exception thread to finish |
| 91 | + thread.join(); |
| 92 | + |
| 93 | + return links + "<br/>Sent an unhandled exception to Bugsnag"; |
| 94 | + } |
| 95 | + |
| 96 | + @RequestMapping("/shutdown") |
| 97 | + public void shutdown() throws InterruptedException { |
| 98 | + LOGGER.info("Shutting down application"); |
| 99 | + |
| 100 | + SpringApplication.exit(applicationContext); |
| 101 | + } |
| 102 | +} |
0 commit comments