Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

SentryLoggingErrorManager.java 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.logger;
  18. import com.dmdirc.config.ConfigBinder;
  19. import com.dmdirc.config.ConfigBinding;
  20. import com.dmdirc.events.ProgramErrorAddedEvent;
  21. import com.dmdirc.events.ProgramErrorEvent;
  22. import com.dmdirc.events.ProgramErrorStatusEvent;
  23. import com.dmdirc.events.eventbus.EventBus;
  24. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  25. import com.google.common.base.Throwables;
  26. import java.util.Arrays;
  27. import java.util.Collection;
  28. import java.util.Optional;
  29. import java.util.concurrent.ExecutorService;
  30. import javax.inject.Inject;
  31. import javax.inject.Named;
  32. import javax.inject.Singleton;
  33. import net.engio.mbassy.listener.Handler;
  34. /**
  35. * Listens for {@link ProgramErrorEvent}s and reports these to Sentry.
  36. */
  37. @Singleton
  38. public class SentryLoggingErrorManager {
  39. /** A list of exceptions which we don't consider bugs and thus don't report. */
  40. private static final Class<?>[] BANNED_EXCEPTIONS = new Class<?>[]{
  41. NoSuchMethodError.class, NoClassDefFoundError.class,
  42. UnsatisfiedLinkError.class, AbstractMethodError.class,
  43. IllegalAccessError.class, OutOfMemoryError.class,
  44. NoSuchFieldError.class,};
  45. /** The event bus to listen for errors on. */
  46. private final EventBus eventBus;
  47. /** Sentry error reporter factory. */
  48. private final SentryErrorReporter sentryErrorReporter;
  49. /** Thread used for sending errors. */
  50. private final ExecutorService executorService;
  51. /** Whether to submit error reports. */
  52. private boolean submitReports;
  53. /** Temp no error reporting. */
  54. private boolean tempNoErrors;
  55. /** Whether or not to send error reports. */
  56. private boolean sendReports;
  57. @Inject
  58. public SentryLoggingErrorManager(final EventBus eventBus,
  59. final SentryErrorReporter sentryErrorReporter,
  60. @Named("errors") final ExecutorService executorService) {
  61. this.eventBus = eventBus;
  62. this.sentryErrorReporter = sentryErrorReporter;
  63. this.executorService = executorService;
  64. }
  65. /**
  66. * Initialises the error manager. Must be called before logging will start.
  67. */
  68. public void initialise(final AggregateConfigProvider config) {
  69. final ConfigBinder configBinder = config.getBinder();
  70. configBinder.bind(this, SentryLoggingErrorManager.class);
  71. eventBus.subscribe(this);
  72. }
  73. @Handler
  74. void handleErrorEvent(final ProgramErrorAddedEvent error) {
  75. final boolean appError = error.getError().isAppError();
  76. if (!isValidError(error.getError().getThrowable())
  77. || !isValidSource(error.getError().getThrowable())
  78. || !appError) {
  79. error.getError().setReportStatus(ErrorReportStatus.NOT_APPLICABLE);
  80. eventBus.publish(new ProgramErrorStatusEvent(error.getError()));
  81. } else if (sendReports) {
  82. sendError(error.getError());
  83. }
  84. }
  85. void sendError(final ProgramError error) {
  86. executorService.submit(new ErrorReportingRunnable(sentryErrorReporter, error));
  87. }
  88. @ConfigBinding(domain = "general", key = "submitErrors")
  89. void handleSubmitErrors(final boolean value) {
  90. submitReports = value;
  91. sendReports = submitReports && !tempNoErrors;
  92. }
  93. @ConfigBinding(domain = "temp", key="noerrorreporting")
  94. void handleNoErrorReporting(final boolean value) {
  95. tempNoErrors = value;
  96. sendReports = submitReports && !tempNoErrors;
  97. }
  98. /**
  99. * Determines whether or not the stack trace associated with this error is from a valid source.
  100. * A valid source is one that is within a DMDirc package (com.dmdirc), and is not the DMDirc
  101. * event queue.
  102. *
  103. * @return True if the source is valid, false otherwise
  104. */
  105. private boolean isValidSource(final Optional<Throwable> throwable) {
  106. if (throwable.isPresent()) {
  107. final String line = getSourceLine(Arrays.asList(
  108. Throwables.getStackTraceAsString(throwable.get()).split("\n")))
  109. .orElse("").trim();
  110. return line.startsWith("at com.dmdirc")
  111. && !line.startsWith("at com.dmdirc.addons.ui_swing.DMDircEventQueue");
  112. }
  113. return false;
  114. }
  115. /**
  116. * Returns the "source line" of this error, which is defined as the first line starting with a
  117. * DMDirc package name (com.dmdirc). If no such line is found, returns the first line of the
  118. * message.
  119. *
  120. * @return This error's source line
  121. */
  122. private Optional<String> getSourceLine(final Collection<String> trace) {
  123. for (String line : trace) {
  124. if (line.trim().startsWith("at com.dmdirc")) {
  125. return Optional.of(line);
  126. }
  127. }
  128. return trace.stream().findFirst();
  129. }
  130. /**
  131. * Determines whether or not the specified exception is one that we are willing to report.
  132. *
  133. * @param exception The exception to test
  134. *
  135. * @since 0.6.3m1
  136. * @return True if the exception may be reported, false otherwise
  137. */
  138. private boolean isValidError(final Optional<Throwable> exception) {
  139. if (exception.isPresent()) {
  140. @SuppressWarnings("ThrowableResultOfMethodCallIgnored")
  141. Throwable target = exception.get();
  142. while (target != null) {
  143. for (Class<?> bad : BANNED_EXCEPTIONS) {
  144. if (bad.equals(target.getClass())) {
  145. return false;
  146. }
  147. }
  148. target = target.getCause();
  149. }
  150. return true;
  151. }
  152. return false;
  153. }
  154. }