Browse Source

Tidy up a little in logger.

pull/378/head
Greg Holmes 9 years ago
parent
commit
567b25dcb1

+ 1
- 2
src/com/dmdirc/config/ConfigModule.java View File

@@ -69,8 +69,7 @@ public class ConfigModule {
69 69
             final ErrorManager errorManager) {
70 70
         final IdentityManager identityManager = new IdentityManager(baseDirectory,
71 71
                 identitiesDirectory, eventBus, clientInfo);
72
-        errorManager.initialise(identityManager.getGlobalConfiguration(), errorsDirectory,
73
-                eventBus, clientInfo);
72
+        errorManager.initialise(identityManager.getGlobalConfiguration(), errorsDirectory, eventBus);
74 73
         identityManager.loadVersionIdentity();
75 74
 
76 75
         try {

+ 6
- 6
src/com/dmdirc/logger/ErrorManager.java View File

@@ -31,7 +31,6 @@ import com.dmdirc.events.ProgramErrorDeletedEvent;
31 31
 import com.dmdirc.events.UserErrorEvent;
32 32
 import com.dmdirc.interfaces.config.AggregateConfigProvider;
33 33
 import com.dmdirc.ui.FatalErrorDialog;
34
-import com.dmdirc.util.ClientInfo;
35 34
 import com.dmdirc.util.EventUtils;
36 35
 import com.dmdirc.util.collections.ListenerList;
37 36
 
@@ -76,6 +75,8 @@ public class ErrorManager {
76 75
     private final ListenerList errorListeners = new ListenerList();
77 76
     /** Countdown latch to wait for FED with. */
78 77
     private final CountDownLatch countDownLatch;
78
+    /** Sentry error reporter factory. */
79
+    private final SentryErrorReporter sentryErrorReporter;
79 80
     /** Event bus to subscribe and publish errors on. */
80 81
     private DMDircMBassador eventBus;
81 82
     /** Whether or not to send error reports. */
@@ -92,13 +93,13 @@ public class ErrorManager {
92 93
     private ExecutorService reportThread;
93 94
     /** Directory to store errors in. */
94 95
     private Path errorsDirectory;
95
-    private ClientInfo clientInfo;
96 96
 
97 97
     /** Creates a new instance of ErrorListDialog. */
98 98
     @Inject
99
-    public ErrorManager() {
99
+    public ErrorManager(final SentryErrorReporter sentryErrorReporter) {
100 100
         errors = new LinkedList<>();
101 101
         countDownLatch = new CountDownLatch(2);
102
+        this.sentryErrorReporter = sentryErrorReporter;
102 103
     }
103 104
 
104 105
     /**
@@ -109,8 +110,7 @@ public class ErrorManager {
109 110
      * @param eventBus     The event bus to listen for error events on.
110 111
      */
111 112
     public void initialise(final AggregateConfigProvider globalConfig, final Path directory,
112
-            final DMDircMBassador eventBus, final ClientInfo clientInfo) {
113
-        this.clientInfo = clientInfo;
113
+            final DMDircMBassador eventBus) {
114 114
         this.eventBus = eventBus;
115 115
         eventBus.subscribe(this);
116 116
         RavenFactory.registerFactory(new DefaultRavenFactory());
@@ -297,7 +297,7 @@ public class ErrorManager {
297 297
         }
298 298
         error.setReportStatus(ErrorReportStatus.QUEUED);
299 299
 
300
-        reportThread.submit(new ErrorReportingRunnable(error, clientInfo));
300
+        reportThread.submit(new ErrorReportingRunnable(sentryErrorReporter, error));
301 301
     }
302 302
 
303 303
     /**

+ 8
- 11
src/com/dmdirc/logger/ErrorReportingRunnable.java View File

@@ -22,34 +22,31 @@
22 22
 
23 23
 package com.dmdirc.logger;
24 24
 
25
-import com.dmdirc.util.ClientInfo;
26
-
27 25
 /**
28 26
  * Runnable that reports an error.
29 27
  */
30 28
 public class ErrorReportingRunnable implements Runnable {
31 29
 
32 30
     private final ProgramError error;
33
-    private final ClientInfo clientInfo;
31
+    private final SentryErrorReporter sentryErrorReporter;
34 32
 
35 33
     /**
36 34
      * Runnable used to report errors.
37 35
      *
38 36
      * @param error Error to report
39 37
      */
40
-    public ErrorReportingRunnable(final ProgramError error, final ClientInfo clientInfo) {
38
+    public ErrorReportingRunnable(final SentryErrorReporter sentryErrorReporter,
39
+            final ProgramError error) {
41 40
         this.error = error;
42
-        this.clientInfo = clientInfo;
41
+        this.sentryErrorReporter = sentryErrorReporter;
43 42
     }
44 43
 
45 44
     @Override
46 45
     public void run() {
47
-        do {
48
-            error.setReportStatus(ErrorReportStatus.SENDING);
49
-            new ErrorReporter(clientInfo).sendException(error.getMessage(), error.getLevel(), error
50
-                    .getDate(), error.getThrowable(), error.getDetails());
51
-            error.setReportStatus(ErrorReportStatus.FINISHED);
52
-        } while (error.getReportStatus() == ErrorReportStatus.QUEUED);
46
+        error.setReportStatus(ErrorReportStatus.SENDING);
47
+        sentryErrorReporter.sendException(error.getMessage(), error.getLevel(), error.getDate(),
48
+                error.getThrowable(), error.getDetails());
49
+        error.setReportStatus(ErrorReportStatus.FINISHED);
53 50
     }
54 51
 
55 52
 }

+ 1
- 1
src/com/dmdirc/logger/ProgramError.java View File

@@ -42,7 +42,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
42 42
 /**
43 43
  * Stores a program error.
44 44
  */
45
-public final class ProgramError implements Serializable {
45
+public class ProgramError implements Serializable {
46 46
 
47 47
     /** A version number for this class. */
48 48
     private static final long serialVersionUID = 4;

src/com/dmdirc/logger/ErrorReporter.java → src/com/dmdirc/logger/SentryErrorReporter.java View File

@@ -28,6 +28,8 @@ import com.dmdirc.util.ClientInfo;
28 28
 import java.util.Date;
29 29
 
30 30
 import javax.annotation.Nullable;
31
+import javax.inject.Inject;
32
+import javax.inject.Singleton;
31 33
 
32 34
 import net.kencochrane.raven.Raven;
33 35
 import net.kencochrane.raven.RavenFactory;
@@ -40,7 +42,8 @@ import net.kencochrane.raven.event.interfaces.MessageInterface;
40 42
 /**
41 43
  * Facilitates reporting errors to the DMDirc developers.
42 44
  */
43
-public class ErrorReporter {
45
+@Singleton
46
+public class SentryErrorReporter {
44 47
 
45 48
     /** DSN used to connect to Sentry. */
46 49
     private static final String SENTRY_DSN = "https://d53a31a3c53c4a4f91c5ff503e612677:"
@@ -49,7 +52,8 @@ public class ErrorReporter {
49 52
     private static final String MODE_ALIAS_TEMPLATE = "%s\n\nConnection headers:\n%s";
50 53
     private final ClientInfo clientInfo;
51 54
 
52
-    public ErrorReporter(final ClientInfo clientInfo) {
55
+    @Inject
56
+    public SentryErrorReporter(final ClientInfo clientInfo) {
53 57
         this.clientInfo = clientInfo;
54 58
     }
55 59
 

+ 2
- 1
test/com/dmdirc/logger/ErrorReportStatusTest.java View File

@@ -41,8 +41,9 @@ public class ErrorReportStatusTest {
41 41
 
42 42
     @Test
43 43
     public void testTerminal() {
44
-        assertTrue(ErrorReportStatus.ERROR.isTerminal());
45 44
         assertTrue(ErrorReportStatus.WAITING.isTerminal());
45
+        assertTrue(ErrorReportStatus.ERROR.isTerminal());
46
+        assertFalse(ErrorReportStatus.QUEUED.isTerminal());
46 47
         assertFalse(ErrorReportStatus.SENDING.isTerminal());
47 48
         assertTrue(ErrorReportStatus.FINISHED.isTerminal());
48 49
         assertTrue(ErrorReportStatus.NOT_APPLICABLE.isTerminal());

+ 20
- 0
test/com/dmdirc/logger/ProgramErrorTest.java View File

@@ -23,20 +23,26 @@
23 23
 package com.dmdirc.logger;
24 24
 
25 25
 import com.dmdirc.DMDircMBassador;
26
+import com.dmdirc.events.ProgramErrorStatusEvent;
26 27
 import com.dmdirc.util.ClientInfo;
27 28
 
28 29
 import com.google.common.collect.Lists;
29 30
 
30 31
 import java.util.Date;
32
+import java.util.List;
31 33
 
32 34
 import org.junit.Test;
33 35
 import org.junit.runner.RunWith;
36
+import org.mockito.ArgumentCaptor;
37
+import org.mockito.Captor;
34 38
 import org.mockito.Mock;
35 39
 import org.mockito.runners.MockitoJUnitRunner;
36 40
 
37 41
 import static org.junit.Assert.assertEquals;
38 42
 import static org.junit.Assert.assertFalse;
39 43
 import static org.junit.Assert.assertTrue;
44
+import static org.mockito.Mockito.never;
45
+import static org.mockito.Mockito.verify;
40 46
 
41 47
 @RunWith(MockitoJUnitRunner.class)
42 48
 public class ProgramErrorTest {
@@ -44,6 +50,7 @@ public class ProgramErrorTest {
44 50
     @Mock private ErrorManager errorManager;
45 51
     @Mock private DMDircMBassador eventBus;
46 52
     @Mock private ClientInfo clientInfo;
53
+    @Captor private ArgumentCaptor<ProgramErrorStatusEvent> event;
47 54
 
48 55
     @Test(expected = NullPointerException.class)
49 56
     public void testConstructorNullErrorLevel() {
@@ -108,10 +115,14 @@ public class ProgramErrorTest {
108 115
         assertEquals(ErrorReportStatus.WAITING, pe.getReportStatus());
109 116
         pe.setReportStatus(null);
110 117
         assertEquals(ErrorReportStatus.WAITING, pe.getReportStatus());
118
+        verify(eventBus, never()).publishAsync(event.capture());
111 119
         pe.setReportStatus(ErrorReportStatus.WAITING);
112 120
         assertEquals(ErrorReportStatus.WAITING, pe.getReportStatus());
121
+        verify(eventBus, never()).publishAsync(event.capture());
113 122
         pe.setReportStatus(ErrorReportStatus.ERROR);
114 123
         assertEquals(ErrorReportStatus.ERROR, pe.getReportStatus());
124
+        verify(eventBus).publishAsync(event.capture());
125
+        assertEquals(pe, event.getValue().getError());
115 126
     }
116 127
 
117 128
     @Test
@@ -122,6 +133,15 @@ public class ProgramErrorTest {
122 133
         assertTrue(pe.toString().contains("moo"));
123 134
     }
124 135
 
136
+    @Test
137
+    public void testGetTrace() {
138
+        final List<String> trace = Lists.newArrayList("test", "test1", "test2");
139
+        final ProgramError pe = new ProgramError(ErrorLevel.HIGH, "moo",
140
+                new UnsupportedOperationException(), trace, null, new Date(), errorManager,
141
+                eventBus);
142
+        assertEquals(trace, pe.getTrace());
143
+    }
144
+
125 145
     @Test
126 146
     public void testEquals() {
127 147
         final Exception ex = new UnsupportedOperationException();

test/com/dmdirc/logger/ErrorReporterTest.java → test/com/dmdirc/logger/SentryErrorReporterTest.java View File

@@ -51,13 +51,13 @@ import static org.mockito.Mockito.verify;
51 51
 import static org.mockito.Mockito.when;
52 52
 
53 53
 @RunWith(MockitoJUnitRunner.class)
54
-public class ErrorReporterTest {
54
+public class SentryErrorReporterTest {
55 55
 
56 56
     private static RavenFactory ravenFactory;
57 57
     @Mock private Raven raven;
58 58
     @Mock private ClientInfo clientInfo;
59 59
     @Captor private ArgumentCaptor<Event> eventCaptor;
60
-    private ErrorReporter errorReporter;
60
+    private SentryErrorReporter sentryErrorReporter;
61 61
 
62 62
     @BeforeClass
63 63
     public static void setUpClass() {
@@ -69,12 +69,12 @@ public class ErrorReporterTest {
69 69
     @Before
70 70
     public void setUp() {
71 71
         when(ravenFactory.createRavenInstance(Matchers.<Dsn>anyObject())).thenReturn(raven);
72
-        errorReporter = new ErrorReporter(clientInfo);
72
+        sentryErrorReporter = new SentryErrorReporter(clientInfo);
73 73
     }
74 74
 
75 75
     @Test
76 76
     public void testSendsMessage() {
77
-        errorReporter.sendException("message 123", ErrorLevel.MEDIUM, new Date(), null, null);
77
+        sentryErrorReporter.sendException("message 123", ErrorLevel.MEDIUM, new Date(), null, null);
78 78
         verify(raven).sendEvent(eventCaptor.capture());
79 79
         assertEquals("message 123", eventCaptor.getValue().getMessage());
80 80
     }
@@ -82,49 +82,49 @@ public class ErrorReporterTest {
82 82
     @Test
83 83
     public void testSendsTimestamp() {
84 84
         final Date date = new Date(561859200000l);
85
-        errorReporter.sendException("message 123", ErrorLevel.MEDIUM, date, null, null);
85
+        sentryErrorReporter.sendException("message 123", ErrorLevel.MEDIUM, date, null, null);
86 86
         verify(raven).sendEvent(eventCaptor.capture());
87 87
         assertEquals(date, eventCaptor.getValue().getTimestamp());
88 88
     }
89 89
 
90 90
     @Test
91 91
     public void testSendsLowLevelAsInfo() {
92
-        errorReporter.sendException("message 123", ErrorLevel.LOW, new Date(), null, null);
92
+        sentryErrorReporter.sendException("message 123", ErrorLevel.LOW, new Date(), null, null);
93 93
         verify(raven).sendEvent(eventCaptor.capture());
94 94
         assertEquals(Event.Level.INFO, eventCaptor.getValue().getLevel());
95 95
     }
96 96
 
97 97
     @Test
98 98
     public void testSendsMediumLevelAsWarning() {
99
-        errorReporter.sendException("message 123", ErrorLevel.MEDIUM, new Date(), null, null);
99
+        sentryErrorReporter.sendException("message 123", ErrorLevel.MEDIUM, new Date(), null, null);
100 100
         verify(raven).sendEvent(eventCaptor.capture());
101 101
         assertEquals(Event.Level.WARNING, eventCaptor.getValue().getLevel());
102 102
     }
103 103
 
104 104
     @Test
105 105
     public void testSendsHighLevelAsError() {
106
-        errorReporter.sendException("message 123", ErrorLevel.HIGH, new Date(), null, null);
106
+        sentryErrorReporter.sendException("message 123", ErrorLevel.HIGH, new Date(), null, null);
107 107
         verify(raven).sendEvent(eventCaptor.capture());
108 108
         assertEquals(Event.Level.ERROR, eventCaptor.getValue().getLevel());
109 109
     }
110 110
 
111 111
     @Test
112 112
     public void testSendsFatalLevelAsFatal() {
113
-        errorReporter.sendException("message 123", ErrorLevel.FATAL, new Date(), null, null);
113
+        sentryErrorReporter.sendException("message 123", ErrorLevel.FATAL, new Date(), null, null);
114 114
         verify(raven).sendEvent(eventCaptor.capture());
115 115
         assertEquals(Event.Level.FATAL, eventCaptor.getValue().getLevel());
116 116
     }
117 117
 
118 118
     @Test
119 119
     public void testSendsUnknownLevelAsInfo() {
120
-        errorReporter.sendException("message 123", ErrorLevel.UNKNOWN, new Date(), null, null);
120
+        sentryErrorReporter.sendException("message 123", ErrorLevel.UNKNOWN, new Date(), null, null);
121 121
         verify(raven).sendEvent(eventCaptor.capture());
122 122
         assertEquals(Event.Level.INFO, eventCaptor.getValue().getLevel());
123 123
     }
124 124
 
125 125
     @Test
126 126
     public void testAddsDetailsAsExtra() {
127
-        errorReporter.sendException("message 123", ErrorLevel.UNKNOWN, new Date(), null,
127
+        sentryErrorReporter.sendException("message 123", ErrorLevel.UNKNOWN, new Date(), null,
128 128
                 "details 456");
129 129
         verify(raven).sendEvent(eventCaptor.capture());
130 130
         assertEquals("details 456", eventCaptor.getValue().getExtra().get("details"));
@@ -133,7 +133,7 @@ public class ErrorReporterTest {
133 133
     @Test
134 134
     public void testAddsExceptionInterface() {
135 135
         final Exception exception = new IndexOutOfBoundsException("Message blah");
136
-        errorReporter.sendException("message 123", ErrorLevel.UNKNOWN, new Date(), exception, null);
136
+        sentryErrorReporter.sendException("message 123", ErrorLevel.UNKNOWN, new Date(), exception, null);
137 137
         verify(raven).sendEvent(eventCaptor.capture());
138 138
         final SentryInterface iface = eventCaptor.getValue().getSentryInterfaces()
139 139
                 .get(ExceptionInterface.EXCEPTION_INTERFACE);

Loading…
Cancel
Save