Browse Source

Change a couple of event bus error reportings.

pull/586/head
Chris Smith 9 years ago
parent
commit
1e3b0b5e28

+ 1
- 1
src/com/dmdirc/Main.java View File

@@ -175,7 +175,7 @@ public class Main {
175 175
      * Initialises the client.
176 176
      */
177 177
     public void init() {
178
-        Thread.setDefaultUncaughtExceptionHandler(new DMDircExceptionHandler(eventBus));
178
+        Thread.setDefaultUncaughtExceptionHandler(new DMDircExceptionHandler());
179 179
         setupLogback();
180 180
         migrators.stream().filter(Migrator::needsMigration).forEach(Migrator::migrate);
181 181
         commands.forEach(c -> commandManager.registerCommand(c.getCommand(), c.getInfo()));

+ 8
- 14
src/com/dmdirc/logger/DMDircExceptionHandler.java View File

@@ -22,28 +22,22 @@
22 22
 
23 23
 package com.dmdirc.logger;
24 24
 
25
-import com.dmdirc.DMDircMBassador;
26
-import com.dmdirc.events.AppErrorEvent;
25
+import org.slf4j.Logger;
26
+import org.slf4j.LoggerFactory;
27
+
28
+import static com.dmdirc.util.LogUtils.APP_ERROR;
29
+import static com.dmdirc.util.LogUtils.FATAL_APP_ERROR;
27 30
 
28 31
 /**
29 32
  * Passes uncaught exceptions to the logger.
30 33
  */
31
-public final class DMDircExceptionHandler implements
32
-        Thread.UncaughtExceptionHandler {
33
-
34
-    private final DMDircMBassador eventBus;
34
+public final class DMDircExceptionHandler implements Thread.UncaughtExceptionHandler {
35 35
 
36
-    public DMDircExceptionHandler(final DMDircMBassador eventBus) {
37
-        this.eventBus = eventBus;
38
-    }
36
+    private static final Logger LOG = LoggerFactory.getLogger(DMDircExceptionHandler.class);
39 37
 
40 38
     @Override
41 39
     public void uncaughtException(final Thread t, final Throwable e) {
42
-        if (e instanceof Error) {
43
-            eventBus.publish(new AppErrorEvent(ErrorLevel.FATAL, e, e.toString(), ""));
44
-        } else {
45
-            eventBus.publish(new AppErrorEvent(ErrorLevel.HIGH, e, e.toString(), ""));
46
-        }
40
+        LOG.error(e instanceof Error ? FATAL_APP_ERROR : APP_ERROR, "Uncaught exception", e);
47 41
     }
48 42
 
49 43
 }

+ 10
- 9
src/com/dmdirc/logger/ModeAliasReporter.java View File

@@ -22,9 +22,7 @@
22 22
 
23 23
 package com.dmdirc.logger;
24 24
 
25
-import com.dmdirc.DMDircMBassador;
26 25
 import com.dmdirc.MissingModeAliasException;
27
-import com.dmdirc.events.AppErrorEvent;
28 26
 import com.dmdirc.events.ServerConnectedEvent;
29 27
 import com.dmdirc.interfaces.config.AggregateConfigProvider;
30 28
 import com.dmdirc.parser.interfaces.Parser;
@@ -32,10 +30,15 @@ import com.dmdirc.parser.interfaces.Parser;
32 30
 import javax.inject.Inject;
33 31
 import javax.inject.Singleton;
34 32
 
33
+import org.slf4j.Logger;
34
+import org.slf4j.LoggerFactory;
35
+
35 36
 import net.engio.mbassy.listener.Handler;
36 37
 import net.engio.mbassy.listener.Listener;
37 38
 import net.engio.mbassy.listener.References;
38 39
 
40
+import static com.dmdirc.util.LogUtils.APP_ERROR;
41
+
39 42
 /**
40 43
  * Watches for newly connected servers and raises errors about their mode aliases.
41 44
  */
@@ -43,14 +46,13 @@ import net.engio.mbassy.listener.References;
43 46
 @Listener(references = References.Strong)
44 47
 public class ModeAliasReporter {
45 48
 
49
+    private static final Logger LOG = LoggerFactory.getLogger(ModeAliasReporter.class);
50
+
46 51
     /** The name of the server domain. */
47 52
     private static final String DOMAIN_SERVER = "server";
48 53
 
49
-    private final DMDircMBassador eventBus;
50
-
51 54
     @Inject
52
-    public ModeAliasReporter(final DMDircMBassador eventBus) {
53
-        this.eventBus = eventBus;
55
+    public ModeAliasReporter() {
54 56
     }
55 57
 
56 58
     @Handler
@@ -99,13 +101,12 @@ public class ModeAliasReporter {
99 101
             }
100 102
 
101 103
 
102
-            eventBus.publish(new AppErrorEvent(ErrorLevel.LOW,
103
-                    new MissingModeAliasException(
104
+            LOG.info(APP_ERROR, "Missing mode aliases", new MissingModeAliasException(
104 105
                             event.getConnection().getNetwork(),
105 106
                             parser,
106 107
                             configManager.getOption("identity", "modealiasversion"),
107 108
                             missing.toString()),
108
-                    missing + " [" + parser.getServerSoftwareType() + ']', ""));
109
+                    missing + " [" + parser.getServerSoftwareType() + ']', "");
109 110
         }
110 111
     }
111 112
 }

+ 21
- 23
src/com/dmdirc/plugins/PluginInfo.java View File

@@ -25,13 +25,10 @@ package com.dmdirc.plugins;
25 25
 import com.dmdirc.DMDircMBassador;
26 26
 import com.dmdirc.config.ConfigFileBackedConfigProvider;
27 27
 import com.dmdirc.config.InvalidIdentityFileException;
28
-import com.dmdirc.events.AppErrorEvent;
29 28
 import com.dmdirc.events.PluginLoadedEvent;
30 29
 import com.dmdirc.events.PluginUnloadedEvent;
31
-import com.dmdirc.events.UserErrorEvent;
32 30
 import com.dmdirc.interfaces.config.ConfigProvider;
33 31
 import com.dmdirc.interfaces.config.IdentityController;
34
-import com.dmdirc.logger.ErrorLevel;
35 32
 import com.dmdirc.util.validators.ValidationResponse;
36 33
 
37 34
 import java.io.IOException;
@@ -57,6 +54,9 @@ import org.slf4j.LoggerFactory;
57 54
 
58 55
 import dagger.ObjectGraph;
59 56
 
57
+import static com.dmdirc.util.LogUtils.APP_ERROR;
58
+import static com.dmdirc.util.LogUtils.USER_ERROR;
59
+
60 60
 /**
61 61
  * Stores plugin metadata and handles loading of plugin resources.
62 62
  */
@@ -253,8 +253,8 @@ public class PluginInfo implements ServiceProvider {
253 253
                 directoryStream.forEach(this::loadIdentity);
254 254
             }
255 255
         } catch (final IOException ioe) {
256
-            eventBus.publish(new UserErrorEvent(ErrorLevel.MEDIUM, ioe,
257
-                    "Error finding identities in plugin '" + metaData.getName() + '\'', ""));
256
+            LOG.warn(USER_ERROR, "Error finding identities in plugin '{}'", metaData.getName(),
257
+                    ioe);
258 258
         }
259 259
     }
260 260
 
@@ -271,14 +271,12 @@ public class PluginInfo implements ServiceProvider {
271 271
                 identityController.addConfigProvider(configProvider);
272 272
                 configProviders.add(configProvider);
273 273
             }
274
-        } catch (final InvalidIdentityFileException ex) {
275
-            eventBus.publish(new UserErrorEvent(ErrorLevel.MEDIUM, ex,
276
-                    "Error with identity file '" + path.getFileName() + "' in plugin '" +
277
-                            metaData.getName() + '\'', ""));
274
+        } catch (InvalidIdentityFileException ex) {
275
+            LOG.warn(USER_ERROR, "Error with identity file '{}' in plugin '{}'",
276
+                    path.getFileName(), metaData.getName(), ex);
278 277
         } catch (IOException ex) {
279
-            eventBus.publish(new UserErrorEvent(ErrorLevel.MEDIUM, ex,
280
-                    "Unable to load identity file '" + path.getFileName() + "' in plugin '" +
281
-                            metaData.getName() + '\'', ""));
278
+            LOG.warn(USER_ERROR, "Unable to load identity file '{}' in plugin '{}'",
279
+                    path.getFileName(), metaData.getName(), ex);
282 280
         }
283 281
     }
284 282
 
@@ -330,10 +328,10 @@ public class PluginInfo implements ServiceProvider {
330 328
      * files.
331 329
      */
332 330
     public void pluginUpdated() throws PluginException {
333
-            updateClassList();
334
-            updateMetaData();
335
-            updateProvides();
336
-            getDefaults();
331
+        updateClassList();
332
+        updateMetaData();
333
+        updateProvides();
334
+        getDefaults();
337 335
     }
338 336
 
339 337
     /**
@@ -597,23 +595,23 @@ public class PluginInfo implements ServiceProvider {
597 595
             lastError = "Class not found ('" + filename + ':' + classname + ':'
598 596
                     + classname.equals(metaData.getMainClass()) + "') - "
599 597
                     + cnfe.getMessage();
600
-            eventBus.publishAsync(new UserErrorEvent(ErrorLevel.LOW, cnfe, lastError, ""));
598
+            LOG.info(USER_ERROR, lastError, cnfe);
601 599
         } catch (NoClassDefFoundError ncdf) {
602 600
             lastError = "Unable to instantiate plugin ('" + filename + ':'
603 601
                     + classname + ':'
604 602
                     + classname.equals(metaData.getMainClass())
605 603
                     + "') - Unable to find class: " + ncdf.getMessage();
606
-            eventBus.publishAsync(new UserErrorEvent(ErrorLevel.LOW, ncdf, lastError, ""));
604
+            LOG.info(USER_ERROR, lastError, ncdf);
607 605
         } catch (VerifyError ve) {
608 606
             lastError = "Unable to instantiate plugin ('" + filename + ':'
609 607
                     + classname + ':'
610 608
                     + classname.equals(metaData.getMainClass())
611 609
                     + "') - Incompatible: " + ve.getMessage();
612
-            eventBus.publishAsync(new UserErrorEvent(ErrorLevel.LOW, ve, lastError, ""));
610
+            LOG.info(USER_ERROR, lastError, ve);
613 611
         } catch (IllegalArgumentException | ReflectiveOperationException ex) {
614 612
             lastError = "Unable to instantiate class for plugin " + metaData.getName()
615 613
                     + ": " + classname;
616
-            eventBus.publishAsync(new AppErrorEvent(ErrorLevel.LOW, ex, lastError, ""));
614
+            LOG.info(APP_ERROR, lastError, ex);
617 615
         }
618 616
     }
619 617
 
@@ -626,7 +624,7 @@ public class PluginInfo implements ServiceProvider {
626 624
                 lastError = "Prerequisites for plugin not met. ('"
627 625
                         + filename + ':' + metaData.getMainClass()
628 626
                         + "' -> '" + prerequisites.getFailureReason() + "') ";
629
-                eventBus.publish(new UserErrorEvent(ErrorLevel.LOW, null, lastError, ""));
627
+                LOG.info(USER_ERROR, lastError);
630 628
             } else {
631 629
                 initialisePlugin((Plugin) temp);
632 630
             }
@@ -640,7 +638,7 @@ public class PluginInfo implements ServiceProvider {
640 638
             plugin.onLoad();
641 639
         } catch (LinkageError | Exception e) {
642 640
             lastError = "Error in onLoad for " + metaData.getName() + ':' + e.getMessage();
643
-            eventBus.publishAsync(new AppErrorEvent(ErrorLevel.MEDIUM, e, lastError, ""));
641
+            LOG.warn(APP_ERROR, lastError, e);
644 642
             unloadPlugin();
645 643
         }
646 644
     }
@@ -698,7 +696,7 @@ public class PluginInfo implements ServiceProvider {
698 696
             } catch (Exception | LinkageError e) {
699 697
                 lastError = "Error in onUnload for " + metaData.getName()
700 698
                         + ':' + e + " - " + e.getMessage();
701
-                eventBus.publishAsync(new AppErrorEvent(ErrorLevel.MEDIUM, e, lastError, ""));
699
+                LOG.warn(APP_ERROR, lastError, e);
702 700
             }
703 701
 
704 702
             //TODO plugin unloading shouldn't be done from here, event bus shouldn't be here.

+ 0
- 73
test/com/dmdirc/logger/DMDircExceptionHandlerTest.java View File

@@ -1,73 +0,0 @@
1
-/*
2
- * Copyright (c) 2006-2015 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
-
24
-
25
-import com.dmdirc.DMDircMBassador;
26
-import com.dmdirc.events.AppErrorEvent;
27
-
28
-import org.junit.Before;
29
-import org.junit.Test;
30
-import org.junit.runner.RunWith;
31
-import org.mockito.ArgumentCaptor;
32
-import org.mockito.Captor;
33
-import org.mockito.Mock;
34
-import org.mockito.runners.MockitoJUnitRunner;
35
-
36
-import static junit.framework.TestCase.assertEquals;
37
-import static org.mockito.Mockito.verify;
38
-
39
-@RunWith(MockitoJUnitRunner.class)
40
-public class DMDircExceptionHandlerTest {
41
-
42
-    @Mock private DMDircMBassador eventBus;
43
-    @Captor private ArgumentCaptor<AppErrorEvent> event;
44
-    private DMDircExceptionHandler eh;
45
-
46
-    @Before
47
-    public void setup() {
48
-        eh = new DMDircExceptionHandler(eventBus);
49
-    }
50
-
51
-    @Test
52
-    public void testUncaughtException() {
53
-        final Exception exception = new UnsupportedOperationException();
54
-        eh.uncaughtException(null, exception);
55
-        verify(eventBus).publish(event.capture());
56
-        assertEquals(ErrorLevel.HIGH, event.getValue().getLevel());
57
-        assertEquals(exception, event.getValue().getThrowable());
58
-        assertEquals(exception.toString(), event.getValue().getMessage());
59
-        assertEquals("", event.getValue().getDetails());
60
-    }
61
-
62
-    @Test
63
-    public void testUncaughtError() {
64
-        final Error error = new OutOfMemoryError();
65
-        eh.uncaughtException(null, error);
66
-        verify(eventBus).publish(event.capture());
67
-        assertEquals(ErrorLevel.FATAL, event.getValue().getLevel());
68
-        assertEquals(error, event.getValue().getThrowable());
69
-        assertEquals(error.toString(), event.getValue().getMessage());
70
-        assertEquals("", event.getValue().getDetails());
71
-    }
72
-
73
-}

Loading…
Cancel
Save