-
Notifications
You must be signed in to change notification settings - Fork 205
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #553 from bugsnag/anr-message-improvements
Improve ANR error message information
- Loading branch information
Showing
14 changed files
with
444 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
bugsnag-android-core/src/androidTest/java/com/bugsnag/android/BugsnagExceptionTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package com.bugsnag.android | ||
|
||
import com.bugsnag.android.BugsnagTestUtils.generateConfiguration | ||
import com.bugsnag.android.BugsnagTestUtils.streamableToJsonArray | ||
import org.json.JSONArray | ||
import org.json.JSONObject | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Assert.assertTrue | ||
import org.junit.Test | ||
|
||
class BugsnagExceptionTest { | ||
|
||
private val config = generateConfiguration() | ||
|
||
@Test | ||
fun stacktraceConstructorSerialisation() { | ||
val trace = arrayOf(StackTraceElement("Foo", "bar", "Foo.kt", 1)) | ||
val exc = jsonObjectFromException(BugsnagException("MyClass", "Custom message", trace)) | ||
assertEquals("MyClass", exc.get("errorClass")) | ||
assertEquals("Custom message", exc.get("message")) | ||
assertEquals("android", exc.get("type")) | ||
|
||
val stacktrace = exc.get("stacktrace") as JSONArray | ||
assertTrue(stacktrace.length() > 0) | ||
} | ||
|
||
@Test | ||
fun throwableConstructorSerialisation() { | ||
val exc = jsonObjectFromException(BugsnagException(RuntimeException("oops"))) | ||
assertEquals("java.lang.RuntimeException", exc.get("errorClass")) | ||
assertEquals("oops", exc.get("message")) | ||
assertEquals("android", exc.get("type")) | ||
|
||
val stacktrace = exc.get("stacktrace") as JSONArray | ||
assertTrue(stacktrace.length() > 0) | ||
} | ||
|
||
@Test | ||
fun overrideName() { | ||
val bugsnagException = BugsnagException(RuntimeException("oops")) | ||
bugsnagException.name = "FatalNetworkError" | ||
val exc = jsonObjectFromException(bugsnagException) | ||
assertEquals("FatalNetworkError", exc.get("errorClass")) | ||
} | ||
|
||
@Test | ||
fun overrideMessage() { | ||
val bugsnagException = BugsnagException(RuntimeException("oops")) | ||
bugsnagException.setMessage("User not found") | ||
val exc = jsonObjectFromException(bugsnagException) | ||
assertEquals("User not found", exc.get("message")) | ||
} | ||
|
||
@Test | ||
fun overrideType() { | ||
val bugsnagException = BugsnagException(RuntimeException("oops")) | ||
bugsnagException.type = "browserjs" | ||
val exc = jsonObjectFromException(bugsnagException) | ||
assertEquals("browserjs", exc.get("type")) | ||
} | ||
|
||
@Test | ||
fun nestedBugsnagException() { | ||
val nestedThrowable = BugsnagException(BugsnagException(RuntimeException("oops"))) | ||
val exc = jsonObjectFromException(nestedThrowable) | ||
assertEquals("java.lang.RuntimeException", exc.get("errorClass")) | ||
assertEquals("oops", exc.get("message")) | ||
assertEquals("android", exc.get("type")) | ||
|
||
val stacktrace = exc.get("stacktrace") as JSONArray | ||
assertTrue(stacktrace.length() > 0) | ||
} | ||
|
||
private fun jsonObjectFromException(bugsnagException: BugsnagException): JSONObject { | ||
val exceptions = Exceptions(config, bugsnagException) | ||
val json = streamableToJsonArray(exceptions) | ||
val exc = json.get(0) as JSONObject | ||
return exc | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
bugsnag-android-core/src/androidTest/java/com/bugsnag/android/ErrorNameTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.bugsnag.android | ||
|
||
import org.junit.Assert.assertEquals | ||
import org.junit.Assert.assertTrue | ||
import org.junit.Before | ||
import org.junit.Test | ||
|
||
class ErrorNameTest { | ||
|
||
private lateinit var error: Error | ||
|
||
@Before | ||
fun setUp() { | ||
val config = Configuration("api-key") | ||
val exception = RuntimeException("Example message", RuntimeException("Another")) | ||
error = Error.Builder(config, exception, null, Thread.currentThread(), false).build() | ||
} | ||
|
||
@Test | ||
fun exceptionTypeConverted() { | ||
assertTrue(error.exception is BugsnagException) | ||
assertTrue(error.exceptions.exception is BugsnagException) | ||
assertTrue(error.exceptions.exception.cause is RuntimeException) | ||
} | ||
|
||
@Test | ||
fun defaultExceptionName() { | ||
assertEquals("java.lang.RuntimeException", error.exceptionName) | ||
} | ||
|
||
@Test | ||
fun defaultExceptionMessage() { | ||
assertEquals("Example message", error.exceptionMessage) | ||
} | ||
|
||
@Test | ||
fun overrideExceptionName() { | ||
error.exceptionName = "Foo" | ||
assertEquals("Foo", error.exceptionName) | ||
} | ||
|
||
@Test | ||
fun overrideExceptionMessage() { | ||
error.exceptionMessage = "Some custom message" | ||
assertEquals("Some custom message", error.exceptionMessage) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 0 additions & 92 deletions
92
bugsnag-android-core/src/androidTest/java/com/bugsnag/android/ExceptionsTest.java
This file was deleted.
Oops, something went wrong.
73 changes: 73 additions & 0 deletions
73
bugsnag-android-core/src/androidTest/java/com/bugsnag/android/ExceptionsTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package com.bugsnag.android | ||
|
||
import com.bugsnag.android.BugsnagTestUtils.generateConfiguration | ||
import com.bugsnag.android.BugsnagTestUtils.streamableToJsonArray | ||
import org.json.JSONObject | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Assert.assertNotNull | ||
import org.junit.Before | ||
import org.junit.Test | ||
|
||
class ExceptionsTest { | ||
|
||
private lateinit var config: Configuration | ||
|
||
@Before | ||
fun setUp() { | ||
config = generateConfiguration() | ||
} | ||
|
||
@Test | ||
fun testBasicException() { | ||
val oops = RuntimeException("oops") | ||
val exceptions = Exceptions(config, BugsnagException(oops)) | ||
val exceptionsJson = streamableToJsonArray(exceptions) | ||
|
||
assertEquals(1, exceptionsJson.length().toLong()) | ||
|
||
val firstException = exceptionsJson.get(0) as JSONObject | ||
assertEquals("java.lang.RuntimeException", firstException.get("errorClass")) | ||
assertEquals("oops", firstException.get("message")) | ||
assertNotNull(firstException.get("stacktrace")) | ||
} | ||
|
||
@Test | ||
fun testCauseException() { | ||
val ex = RuntimeException("oops", Exception("cause")) | ||
val exceptions = Exceptions(config, BugsnagException(ex)) | ||
val exceptionsJson = streamableToJsonArray(exceptions) | ||
|
||
assertEquals(2, exceptionsJson.length().toLong()) | ||
|
||
val firstException = exceptionsJson.get(0) as JSONObject | ||
assertEquals("java.lang.RuntimeException", firstException.get("errorClass")) | ||
assertEquals("oops", firstException.get("message")) | ||
assertNotNull(firstException.get("stacktrace")) | ||
|
||
val causeException = exceptionsJson.get(1) as JSONObject | ||
assertEquals("java.lang.Exception", causeException.get("errorClass")) | ||
assertEquals("cause", causeException.get("message")) | ||
assertNotNull(causeException.get("stacktrace")) | ||
} | ||
|
||
@Test | ||
fun testNamedException() { | ||
val element = StackTraceElement("Class", "method", "Class.java", 123) | ||
val frames = arrayOf(element) | ||
val error = Error.Builder( | ||
config, "RuntimeException", | ||
"Example message", frames, BugsnagTestUtils.generateSessionTracker(), | ||
Thread.currentThread() | ||
).build() | ||
val exceptions = Exceptions(config, BugsnagException(error.exception)) | ||
|
||
val exceptionJson = streamableToJsonArray(exceptions).getJSONObject(0) | ||
assertEquals("RuntimeException", exceptionJson.get("errorClass")) | ||
assertEquals("Example message", exceptionJson.get("message")) | ||
|
||
val stackframeJson = exceptionJson.getJSONArray("stacktrace").getJSONObject(0) | ||
assertEquals("Class.method", stackframeJson.get("method")) | ||
assertEquals("Class.java", stackframeJson.get("file")) | ||
assertEquals(123, stackframeJson.get("lineNumber")) | ||
} | ||
} |
Oops, something went wrong.