Skip to content

Commit

Permalink
feat: update exceptionHandler threadstate to record exception stacktr…
Browse files Browse the repository at this point in the history
…ace for unhandled errors
  • Loading branch information
fractalwrench committed Aug 9, 2018
1 parent 3949a23 commit 2f83a7c
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,6 @@ internal fun createCustomHeaderDelivery(context: Context): Delivery {

internal fun writeErrorToStore(client: Client) {
val error = Error.Builder(Configuration("api-key"), RuntimeException(), null,
Thread.currentThread()).build()
Thread.currentThread(), false).build()
client.errorStore.write(error)
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class BeforeNotifyTest {
}

val error = Error.Builder(config, RuntimeException("Test"), null,
Thread.currentThread()).build()
Thread.currentThread(), false).build()
beforeNotify.run(error)
assertEquals(context, error.context)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void testWrite() throws Exception {
@NonNull
private Error writeErrorToStore() {
Error error = new Error.Builder(config, new RuntimeException(),
null, Thread.currentThread()).build();
null, Thread.currentThread(), false).build();
errorStore.write(error);
return error;
}
Expand Down
31 changes: 16 additions & 15 deletions sdk/src/androidTest/java/com/bugsnag/android/ErrorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class ErrorTest {
public void setUp() throws Exception {
config = new Configuration("api-key");
RuntimeException exception = new RuntimeException("Example message");
error = new Error.Builder(config, exception, null, Thread.currentThread()).build();
error = new Error.Builder(config, exception, null, Thread.currentThread(), false).build();
}

@After
Expand All @@ -56,13 +56,13 @@ public void testShouldIgnoreClass() {
// Shouldn't ignore classes not in ignoreClasses
RuntimeException runtimeException = new RuntimeException("Test");
Error error = new Error.Builder(config,
runtimeException, null, Thread.currentThread()).build();
runtimeException, null, Thread.currentThread(), false).build();
assertFalse(error.shouldIgnoreClass());

// Should ignore errors in ignoreClasses
IOException ioException = new IOException("Test");
error = new Error.Builder(config,
ioException, null, Thread.currentThread()).build();
ioException, null, Thread.currentThread(), false).build();
assertTrue(error.shouldIgnoreClass());
}

Expand Down Expand Up @@ -94,7 +94,7 @@ public void testBasicSerialization() throws JSONException, IOException {
@Test
public void testHandledSerialisation() throws Exception {
Error err = new Error.Builder(config,
new RuntimeException(), null, Thread.currentThread())
new RuntimeException(), null, Thread.currentThread(), false)
.severityReasonType(HandledState.REASON_HANDLED_EXCEPTION)
.build();

Expand All @@ -112,7 +112,7 @@ public void testHandledSerialisation() throws Exception {
@Test
public void testUnhandledSerialisation() throws Exception {
Error err = new Error.Builder(config,
new RuntimeException(), null, Thread.currentThread())
new RuntimeException(), null, Thread.currentThread(), false)
.severityReasonType(HandledState.REASON_UNHANDLED_EXCEPTION)
.severity(Severity.ERROR)
.build();
Expand All @@ -131,7 +131,7 @@ public void testUnhandledSerialisation() throws Exception {
@Test
public void testPromiseRejectionSerialisation() throws Exception {
Error err = new Error.Builder(config,
new RuntimeException(), null, Thread.currentThread())
new RuntimeException(), null, Thread.currentThread(), false)
.severityReasonType(HandledState.REASON_PROMISE_REJECTION)
.severity(Severity.ERROR)
.build();
Expand All @@ -150,7 +150,7 @@ public void testPromiseRejectionSerialisation() throws Exception {
@Test
public void testLogSerialisation() throws Exception {
Error err = new Error.Builder(config,
new RuntimeException(), null, Thread.currentThread())
new RuntimeException(), null, Thread.currentThread(), false)
.severityReasonType(HandledState.REASON_LOG)
.severity(Severity.WARNING)
.attributeValue("warning")
Expand Down Expand Up @@ -186,7 +186,7 @@ public void testUserSpecifiedSerialisation() throws Exception {
@Test
public void testStrictModeSerialisation() throws Exception {
Error err = new Error.Builder(config,
new RuntimeException(), null, Thread.currentThread())
new RuntimeException(), null, Thread.currentThread(), false)
.severityReasonType(HandledState.REASON_STRICT_MODE)
.attributeValue("Test")
.build();
Expand Down Expand Up @@ -252,7 +252,7 @@ public void testSetSeverity() throws JSONException, IOException {
public void testSessionIncluded() throws Exception {
Session session = generateSession();
Error err = new Error.Builder(config,
new RuntimeException(), session, Thread.currentThread()).build();
new RuntimeException(), session, Thread.currentThread(), false).build();

JSONObject errorJson = streamableToJson(err);
assertNotNull(errorJson);
Expand All @@ -273,7 +273,7 @@ public void testSessionIncluded() throws Exception {
@Test(expected = JSONException.class)
public void testSessionExcluded() throws Exception {
Error err = new Error.Builder(config,
new RuntimeException(), null, Thread.currentThread()).build();
new RuntimeException(), null, Thread.currentThread(), false).build();

JSONObject errorJson = streamableToJson(err);
assertNotNull(errorJson);
Expand All @@ -284,11 +284,11 @@ public void testSessionExcluded() throws Exception {
public void checkExceptionMessageNullity() throws Exception {
String msg = "Foo";
Error err = new Error.Builder(config,
new RuntimeException(msg), null, Thread.currentThread()).build();
new RuntimeException(msg), null, Thread.currentThread(), false).build();
assertEquals(msg, err.getExceptionMessage());

err = new Error.Builder(config,
new RuntimeException(), null, Thread.currentThread()).build();
new RuntimeException(), null, Thread.currentThread(), false).build();
assertEquals("", err.getExceptionMessage());
}

Expand All @@ -310,7 +310,7 @@ public void testBugsnagExceptionName() throws Exception {
BugsnagException exception = new BugsnagException("Busgang", "exceptional",
new StackTraceElement[]{});
Error err = new Error.Builder(config,
exception, null, Thread.currentThread()).build();
exception, null, Thread.currentThread(), false).build();
assertEquals("Busgang", err.getExceptionName());
}

Expand Down Expand Up @@ -370,7 +370,7 @@ public void testSetUser() throws Exception {
public void testBuilderMetaData() {
Configuration config = new Configuration("api-key");
Error.Builder builder = new Error.Builder(config,
new RuntimeException("foo"), null, Thread.currentThread());
new RuntimeException("foo"), null, Thread.currentThread(), false);

assertNotNull(builder.metaData(new MetaData()).build());

Expand Down Expand Up @@ -414,7 +414,8 @@ public void testBuilderNullSession() throws Throwable {

Session session = generateSession();
session.setAutoCaptured(true);
error = new Error.Builder(config, exception, session, Thread.currentThread()).build();
error = new Error.Builder(config, exception, session,
Thread.currentThread(), false).build();

JSONObject errorJson = streamableToJson(error);
assertFalse(errorJson.has("session"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ public void tearDown() throws Exception {

@Test
public void testErrorDefaultMetaData() throws Exception {
Error error = new Error.Builder(config, throwable, null, Thread.currentThread()).build();
Error error = new Error.Builder(config, throwable, null,
Thread.currentThread(), false).build();
validateDefaultMetadata(error.getMetaData());
}

Expand All @@ -56,7 +57,8 @@ public void testSecondErrorDefaultMetaData() throws Exception {

@Test
public void testErrorSetMetadataRef() throws Exception {
Error error = new Error.Builder(config, throwable, null, Thread.currentThread()).build();
Error error = new Error.Builder(config, throwable, null,
Thread.currentThread(), false).build();
MetaData metaData = new MetaData();
metaData.addToTab(TAB_KEY, "test", "data");
error.setMetaData(metaData);
Expand All @@ -65,7 +67,8 @@ public void testErrorSetMetadataRef() throws Exception {

@Test
public void testErrorSetNullMetadata() throws Exception {
Error error = new Error.Builder(config, throwable, null, Thread.currentThread()).build();
Error error = new Error.Builder(config, throwable, null,
Thread.currentThread(), false).build();
error.setMetaData(null);
validateDefaultMetadata(error.getMetaData());
}
Expand Down Expand Up @@ -99,7 +102,7 @@ public boolean run(Error error) {
}
});
Error error = new Error.Builder(config, new Throwable(),
null, Thread.currentThread()).build();
null, Thread.currentThread(), false).build();
Client client = Bugsnag.getClient();
client.notify(error, DeliveryStyle.SAME_THREAD, null);
}
Expand Down
3 changes: 2 additions & 1 deletion sdk/src/androidTest/java/com/bugsnag/android/ReportTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public class ReportTest {
public void setUp() throws Exception {
Configuration config = new Configuration("example-api-key");
RuntimeException exception = new RuntimeException("Something broke");
Error error = new Error.Builder(config, exception, null, Thread.currentThread()).build();
Error error = new Error.Builder(config, exception, null,
Thread.currentThread(), false).build();
report = new Report("api-key", error);
}

Expand Down
24 changes: 12 additions & 12 deletions sdk/src/main/java/com/bugsnag/android/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,7 @@ public void beforeRecordBreadcrumb(BeforeRecordBreadcrumb beforeRecordBreadcrumb
*/
public void notify(@NonNull Throwable exception) {
Error error = new Error.Builder(config, exception, sessionTracker.getCurrentSession(),
Thread.currentThread())
Thread.currentThread(), false)
.severityReasonType(HandledState.REASON_HANDLED_EXCEPTION)
.build();
notify(error, !BLOCKING);
Expand All @@ -777,7 +777,7 @@ public void notify(@NonNull Throwable exception) {
*/
public void notify(@NonNull Throwable exception, Callback callback) {
Error error = new Error.Builder(config, exception, sessionTracker.getCurrentSession(),
Thread.currentThread())
Thread.currentThread(), false)
.severityReasonType(HandledState.REASON_HANDLED_EXCEPTION)
.build();
notify(error, DeliveryStyle.ASYNC, callback);
Expand Down Expand Up @@ -812,7 +812,7 @@ public void notify(@NonNull String name,
*/
public void notify(@NonNull Throwable exception, Severity severity) {
Error error = new Error.Builder(config, exception, sessionTracker.getCurrentSession(),
Thread.currentThread())
Thread.currentThread(), false)
.severity(severity)
.build();
notify(error, !BLOCKING);
Expand All @@ -829,7 +829,7 @@ public void notify(@NonNull Throwable exception, Severity severity) {
public void notify(@NonNull Throwable exception,
@NonNull MetaData metaData) {
Error error = new Error.Builder(config, exception, sessionTracker.getCurrentSession(),
Thread.currentThread())
Thread.currentThread(), false)
.metaData(metaData)
.severityReasonType(HandledState.REASON_HANDLED_EXCEPTION)
.build();
Expand All @@ -849,7 +849,7 @@ public void notify(@NonNull Throwable exception,
public void notify(@NonNull Throwable exception, Severity severity,
@NonNull MetaData metaData) {
Error error = new Error.Builder(config, exception, sessionTracker.getCurrentSession(),
Thread.currentThread())
Thread.currentThread(), false)
.metaData(metaData)
.severity(severity)
.build();
Expand Down Expand Up @@ -1011,7 +1011,7 @@ public void run() {
*/
public void notifyBlocking(@NonNull Throwable exception) {
Error error = new Error.Builder(config, exception, sessionTracker.getCurrentSession(),
Thread.currentThread())
Thread.currentThread(), false)
.severityReasonType(HandledState.REASON_HANDLED_EXCEPTION)
.build();
notify(error, BLOCKING);
Expand All @@ -1026,7 +1026,7 @@ public void notifyBlocking(@NonNull Throwable exception) {
*/
public void notifyBlocking(@NonNull Throwable exception, Callback callback) {
Error error = new Error.Builder(config, exception, sessionTracker.getCurrentSession(),
Thread.currentThread())
Thread.currentThread(), false)
.severityReasonType(HandledState.REASON_HANDLED_EXCEPTION)
.build();
notify(error, DeliveryStyle.SAME_THREAD, callback);
Expand Down Expand Up @@ -1063,7 +1063,7 @@ public void notifyBlocking(@NonNull String name,
public void notifyBlocking(@NonNull Throwable exception,
@NonNull MetaData metaData) {
Error error = new Error.Builder(config, exception, sessionTracker.getCurrentSession(),
Thread.currentThread())
Thread.currentThread(), false)
.severityReasonType(HandledState.REASON_HANDLED_EXCEPTION)
.metaData(metaData)
.build();
Expand All @@ -1083,7 +1083,7 @@ public void notifyBlocking(@NonNull Throwable exception,
public void notifyBlocking(@NonNull Throwable exception, Severity severity,
@NonNull MetaData metaData) {
Error error = new Error.Builder(config, exception, sessionTracker.getCurrentSession(),
Thread.currentThread())
Thread.currentThread(), false)
.metaData(metaData)
.severity(severity)
.build();
Expand Down Expand Up @@ -1154,7 +1154,7 @@ public void notifyBlocking(@NonNull String name,
*/
public void notifyBlocking(@NonNull Throwable exception, Severity severity) {
Error error = new Error.Builder(config, exception,
sessionTracker.getCurrentSession(), Thread.currentThread())
sessionTracker.getCurrentSession(), Thread.currentThread(), false)
.severity(severity)
.build();
notify(error, BLOCKING);
Expand Down Expand Up @@ -1183,7 +1183,7 @@ public void internalClientNotify(@NonNull Throwable exception,

@SuppressWarnings("WrongConstant")
Error error = new Error.Builder(config, exception,
sessionTracker.getCurrentSession(), Thread.currentThread())
sessionTracker.getCurrentSession(), Thread.currentThread(), false)
.severity(Severity.fromString(severity))
.severityReasonType(severityReason)
.attributeValue(logLevel)
Expand Down Expand Up @@ -1342,7 +1342,7 @@ void cacheAndNotify(@NonNull Throwable exception, Severity severity, MetaData me
@HandledState.SeverityReason String severityReason,
@Nullable String attributeValue, Thread thread) {
Error error = new Error.Builder(config, exception,
sessionTracker.getCurrentSession(), thread)
sessionTracker.getCurrentSession(), thread, true)
.severity(severity)
.metaData(metaData)
.severityReasonType(severityReason)
Expand Down
10 changes: 6 additions & 4 deletions sdk/src/main/java/com/bugsnag/android/Error.java
Original file line number Diff line number Diff line change
Expand Up @@ -401,9 +401,11 @@ static class Builder {

Builder(@NonNull Configuration config,
@NonNull Throwable exception,
Session session,
Thread thread) {
this.threadState = new ThreadState(config, thread, Thread.getAllStackTraces(), null);
@Nullable Session session,
@NonNull Thread thread,
boolean unhandled) {
Throwable exc = unhandled ? exception : null;
this.threadState = new ThreadState(config, thread, Thread.getAllStackTraces(), exc);
this.config = config;
this.exception = exception;
this.severityReasonType = HandledState.REASON_USER_SPECIFIED; // default
Expand All @@ -419,7 +421,7 @@ static class Builder {
Builder(@NonNull Configuration config, @NonNull String name,
@NonNull String message, @NonNull StackTraceElement[] frames,
Session session, Thread thread) {
this(config, new BugsnagException(name, message, frames), session, thread);
this(config, new BugsnagException(name, message, frames), session, thread, false);
}

Builder severityReasonType(@HandledState.SeverityReason String severityReasonType) {
Expand Down

0 comments on commit 2f83a7c

Please sign in to comment.