-
Notifications
You must be signed in to change notification settings - Fork 0
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
Fix/crashlytics groups Napier error messages #76
Open
ninovanhooff
wants to merge
4
commits into
develop
Choose a base branch
from
fix/crashlytics-groups-error-messages
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e982302
WIP fix crashlytics log grouping
ninovanhooff c8e8160
Merge branch 'crash-logging' into fix/crashlytics-groups-error-messages
ninovanhooff 9a4ed7c
CHANGE Crashlyticslogger numToRemove to 9
ninovanhooff 6ab2fb2
Merge remote-tracking branch 'refs/remotes/origin/main' into fix/cras…
ninovanhooff 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 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 | ||||
---|---|---|---|---|---|---|
|
@@ -35,15 +35,44 @@ class CrashlyticsLogger : Antilog() { | |||||
|
||||||
val limitedMessage = message?.take(MAX_CHARS_IN_LOG) ?: "(no message)" // to avoid OutOfMemoryError's | ||||||
|
||||||
// at least one of message or throwable is not null | ||||||
if (priority < LogLevel.ERROR) { | ||||||
// at least one of message or throwable is not null | ||||||
val errorMessage = throwable?.let { | ||||||
" with error: ${throwable}: ${throwable.message}".take(MAX_CHARS_IN_LOG) | ||||||
} ?: "" | ||||||
Firebase.crashlytics.log(limitedMessage + errorMessage) | ||||||
} else { | ||||||
Firebase.crashlytics.log("recordException with message: $limitedMessage") | ||||||
Firebase.crashlytics.recordException(throwable ?: Exception(message)) | ||||||
Firebase.crashlytics.recordException(throwable ?: buildCrashlyticsSyntheticException(limitedMessage)) | ||||||
} | ||||||
} | ||||||
|
||||||
/** Strip the stacktrace so that the calls implementing error logging are removed and the actual | ||||||
* code that called Napier.e() remains. | ||||||
* | ||||||
* This is a workaround for the fact that Crashlytics groups errors by stacktrace | ||||||
* [https://stackoverflow.com/a/59779764](https://stackoverflow.com/a/59779764) | ||||||
*/ | ||||||
private fun buildCrashlyticsSyntheticException(message: String): Exception { | ||||||
val stackTrace = Thread.currentThread().stackTrace | ||||||
val numToRemove = 9 | ||||||
val lastToRemove = stackTrace[numToRemove - 1] | ||||||
if (lastToRemove.className != "io.github.aakira.napier.Napier" || lastToRemove.methodName != "e\$default"){ | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Could we substitute |
||||||
logcatLogger.log(priority = LogLevel.ERROR, tag = null, throwable = null, | ||||||
message = "Got unexpected stacktrace: class: ${lastToRemove.className}, method: ${lastToRemove.methodName}" | ||||||
) | ||||||
} | ||||||
val abbreviatedStackTrace = stackTrace.takeLast(stackTrace.size - numToRemove).toTypedArray() | ||||||
return SyntheticException("Synthetic Exception: $message", abbreviatedStackTrace) | ||||||
} | ||||||
|
||||||
} | ||||||
|
||||||
class SyntheticException( | ||||||
message: String, | ||||||
private val abbreviatedStackTrace: Array<StackTraceElement> | ||||||
) : Exception(message) { | ||||||
override fun getStackTrace(): Array<StackTraceElement> { | ||||||
return abbreviatedStackTrace | ||||||
} | ||||||
} |
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
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.
We can't never be 💯 % sure there are 8 items in the stacktrace array, so this could either throw an IndexOutOfBounds exception or return null, in the later case, then the
if
below should crash accessing the className, but since this code compiles I assume the array accessor is not nullable and simply throws an exception.Would you mind checking if this is the case and if so try to prevent it?