Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

ErrorReporter.java 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * Copyright (c) 2006-2014 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.logger;
  23. import com.dmdirc.interfaces.Connection;
  24. import com.dmdirc.util.ClientInfo;
  25. import java.util.Date;
  26. import javax.annotation.Nullable;
  27. import net.kencochrane.raven.Raven;
  28. import net.kencochrane.raven.RavenFactory;
  29. import net.kencochrane.raven.dsn.Dsn;
  30. import net.kencochrane.raven.event.Event;
  31. import net.kencochrane.raven.event.EventBuilder;
  32. import net.kencochrane.raven.event.interfaces.ExceptionInterface;
  33. import net.kencochrane.raven.event.interfaces.MessageInterface;
  34. /**
  35. * Facilitates reporting errors to the DMDirc developers.
  36. */
  37. public class ErrorReporter {
  38. /** DSN used to connect to Sentry. */
  39. private static final String SENTRY_DSN = "https://d53a31a3c53c4a4f91c5ff503e612677:"
  40. + "e0a8aa1ecca14568a9f52d052ecf6a30@sentry.dmdirc.com/2?raven.async=false";
  41. /** Template to use when sending mode alias reports. */
  42. private static final String MODE_ALIAS_TEMPLATE = "%s\n\nConnection headers:\n%s";
  43. private final ClientInfo clientInfo;
  44. public ErrorReporter(final ClientInfo clientInfo) {
  45. this.clientInfo = clientInfo;
  46. }
  47. /**
  48. * Sends an error report caused by some kind of exception.
  49. *
  50. * @param message The message generated when the exception occurred.
  51. * @param level The severity level of the error.
  52. * @param timestamp The timestamp that the error first occurred at.
  53. * @param exception The actual exception, if available.
  54. * @param details The details of the exception, if any.
  55. */
  56. public void sendException(
  57. final String message,
  58. final ErrorLevel level,
  59. final Date timestamp,
  60. @Nullable final Throwable exception,
  61. @Nullable final String details) {
  62. final EventBuilder eventBuilder = newEventBuilder()
  63. .setMessage(message)
  64. .setLevel(getSentryLevel(level))
  65. .setTimestamp(timestamp);
  66. if (exception != null) {
  67. eventBuilder.addSentryInterface(new ExceptionInterface(exception));
  68. }
  69. if (details != null) {
  70. eventBuilder.addExtra("details", details);
  71. }
  72. send(eventBuilder.build());
  73. }
  74. /**
  75. * Sends a report of missing mode aliases.
  76. *
  77. * @param connection The connection that encountered the missing aliases.
  78. * @param channelModes The missing channel modes (may be empty).
  79. * @param userModes The missing user modes (may be empty).
  80. * @param modeAliasVersion The version of the mode aliases used.
  81. */
  82. public void sendModeAliasesReport(
  83. final Connection connection,
  84. final String channelModes,
  85. final String userModes,
  86. final String modeAliasVersion) {
  87. final String title = getModeAliasReportTitle(channelModes, userModes, connection.getIrcd());
  88. final EventBuilder eventBuilder = newEventBuilder()
  89. .setMessage(title)
  90. .setCulprit(title)
  91. .setLevel(Event.Level.INFO)
  92. .addTag("ircd.type", connection.getIrcd())
  93. .addTag("ircd.version", connection.getParser().getServerSoftware())
  94. .addTag("network", connection.getNetwork())
  95. .addTag("server", connection.getAddress())
  96. .addTag("modealiases", modeAliasVersion)
  97. .addSentryInterface(new MessageInterface(MODE_ALIAS_TEMPLATE, title,
  98. connection.getParser().getServerInformationLines().toString()));
  99. send(eventBuilder.build());
  100. }
  101. /**
  102. * Constructs a title for a mode alias report.
  103. *
  104. * @param channelModes The missing channel modes (may be empty).
  105. * @param userModes The missing user modes (may be empty).
  106. * @param ircd The name of the IRCd that has the modes.
  107. *
  108. * @return A title string to use for mode alias reports.
  109. */
  110. private String getModeAliasReportTitle(final String channelModes, final String userModes,
  111. final String ircd) {
  112. final StringBuilder title = new StringBuilder("Missing mode aliases:");
  113. if (!channelModes.isEmpty()) {
  114. title.append(" channel: +").append(channelModes);
  115. }
  116. if (!userModes.isEmpty()) {
  117. title.append(" user: ").append(userModes);
  118. }
  119. title.append(" [").append(ircd).append(']');
  120. return title.toString();
  121. }
  122. /**
  123. * Creates a new event builder with DMDirc and platform information pre-populated.
  124. *
  125. * @return A new event builder.
  126. */
  127. private EventBuilder newEventBuilder() {
  128. return new EventBuilder()
  129. .setServerName("")
  130. .addTag("version", clientInfo.getVersion())
  131. .addTag("version.major", clientInfo.getMajorVersion())
  132. .addTag("os.name", clientInfo.getOperatingSystemName())
  133. .addTag("os.version", clientInfo.getOperatingSystemVersion())
  134. .addTag("os.arch", clientInfo.getOperatingSystemArchitecture())
  135. .addTag("encoding", clientInfo.getSystemFileEncoding())
  136. .addTag("locale", clientInfo.getSystemDefaultLocale())
  137. .addTag("jvm.name", clientInfo.getJavaName())
  138. .addTag("jvm.vendor", clientInfo.getJavaVendor())
  139. .addTag("jvm.version", clientInfo.getJavaVersion())
  140. .addTag("jvm.version.major", clientInfo.getJavaMajorVersion());
  141. }
  142. /**
  143. * Connects to Sentry and sends the specified event.
  144. *
  145. * @param event The event to be sent.
  146. */
  147. private void send(final Event event) {
  148. final Raven raven = RavenFactory.ravenInstance(new Dsn(SENTRY_DSN));
  149. raven.sendEvent(event);
  150. }
  151. /**
  152. * Converts a DMDirc error level into a Sentry event level.
  153. *
  154. * @param level The DMDirc error level to convert.
  155. *
  156. * @return The corresponding sentry error level.
  157. */
  158. private static Event.Level getSentryLevel(final ErrorLevel level) {
  159. switch (level) {
  160. case FATAL:
  161. return Event.Level.FATAL;
  162. case HIGH:
  163. return Event.Level.ERROR;
  164. case MEDIUM:
  165. return Event.Level.WARNING;
  166. case LOW:
  167. return Event.Level.INFO;
  168. default:
  169. return Event.Level.INFO;
  170. }
  171. }
  172. }