Browse Source

Raise events for error status changes.

pull/372/head
Greg Holmes 9 years ago
parent
commit
5fd1b9c24a

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

@@ -254,7 +254,7 @@ public class ErrorManager {
254 254
     protected ProgramError getError(final ErrorLevel level, final String message,
255 255
             final Throwable exception, final String details) {
256 256
         return new ProgramError(level, message, exception, getTrace(message, exception), details,
257
-                new Date(), this);
257
+                new Date(), this, eventBus);
258 258
     }
259 259
 
260 260
     /**
@@ -381,8 +381,8 @@ public class ErrorManager {
381 381
                     listener.errorAdded(event.getError());
382 382
                 });
383 383
         if (!event.isHandled()) {
384
-            System.err.println(
385
-                    "An error has occurred: " + event.getError().getLevel() + ": " + event.getError().getMessage());
384
+            System.err.println("An error has occurred: " + event.getError().getLevel() + ": "
385
+                            + event.getError().getMessage());
386 386
 
387 387
             for (String line : event.getError().getTrace()) {
388 388
                 System.err.println("\t" + line);

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

@@ -22,6 +22,9 @@
22 22
 
23 23
 package com.dmdirc.logger;
24 24
 
25
+import com.dmdirc.DMDircMBassador;
26
+import com.dmdirc.events.ProgramErrorStatusEvent;
27
+
25 28
 import com.google.common.base.MoreObjects;
26 29
 import com.google.common.collect.Lists;
27 30
 
@@ -57,6 +60,8 @@ public final class ProgramError implements Serializable {
57 60
     private final Date firstDate;
58 61
     /** The error manager to register with. */
59 62
     private final ErrorManager errorManager;
63
+    /** The eventbus to post status changes to. */
64
+    private final DMDircMBassador eventBus;
60 65
     /** Error report Status. */
61 66
     private ErrorReportStatus reportStatus;
62 67
     /** Has the error been output. */
@@ -71,13 +76,15 @@ public final class ProgramError implements Serializable {
71 76
      * @param trace     The textual trace for this error
72 77
      * @param details   The detailed cause of the error, if any.
73 78
      * @param date      Error time and date
79
+     * @param eventBus  The event bus to post status changes to
74 80
      */
75 81
     public ProgramError(final ErrorLevel level, final String message,
76 82
             @Nullable final Throwable exception,
77 83
             final Iterable<String> trace,
78 84
             @Nullable final String details,
79 85
             final Date date,
80
-            final ErrorManager errorManager) {
86
+            final ErrorManager errorManager,
87
+            final DMDircMBassador eventBus) {
81 88
         checkNotNull(level);
82 89
         checkNotNull(message);
83 90
         checkNotNull(date);
@@ -91,6 +98,7 @@ public final class ProgramError implements Serializable {
91 98
         this.firstDate = (Date) date.clone();
92 99
         this.reportStatus = ErrorReportStatus.WAITING;
93 100
         this.errorManager = errorManager;
101
+        this.eventBus = eventBus;
94 102
     }
95 103
 
96 104
     /**
@@ -152,14 +160,10 @@ public final class ProgramError implements Serializable {
152 160
      * @param newStatus new ErrorReportStatus for the error
153 161
      */
154 162
     public void setReportStatus(final ErrorReportStatus newStatus) {
155
-        // TODO: Shift this into ErrorManager and use the event not the listeners
156 163
         if (newStatus != null && reportStatus != newStatus) {
157 164
             reportStatus = newStatus;
165
+            eventBus.publishAsync(new ProgramErrorStatusEvent(this));
158 166
             errorManager.fireErrorStatusChanged(this);
159
-
160
-            synchronized (this) {
161
-                notifyAll();
162
-            }
163 167
         }
164 168
     }
165 169
 

+ 18
- 15
test/com/dmdirc/logger/ProgramErrorTest.java View File

@@ -22,6 +22,7 @@
22 22
 
23 23
 package com.dmdirc.logger;
24 24
 
25
+import com.dmdirc.DMDircMBassador;
25 26
 import com.dmdirc.util.ClientInfo;
26 27
 
27 28
 import com.google.common.collect.Lists;
@@ -41,42 +42,44 @@ import static org.junit.Assert.assertTrue;
41 42
 public class ProgramErrorTest {
42 43
 
43 44
     @Mock private ErrorManager errorManager;
45
+    @Mock private DMDircMBassador eventBus;
44 46
     @Mock private ClientInfo clientInfo;
45 47
 
46 48
     @Test(expected = NullPointerException.class)
47 49
     public void testConstructorNullErrorLevel() {
48
-        new ProgramError(null, "moo", null, Lists.newArrayList(), null, new Date(), errorManager);
50
+        new ProgramError(null, "moo", null, Lists.newArrayList(), null, new Date(), errorManager,
51
+                eventBus);
49 52
     }
50 53
 
51 54
     @Test(expected = NullPointerException.class)
52 55
     public void testConstructorNullMessage() {
53 56
         new ProgramError(ErrorLevel.HIGH, null, null, Lists.newArrayList(), null, new Date(),
54
-                errorManager);
57
+                errorManager, eventBus);
55 58
     }
56 59
 
57 60
     @Test(expected = IllegalArgumentException.class)
58 61
     public void testConstructorEmptyMessage() {
59 62
         new ProgramError(ErrorLevel.HIGH, "", null, Lists.newArrayList(), null, new Date(),
60
-                errorManager);
63
+                errorManager, eventBus);
61 64
     }
62 65
 
63 66
     @Test(expected = NullPointerException.class)
64 67
     public void testConstructorNullDate() {
65 68
         new ProgramError(ErrorLevel.HIGH, "moo", null, Lists.newArrayList(), null, null,
66
-                errorManager);
69
+                errorManager, eventBus);
67 70
     }
68 71
 
69 72
     @Test
70 73
     public void testConstructorGood() {
71 74
         new ProgramError(ErrorLevel.HIGH, "moo", new UnsupportedOperationException(),
72
-                Lists.newArrayList(), null, new Date(), errorManager);
75
+                Lists.newArrayList(), null, new Date(), errorManager, eventBus);
73 76
     }
74 77
 
75 78
     @Test
76 79
     public void testGetLevel() {
77 80
         final ProgramError pe = new ProgramError(ErrorLevel.HIGH, "moo",
78 81
                 new UnsupportedOperationException(), Lists.newArrayList(), null, new Date(),
79
-                errorManager);
82
+                errorManager, eventBus);
80 83
         assertEquals(ErrorLevel.HIGH, pe.getLevel());
81 84
     }
82 85
 
@@ -84,7 +87,7 @@ public class ProgramErrorTest {
84 87
     public void testGetMessage() {
85 88
         final ProgramError pe = new ProgramError(ErrorLevel.HIGH, "moo",
86 89
                 new UnsupportedOperationException(), Lists.newArrayList(), null, new Date(),
87
-                errorManager);
90
+                errorManager, eventBus);
88 91
         assertEquals("moo", pe.getMessage());
89 92
     }
90 93
 
@@ -93,7 +96,7 @@ public class ProgramErrorTest {
93 96
         final Date date = new Date();
94 97
         final ProgramError pe = new ProgramError(ErrorLevel.HIGH, "moo",
95 98
                 new UnsupportedOperationException(), Lists.newArrayList(), null, date,
96
-                errorManager);
99
+                errorManager, eventBus);
97 100
         assertEquals(date, pe.getDate());
98 101
     }
99 102
 
@@ -101,7 +104,7 @@ public class ProgramErrorTest {
101 104
     public void testReportStatus() {
102 105
         final ProgramError pe = new ProgramError(ErrorLevel.HIGH, "moo",
103 106
                 new UnsupportedOperationException(), Lists.newArrayList(), null, new Date(),
104
-                errorManager);
107
+                errorManager, eventBus);
105 108
         assertEquals(ErrorReportStatus.WAITING, pe.getReportStatus());
106 109
         pe.setReportStatus(null);
107 110
         assertEquals(ErrorReportStatus.WAITING, pe.getReportStatus());
@@ -115,7 +118,7 @@ public class ProgramErrorTest {
115 118
     public void testToString() {
116 119
         final ProgramError pe = new ProgramError(ErrorLevel.HIGH, "moo",
117 120
                 new UnsupportedOperationException(), Lists.newArrayList(), null, new Date(),
118
-                errorManager);
121
+                errorManager, eventBus);
119 122
         assertTrue(pe.toString().contains("moo"));
120 123
     }
121 124
 
@@ -123,15 +126,15 @@ public class ProgramErrorTest {
123 126
     public void testEquals() {
124 127
         final Exception ex = new UnsupportedOperationException();
125 128
         final ProgramError pe1 = new ProgramError(ErrorLevel.LOW, "moo",
126
-                ex, Lists.newArrayList(), null, new Date(), errorManager);
129
+                ex, Lists.newArrayList(), null, new Date(), errorManager, eventBus);
127 130
         final ProgramError pe2 = new ProgramError(ErrorLevel.LOW, "moo",
128
-                ex, Lists.newArrayList(), null, new Date(), errorManager);
131
+                ex, Lists.newArrayList(), null, new Date(), errorManager, eventBus);
129 132
         final ProgramError pe3 = new ProgramError(ErrorLevel.MEDIUM, "moo",
130
-                ex, Lists.newArrayList(), null, new Date(), errorManager);
133
+                ex, Lists.newArrayList(), null, new Date(), errorManager, eventBus);
131 134
         final ProgramError pe4 = new ProgramError(ErrorLevel.LOW, "bar",
132
-                ex, Lists.newArrayList(), null, new Date(), errorManager);
135
+                ex, Lists.newArrayList(), null, new Date(), errorManager, eventBus);
133 136
         final ProgramError pe5 = new ProgramError(ErrorLevel.LOW, "moo",
134
-                null, Lists.newArrayList(), "Hello", new Date(), errorManager);
137
+                null, Lists.newArrayList(), "Hello", new Date(), errorManager, eventBus);
135 138
 
136 139
         assertFalse(pe1.equals(null)); // NOPMD
137 140
         assertFalse(pe1.equals("moo"));

Loading…
Cancel
Save