Skip to content

Commit 41f50db

Browse files
Merge pull request #82 from bugsnag/next
Next release
2 parents 1224f62 + e2a727f commit 41f50db

File tree

6 files changed

+121
-12
lines changed

6 files changed

+121
-12
lines changed

CHANGELOG.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
# Changelog
22

3-
## TBD
4-
3+
## 3.1.6 (2018-05-03)
54
* Make preemptive copy of map filtering specified keys
6-
[#77](https://github.com/bugsnag/bugsnag-java/pull/77)
75
[Leandro Aparecido](https://github.com/lehphyro)
6+
[#77](https://github.com/bugsnag/bugsnag-java/pull/77)
7+
* Add setter for overriding error class
8+
[Jamie Lynch](https://github.com/fractalwrench)
9+
[#78](https://github.com/bugsnag/bugsnag-java/pull/78)
810

911
## 3.1.5 (2018-03-08)
1012

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version=3.1.5
1+
version=3.1.6
22
group=com.bugsnag
33

44
# Default properties

src/main/java/com/bugsnag/Exception.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,17 @@
77
class Exception {
88
private Configuration config;
99
private Throwable throwable;
10+
private String errorClass;
1011

1112
Exception(Configuration config, Throwable throwable) {
1213
this.config = config;
1314
this.throwable = throwable;
15+
this.errorClass = throwable.getClass().getName();
1416
}
1517

1618
@Expose
1719
public String getErrorClass() {
18-
return throwable.getClass().getName();
20+
return errorClass;
1921
}
2022

2123
@Expose
@@ -27,4 +29,12 @@ public String getMessage() {
2729
public List<Stackframe> getStacktrace() {
2830
return Stackframe.getStacktrace(config, throwable.getStackTrace());
2931
}
32+
33+
public void setErrorClass(String errorClass) {
34+
this.errorClass = errorClass;
35+
}
36+
37+
public Throwable getThrowable() {
38+
return throwable;
39+
}
3040
}

src/main/java/com/bugsnag/Notifier.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
class Notifier {
66
private static final String NOTIFIER_NAME = "Bugsnag Java";
7-
private static final String NOTIFIER_VERSION = "3.1.5";
7+
private static final String NOTIFIER_VERSION = "3.1.6";
88
private static final String NOTIFIER_URL = "https://github.com/bugsnag/bugsnag-java";
99

1010
@Expose

src/main/java/com/bugsnag/Report.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class Report {
1515
private Configuration config;
1616

1717
private String apiKey;
18-
private Throwable throwable;
18+
private final Exception exception;
1919
private final HandledState handledState;
2020
private Severity severity;
2121
private String groupingHash;
@@ -35,7 +35,7 @@ protected Report(Configuration config, Throwable throwable) {
3535

3636
Report(Configuration config, Throwable throwable, HandledState handledState) {
3737
this.config = config;
38-
this.throwable = throwable;
38+
this.exception = new Exception(config, throwable);
3939
this.handledState = handledState;
4040
this.severity = handledState.getOriginalSeverity();
4141
}
@@ -53,8 +53,9 @@ protected String getPayloadVersion() {
5353
@Expose
5454
protected List<Exception> getExceptions() {
5555
List<Exception> exceptions = new ArrayList<Exception>();
56+
exceptions.add(exception);
5657

57-
Throwable currentThrowable = throwable;
58+
Throwable currentThrowable = exception.getThrowable().getCause();
5859
while (currentThrowable != null) {
5960
exceptions.add(new Exception(config, currentThrowable));
6061
currentThrowable = currentThrowable.getCause();
@@ -117,21 +118,30 @@ public Map getMetaData() {
117118
* @return The {@linkplain Throwable exception} which triggered this error report.
118119
*/
119120
public Throwable getException() {
120-
return throwable;
121+
return exception.getThrowable();
121122
}
122123

123124
/**
124125
* @return the class name from the exception contained in this error report.
125126
*/
126127
public String getExceptionName() {
127-
return throwable.getClass().getName();
128+
return exception.getErrorClass();
129+
}
130+
131+
/**
132+
* Sets the class name from the exception contained in this error report.
133+
*
134+
* @param exceptionName the error name
135+
*/
136+
public void setExceptionName(String exceptionName) {
137+
exception.setErrorClass(exceptionName);
128138
}
129139

130140
/**
131141
* @return The message from the exception contained in this error report.
132142
*/
133143
public String getExceptionMessage() {
134-
return throwable.getLocalizedMessage();
144+
return exception.getThrowable().getLocalizedMessage();
135145
}
136146

137147
/**
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.bugsnag;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertFalse;
5+
import static org.junit.Assert.assertNotNull;
6+
import static org.junit.Assert.assertTrue;
7+
8+
import com.bugsnag.callbacks.Callback;
9+
import com.bugsnag.delivery.Delivery;
10+
import com.bugsnag.serialization.Serializer;
11+
12+
import org.junit.Before;
13+
import org.junit.Test;
14+
15+
import java.util.List;
16+
17+
public class ExceptionTest {
18+
19+
private Exception exception;
20+
private RuntimeException ogThrowable;
21+
22+
/**
23+
* Initialises a config
24+
*
25+
* @throws Throwable if config couldn't be initialised
26+
*/
27+
@Before
28+
public void setUp() throws Throwable {
29+
Configuration config = new Configuration("api-key");
30+
ogThrowable = new RuntimeException("Test");
31+
exception = new Exception(config, ogThrowable);
32+
}
33+
34+
@Test
35+
public void testDefaults() throws Throwable {
36+
assertEquals("java.lang.RuntimeException", exception.getErrorClass());
37+
assertEquals("Test", exception.getMessage());
38+
assertEquals(ogThrowable, exception.getThrowable());
39+
assertFalse(exception.getStacktrace().isEmpty());
40+
}
41+
42+
@Test
43+
public void testClassOverride() throws Throwable {
44+
exception.setErrorClass("Hello");
45+
assertEquals("Hello", exception.getErrorClass());
46+
assertEquals("Test", exception.getMessage());
47+
}
48+
49+
@Test
50+
public void testReportCallback() throws Throwable {
51+
Bugsnag bugsnag = new Bugsnag("apikey");
52+
bugsnag.setDelivery(new Delivery() {
53+
@Override
54+
public void deliver(Serializer serializer, Object object) {
55+
}
56+
57+
@Override
58+
public void close() {
59+
}
60+
});
61+
assertTrue(bugsnag.notify(ogThrowable, new Callback() {
62+
@Override
63+
public void beforeNotify(Report report) {
64+
try {
65+
assertEquals(ogThrowable, report.getException());
66+
assertEquals("Test", report.getExceptionMessage());
67+
assertEquals("java.lang.RuntimeException", report.getExceptionName());
68+
69+
report.setExceptionName("Foo");
70+
assertEquals("Foo", report.getExceptionName());
71+
72+
73+
List<Exception> exceptions = report.getExceptions();
74+
assertEquals(1, exceptions.size());
75+
76+
Exception exception = exceptions.get(0);
77+
assertNotNull(exception);
78+
assertEquals("Foo", exception.getErrorClass());
79+
assertEquals("Test", exception.getMessage());
80+
} catch (Throwable throwable) {
81+
report.cancel();
82+
}
83+
}
84+
}));
85+
}
86+
87+
}

0 commit comments

Comments
 (0)