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
             final ErrorManager errorManager) {
69
             final ErrorManager errorManager) {
70
         final IdentityManager identityManager = new IdentityManager(baseDirectory,
70
         final IdentityManager identityManager = new IdentityManager(baseDirectory,
71
                 identitiesDirectory, eventBus, clientInfo);
71
                 identitiesDirectory, eventBus, clientInfo);
72
-        errorManager.initialise(identityManager.getGlobalConfiguration(), errorsDirectory,
73
-                eventBus, clientInfo);
72
+        errorManager.initialise(identityManager.getGlobalConfiguration(), errorsDirectory, eventBus);
74
         identityManager.loadVersionIdentity();
73
         identityManager.loadVersionIdentity();
75
 
74
 
76
         try {
75
         try {

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

31
 import com.dmdirc.events.UserErrorEvent;
31
 import com.dmdirc.events.UserErrorEvent;
32
 import com.dmdirc.interfaces.config.AggregateConfigProvider;
32
 import com.dmdirc.interfaces.config.AggregateConfigProvider;
33
 import com.dmdirc.ui.FatalErrorDialog;
33
 import com.dmdirc.ui.FatalErrorDialog;
34
-import com.dmdirc.util.ClientInfo;
35
 import com.dmdirc.util.EventUtils;
34
 import com.dmdirc.util.EventUtils;
36
 import com.dmdirc.util.collections.ListenerList;
35
 import com.dmdirc.util.collections.ListenerList;
37
 
36
 
76
     private final ListenerList errorListeners = new ListenerList();
75
     private final ListenerList errorListeners = new ListenerList();
77
     /** Countdown latch to wait for FED with. */
76
     /** Countdown latch to wait for FED with. */
78
     private final CountDownLatch countDownLatch;
77
     private final CountDownLatch countDownLatch;
78
+    /** Sentry error reporter factory. */
79
+    private final SentryErrorReporter sentryErrorReporter;
79
     /** Event bus to subscribe and publish errors on. */
80
     /** Event bus to subscribe and publish errors on. */
80
     private DMDircMBassador eventBus;
81
     private DMDircMBassador eventBus;
81
     /** Whether or not to send error reports. */
82
     /** Whether or not to send error reports. */
92
     private ExecutorService reportThread;
93
     private ExecutorService reportThread;
93
     /** Directory to store errors in. */
94
     /** Directory to store errors in. */
94
     private Path errorsDirectory;
95
     private Path errorsDirectory;
95
-    private ClientInfo clientInfo;
96
 
96
 
97
     /** Creates a new instance of ErrorListDialog. */
97
     /** Creates a new instance of ErrorListDialog. */
98
     @Inject
98
     @Inject
99
-    public ErrorManager() {
99
+    public ErrorManager(final SentryErrorReporter sentryErrorReporter) {
100
         errors = new LinkedList<>();
100
         errors = new LinkedList<>();
101
         countDownLatch = new CountDownLatch(2);
101
         countDownLatch = new CountDownLatch(2);
102
+        this.sentryErrorReporter = sentryErrorReporter;
102
     }
103
     }
103
 
104
 
104
     /**
105
     /**
109
      * @param eventBus     The event bus to listen for error events on.
110
      * @param eventBus     The event bus to listen for error events on.
110
      */
111
      */
111
     public void initialise(final AggregateConfigProvider globalConfig, final Path directory,
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
         this.eventBus = eventBus;
114
         this.eventBus = eventBus;
115
         eventBus.subscribe(this);
115
         eventBus.subscribe(this);
116
         RavenFactory.registerFactory(new DefaultRavenFactory());
116
         RavenFactory.registerFactory(new DefaultRavenFactory());
297
         }
297
         }
298
         error.setReportStatus(ErrorReportStatus.QUEUED);
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
 
22
 
23
 package com.dmdirc.logger;
23
 package com.dmdirc.logger;
24
 
24
 
25
-import com.dmdirc.util.ClientInfo;
26
-
27
 /**
25
 /**
28
  * Runnable that reports an error.
26
  * Runnable that reports an error.
29
  */
27
  */
30
 public class ErrorReportingRunnable implements Runnable {
28
 public class ErrorReportingRunnable implements Runnable {
31
 
29
 
32
     private final ProgramError error;
30
     private final ProgramError error;
33
-    private final ClientInfo clientInfo;
31
+    private final SentryErrorReporter sentryErrorReporter;
34
 
32
 
35
     /**
33
     /**
36
      * Runnable used to report errors.
34
      * Runnable used to report errors.
37
      *
35
      *
38
      * @param error Error to report
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
         this.error = error;
40
         this.error = error;
42
-        this.clientInfo = clientInfo;
41
+        this.sentryErrorReporter = sentryErrorReporter;
43
     }
42
     }
44
 
43
 
45
     @Override
44
     @Override
46
     public void run() {
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
 /**
42
 /**
43
  * Stores a program error.
43
  * Stores a program error.
44
  */
44
  */
45
-public final class ProgramError implements Serializable {
45
+public class ProgramError implements Serializable {
46
 
46
 
47
     /** A version number for this class. */
47
     /** A version number for this class. */
48
     private static final long serialVersionUID = 4;
48
     private static final long serialVersionUID = 4;

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

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

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

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

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

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

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

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

Loading…
Cancel
Save