-
Notifications
You must be signed in to change notification settings - Fork 112
Better crash reporter error grouping from RenderContext assertions #1302
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
66 changes: 62 additions & 4 deletions
66
workflow-runtime/src/commonMain/kotlin/com/squareup/workflow1/internal/Throwables.kt
This file contains hidden or 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 |
|---|---|---|
| @@ -1,8 +1,66 @@ | ||
| package com.squareup.workflow1.internal | ||
|
|
||
| import kotlinx.coroutines.CancellationException | ||
| import kotlin.contracts.ExperimentalContracts | ||
| import kotlin.contracts.contract | ||
|
|
||
| internal tailrec fun Throwable.unwrapCancellationCause(): Throwable? { | ||
| if (this !is CancellationException) return this | ||
| return cause?.unwrapCancellationCause() | ||
| /** | ||
| * Like Kotlin's [require], but uses [stackTraceKey] to create a fake top element | ||
| * on the stack trace, ensuring that crash reporter's default grouping will create unique | ||
| * groups for unique keys. | ||
| * | ||
| * So far [stackTraceKey] is only effective on JVM, it has no effect in other languages. | ||
| * | ||
| * @see [withKey] | ||
| * | ||
| * @throws IllegalArgumentException if the [value] is false. | ||
| */ | ||
| @OptIn(ExperimentalContracts::class) | ||
| internal inline fun requireWithKey( | ||
| value: Boolean, | ||
| stackTraceKey: Any, | ||
| lazyMessage: () -> Any = { "Failed requirement." } | ||
| ) { | ||
| contract { | ||
| returns() implies value | ||
| } | ||
| if (!value) { | ||
| val message = lazyMessage() | ||
| val exception: Throwable = IllegalArgumentException(message.toString()) | ||
| throw exception.withKey(stackTraceKey) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Like Kotlin's [check], but uses [stackTraceKey] to create a fake top element | ||
| * on the stack trace, ensuring that crash reporter's default grouping will create unique | ||
| * groups for unique keys. | ||
| * | ||
| * So far [stackTraceKey] is only effective on JVM, it has no effect in other languages. | ||
| * | ||
| * @see [withKey] | ||
| * | ||
| * @throws IllegalStateException if the [value] is false. | ||
| */ | ||
| @OptIn(ExperimentalContracts::class) | ||
| internal inline fun checkWithKey( | ||
| value: Boolean, | ||
| stackTraceKey: Any, | ||
| lazyMessage: () -> Any = { "Check failed." } | ||
| ) { | ||
| contract { | ||
| returns() implies value | ||
| } | ||
| if (!value) { | ||
| val message = lazyMessage() | ||
| val exception: Throwable = IllegalStateException(message.toString()) | ||
| throw exception.withKey(stackTraceKey) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Uses [stackTraceKey] to create a fake top element on the stack trace, ensuring | ||
| * that crash reporter's default grouping will create unique groups for unique keys. | ||
| * | ||
| * So far only effective on JVM, this is a pass through in other languages. | ||
| */ | ||
| internal expect fun <T : Throwable> T.withKey(stackTraceKey: Any): T | ||
This file contains hidden or 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
3 changes: 3 additions & 0 deletions
3
workflow-runtime/src/iosMain/kotlin/com/squareup/workflow1/internal/Throwables.ios.kt
This file contains hidden or 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,3 @@ | ||
| package com.squareup.workflow1.internal | ||
|
|
||
| actual fun <T : Throwable> T.withKey(stackTraceKey: Any): T = this |
3 changes: 3 additions & 0 deletions
3
workflow-runtime/src/jsMain/kotlin/com/squareup/workflow1/internal/Throwables.js.kt
This file contains hidden or 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,3 @@ | ||
| package com.squareup.workflow1.internal | ||
|
|
||
| actual fun <T : Throwable> T.withKey(stackTraceKey: Any): T = this |
13 changes: 13 additions & 0 deletions
13
workflow-runtime/src/jvmMain/kotlin/com/squareup/workflow1/internal/Throwables.jvm.kt
This file contains hidden or 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,13 @@ | ||
| package com.squareup.workflow1.internal | ||
|
|
||
| internal actual fun <T : Throwable> T.withKey(stackTraceKey: Any): T = apply { | ||
| val realTop = stackTrace[0] | ||
| val fakeTop = StackTraceElement( | ||
| // Real class name to ensure that we are still "in project". | ||
| realTop.className, | ||
| "fakeMethodForCrashGrouping", | ||
| /* fileName = */ stackTraceKey.toString(), | ||
| /* lineNumber = */ stackTraceKey.hashCode() | ||
| ) | ||
| stackTrace = stackTrace.toMutableList().apply { add(0, fakeTop) }.toTypedArray() | ||
| } |
39 changes: 39 additions & 0 deletions
39
workflow-runtime/src/jvmTest/kotlin/com/squareup/workflow1/internal/ThrowablesTest.kt
This file contains hidden or 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,39 @@ | ||
| package com.squareup.workflow1.internal | ||
|
|
||
| import kotlin.test.Test | ||
| import kotlin.test.assertEquals | ||
|
|
||
| class ThrowablesTest { | ||
|
|
||
| @Test fun `requireWithKey throws IllegalArgumentException`() { | ||
| try { | ||
| requireWithKey(false, "requiredKey") { "message" } | ||
| } catch (e: IllegalArgumentException) { | ||
| assertEquals("message", e.message) | ||
| e.assertIsKeyedException("requiredKey") | ||
| return | ||
| } | ||
| } | ||
|
|
||
| @Test fun `checkWithKey throws IllegalStateException`() { | ||
| try { | ||
| checkWithKey(false, "checkedKey") { "message" } | ||
| } catch (e: IllegalStateException) { | ||
| assertEquals("message", e.message) | ||
| e.assertIsKeyedException("checkedKey") | ||
| return | ||
| } | ||
| } | ||
|
|
||
| @Test fun `Throwable withKey adds frame based on key`() { | ||
| RuntimeException("cause").withKey("key").assertIsKeyedException("key") | ||
| } | ||
|
|
||
| private fun RuntimeException.assertIsKeyedException(key: String) { | ||
| val top = stackTrace[0] | ||
| val topPlusOne = stackTrace[1] | ||
| assertEquals(topPlusOne.className, top.className, "Uses real class name") | ||
| assertEquals(key, top.fileName) | ||
| assertEquals(key.hashCode(), top.lineNumber) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was unused.