Bläddra i källkod

Remove {User,App}ErrorEvents.

pull/593/head
Greg Holmes 9 år sedan
förälder
incheckning
3e93816b34

+ 7
- 2
src/com/dmdirc/Main.java Visa fil

@@ -37,6 +37,7 @@ import com.dmdirc.interfaces.ui.UIController;
37 37
 import com.dmdirc.logger.DMDircExceptionHandler;
38 38
 import com.dmdirc.logger.ModeAliasReporter;
39 39
 import com.dmdirc.logger.ProgramErrorAppender;
40
+import com.dmdirc.logger.ProgramErrorManager;
40 41
 import com.dmdirc.plugins.CorePluginExtractor;
41 42
 import com.dmdirc.plugins.PluginManager;
42 43
 import com.dmdirc.plugins.Service;
@@ -98,6 +99,7 @@ public class Main {
98 99
     /** Mode alias reporter to use. */
99 100
     private final ModeAliasReporter reporter;
100 101
     private final ServiceManager serviceManager;
102
+    private final ProgramErrorManager errorManager;
101 103
 
102 104
     static {
103 105
         // TODO: Can this go in a Dagger module?
@@ -107,6 +109,7 @@ public class Main {
107 109
         context.reset();
108 110
     }
109 111
 
112
+
110 113
     /**
111 114
      * Creates a new instance of {@link Main}.
112 115
      */
@@ -124,7 +127,8 @@ public class Main {
124 127
             final DMDircMBassador eventBus,
125 128
             final Set<CommandDetails> commands,
126 129
             final ModeAliasReporter reporter,
127
-            final ServiceManager serviceManager) {
130
+            final ServiceManager serviceManager,
131
+            final ProgramErrorManager errorManager) {
128 132
         this.identityManager = identityManager;
129 133
         this.connectionManager = connectionManager;
130 134
         this.commandLineParser = commandLineParser;
@@ -138,6 +142,7 @@ public class Main {
138 142
         this.commands = commands;
139 143
         this.reporter = reporter;
140 144
         this.serviceManager = serviceManager;
145
+        this.errorManager = errorManager;
141 146
     }
142 147
 
143 148
     /**
@@ -278,7 +283,7 @@ public class Main {
278 283
         // TODO: Add a normal logging thing, with or without runtime switching.
279 284
         final LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
280 285
         final ProgramErrorAppender appender = new ProgramErrorAppender();
281
-        appender.setEventBus(eventBus);
286
+        appender.setProgramErrorManager(errorManager);
282 287
         appender.setContext(context);
283 288
         appender.setName("Error Logger");
284 289
         appender.start();

+ 0
- 37
src/com/dmdirc/events/AppErrorEvent.java Visa fil

@@ -1,37 +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
-
23
-package com.dmdirc.events;
24
-
25
-import com.dmdirc.logger.ErrorLevel;
26
-
27
-/**
28
- * Event class for app errors.
29
- */
30
-public class AppErrorEvent extends ErrorEvent {
31
-
32
-    public AppErrorEvent(final ErrorLevel level, final Throwable throwable, final String message,
33
-            final String details) {
34
-        super(level, throwable, message, details);
35
-    }
36
-
37
-}

+ 0
- 37
src/com/dmdirc/events/UserErrorEvent.java Visa fil

@@ -1,37 +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
-
23
-package com.dmdirc.events;
24
-
25
-import com.dmdirc.logger.ErrorLevel;
26
-
27
-/**
28
- * Event class for user errors.
29
- */
30
-public class UserErrorEvent extends ErrorEvent {
31
-
32
-    public UserErrorEvent(final ErrorLevel level, final Throwable throwable, final String message,
33
-            final String details) {
34
-        super(level, throwable, message, details);
35
-    }
36
-
37
-}

+ 10
- 6
src/com/dmdirc/logger/DiskLoggingErrorManager.java Visa fil

@@ -28,6 +28,7 @@ import com.dmdirc.commandline.CommandLineOptionsModule.DirectoryType;
28 28
 import com.dmdirc.config.ConfigBinder;
29 29
 import com.dmdirc.config.ConfigBinding;
30 30
 import com.dmdirc.events.ErrorEvent;
31
+import com.dmdirc.events.ProgramErrorEvent;
31 32
 import com.dmdirc.interfaces.config.AggregateConfigProvider;
32 33
 
33 34
 import com.google.common.base.Throwables;
@@ -96,19 +97,22 @@ public class DiskLoggingErrorManager {
96 97
     }
97 98
 
98 99
     @Handler
99
-    void handleErrorEvent(final ErrorEvent error) {
100
+    void handleErrorEvent(final ProgramErrorEvent error) {
100 101
         if (directoryError || !logging) {
101 102
             return;
102 103
         }
103
-        final String logName = error.getTimestamp() + "-" + error.getLevel();
104
+        final String logName = error.getTimestamp() + "-" + error.getError().getLevel();
104 105
         final Path errorFile = errorsDirectory.resolve(logName + ".log");
105 106
         final List<String> data = Lists
106 107
                 .newArrayList("Date: " + new Date(error.getTimestamp()),
107
-                        "Level: " + error.getLevel(),
108
-                        "Description: " + error.getMessage(),
108
+                        "Level: " + error.getError().getLevel(),
109
+                        "Description: " + error.getError().getMessage(),
109 110
                         "Details: ");
110
-        Arrays.stream(Throwables.getStackTraceAsString(error.getThrowable()).split("\n"))
111
-                .forEach(data::add);
111
+        //noinspection ThrowableResultOfMethodCallIgnored
112
+        if (error.getError().getThrowable() != null) {
113
+            Arrays.stream(Throwables.getStackTraceAsString(error.getError().getThrowable())
114
+                    .split("\n")).forEach(data::add);
115
+        }
112 116
         try {
113 117
             Files.write(errorFile, data, Charset.forName("UTF-8"));
114 118
         } catch (IOException ex) {

+ 5
- 31
src/com/dmdirc/logger/ProgramErrorAppender.java Visa fil

@@ -22,32 +22,19 @@
22 22
 
23 23
 package com.dmdirc.logger;
24 24
 
25
-import com.dmdirc.DMDircMBassador;
26
-import com.dmdirc.events.AppErrorEvent;
27
-import com.dmdirc.events.UserErrorEvent;
28
-import com.dmdirc.logger.ErrorLevel;
29
-import com.dmdirc.logger.ProgramError;
30
-
31 25
 import ch.qos.logback.classic.spi.ILoggingEvent;
32 26
 import ch.qos.logback.core.AppenderBase;
33 27
 
34
-import static com.dmdirc.util.LogUtils.APP_ERROR;
35
-import static com.dmdirc.util.LogUtils.FATAL_APP_ERROR;
36
-import static com.dmdirc.util.LogUtils.FATAL_USER_ERROR;
37
-import static com.dmdirc.util.LogUtils.USER_ERROR;
38
-import static com.dmdirc.util.LogUtils.getErrorLevel;
39
-import static com.dmdirc.util.LogUtils.getThrowable;
40
-
41 28
 /**
42 29
  * Converts log states into {@link ProgramError}s so they can be displayed to the user.
43 30
  */
44 31
 public class ProgramErrorAppender extends AppenderBase<ILoggingEvent> {
45 32
 
46
-    private DMDircMBassador eventBus;
33
+    private ProgramErrorManager errorManager;
47 34
 
48 35
     @Override
49 36
     public void start() {
50
-        if (eventBus == null) {
37
+        if (errorManager == null) {
51 38
             addError("No eventBus set for the appender named ["+ name +"].");
52 39
             return;
53 40
         }
@@ -59,23 +46,10 @@ public class ProgramErrorAppender extends AppenderBase<ILoggingEvent> {
59 46
         if (eventObject.getMarker() == null) {
60 47
             return;
61 48
         }
62
-        if (eventObject.getMarker() == APP_ERROR) {
63
-            eventBus.publish(new AppErrorEvent(
64
-                    getErrorLevel(eventObject.getLevel()),
65
-                    getThrowable(eventObject), eventObject.getFormattedMessage(), ""));
66
-        } else if (eventObject.getMarker() == USER_ERROR) {
67
-            eventBus.publish(new UserErrorEvent(getErrorLevel(eventObject.getLevel()),
68
-                    getThrowable(eventObject), eventObject.getFormattedMessage(), ""));
69
-        } else if (eventObject.getMarker() == FATAL_APP_ERROR) {
70
-            eventBus.publish(new AppErrorEvent(ErrorLevel.FATAL, getThrowable(eventObject),
71
-                    eventObject.getFormattedMessage(), ""));
72
-        } else if (eventObject.getMarker() == FATAL_USER_ERROR) {
73
-            eventBus.publish(new UserErrorEvent(ErrorLevel.FATAL, getThrowable(eventObject),
74
-                    eventObject.getFormattedMessage(), ""));
75
-        }
49
+        errorManager.handle(eventObject);
76 50
     }
77 51
 
78
-    public void setEventBus(final DMDircMBassador eventBus) {
79
-        this.eventBus = eventBus;
52
+    public void setProgramErrorManager(final ProgramErrorManager errorManager) {
53
+        this.errorManager = errorManager;
80 54
     }
81 55
 }

+ 15
- 11
src/com/dmdirc/logger/ProgramErrorManager.java Visa fil

@@ -23,14 +23,15 @@
23 23
 package com.dmdirc.logger;
24 24
 
25 25
 import com.dmdirc.DMDircMBassador;
26
-import com.dmdirc.events.AppErrorEvent;
27 26
 import com.dmdirc.events.ErrorEvent;
28 27
 import com.dmdirc.events.FatalProgramErrorEvent;
29 28
 import com.dmdirc.events.NonFatalProgramErrorEvent;
30 29
 import com.dmdirc.events.ProgramErrorDeletedEvent;
31 30
 import com.dmdirc.events.ProgramErrorEvent;
32
-import com.dmdirc.events.UserErrorEvent;
33 31
 import com.dmdirc.util.EventUtils;
32
+import com.dmdirc.util.LogUtils;
33
+
34
+import ch.qos.logback.classic.spi.ILoggingEvent;
34 35
 
35 36
 import com.google.common.base.Throwables;
36 37
 
@@ -43,9 +44,12 @@ import java.util.HashSet;
43 44
 import java.util.Set;
44 45
 import java.util.concurrent.CopyOnWriteArraySet;
45 46
 
47
+import javax.annotation.Nullable;
46 48
 import javax.inject.Inject;
47 49
 import javax.inject.Singleton;
48 50
 
51
+import org.slf4j.Marker;
52
+
49 53
 import net.engio.mbassy.listener.Handler;
50 54
 
51 55
 /**
@@ -77,19 +81,19 @@ public class ProgramErrorManager {
77 81
         eventBus.subscribe(this);
78 82
     }
79 83
 
80
-    @Handler
81
-    void handleAppError(final AppErrorEvent event) {
82
-        handleErrorEvent(event, true);
84
+    void handle(final ILoggingEvent event) {
85
+        final ProgramError error = addError(LogUtils.getErrorLevel(event.getLevel()),
86
+                event.getFormattedMessage(), LogUtils.getThrowable(event), "",
87
+                isAppError(event.getMarker()));
88
+        handleErrorEvent(error);
83 89
     }
84 90
 
85
-    @Handler
86
-    void handleUserError(final UserErrorEvent event) {
87
-        handleErrorEvent(event, false);
91
+    private boolean isAppError(@Nullable final Marker marker) {
92
+        return marker != null
93
+                && (marker == LogUtils.APP_ERROR || marker == LogUtils.FATAL_APP_ERROR);
88 94
     }
89 95
 
90
-    private void handleErrorEvent(final ErrorEvent event, final boolean appError) {
91
-        final ProgramError error = addError(event.getLevel(), event.getMessage(),
92
-                event.getThrowable(), event.getDetails(), appError);
96
+    void handleErrorEvent(final ProgramError error) {
93 97
         if (error.getLevel() == ErrorLevel.FATAL) {
94 98
             eventBus.publish(new FatalProgramErrorEvent(error));
95 99
         } else {

+ 9
- 8
test/com/dmdirc/logger/DiskLoggingErrorManagerTest.java Visa fil

@@ -24,7 +24,7 @@ package com.dmdirc.logger;
24 24
 
25 25
 import com.dmdirc.DMDircMBassador;
26 26
 import com.dmdirc.config.ConfigBinder;
27
-import com.dmdirc.events.UserErrorEvent;
27
+import com.dmdirc.events.ProgramErrorEvent;
28 28
 import com.dmdirc.interfaces.config.AggregateConfigProvider;
29 29
 
30 30
 import com.google.common.jimfs.Configuration;
@@ -33,6 +33,7 @@ import com.google.common.jimfs.Jimfs;
33 33
 import java.nio.file.FileSystem;
34 34
 import java.nio.file.Files;
35 35
 import java.nio.file.Path;
36
+import java.util.Date;
36 37
 
37 38
 import org.junit.Before;
38 39
 import org.junit.Test;
@@ -51,12 +52,17 @@ public class DiskLoggingErrorManagerTest {
51 52
     @Mock private DMDircMBassador eventBus;
52 53
     @Mock private AggregateConfigProvider config;
53 54
     @Mock private ConfigBinder configBinder;
55
+    @Mock private ProgramErrorEvent error;
56
+    @Mock private ProgramError programError;
54 57
 
55 58
     private FileSystem fileSystem;
56 59
     private DiskLoggingErrorManager instance;
57 60
 
58 61
     @Before
59 62
     public void setUp() throws Exception {
63
+        when(error.getTimestamp()).thenReturn(new Date().getTime());
64
+        when(error.getError()).thenReturn(programError);
65
+        when(programError.getLevel()).thenReturn(ErrorLevel.MEDIUM);
60 66
         when(config.getBinder()).thenReturn(configBinder);
61 67
         fileSystem = Jimfs.newFileSystem(Configuration.unix());
62 68
         instance = new DiskLoggingErrorManager(fileSystem.getPath("/errors"), eventBus);
@@ -78,11 +84,9 @@ public class DiskLoggingErrorManagerTest {
78 84
 
79 85
     @Test
80 86
     public void testHandleErrorEvent() throws Exception {
81
-        final UserErrorEvent error = new UserErrorEvent(ErrorLevel.MEDIUM,
82
-                new IllegalStateException(""), "", "");
83 87
         instance.initialise(config);
84 88
         instance.handleLoggingSetting(true);
85
-        final String logName = error.getTimestamp() + "-" + error.getLevel() + ".log";;
89
+        final String logName = error.getTimestamp() + "-" + error.getError().getLevel() + ".log";;
86 90
         assertFalse(Files.exists(fileSystem.getPath("/errors", logName)));
87 91
         instance.handleErrorEvent(error);
88 92
         final Path errorPath = fileSystem.getPath("/errors", logName);
@@ -92,12 +96,9 @@ public class DiskLoggingErrorManagerTest {
92 96
 
93 97
     @Test
94 98
     public void testHandleErrorEventNotLogging() throws Exception {
95
-        final UserErrorEvent error = new UserErrorEvent(ErrorLevel.MEDIUM,
96
-                new IllegalStateException(""),
97
-                "", "");
98 99
         instance.initialise(config);
99 100
         instance.handleLoggingSetting(false);
100
-        final String logName = error.getTimestamp() + "-" + error.getLevel() + ".log";;
101
+        final String logName = error.getTimestamp() + "-" + error.getError().getLevel() + ".log";;
101 102
         assertFalse(Files.exists(fileSystem.getPath("/errors", logName)));
102 103
         instance.handleErrorEvent(error);
103 104
         assertFalse(Files.exists(fileSystem.getPath("/errors", logName)));

Laddar…
Avbryt
Spara