Skip to content

Commit 6f10e1b

Browse files
Merge pull request #2024 from Infomaniak/sentry-scope
Update Sentry scope usage
2 parents fe6f66e + 52afa00 commit 6f10e1b

19 files changed

+81
-117
lines changed

app/src/main/java/com/infomaniak/mail/data/api/UnwrappingJsonListSerializer.kt

+1-2
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,8 @@ object UnwrappingJsonListSerializer : JsonTransformingSerializer<String>(String.
2727

2828
override fun transformDeserialize(element: JsonElement): JsonElement {
2929
return if (element is JsonArray) {
30-
Sentry.withScope { scope ->
30+
Sentry.captureMessage("Found an array of inReplyTo") { scope ->
3131
scope.setExtra("inReplyToList", "ids: ${element.map { it }}")
32-
Sentry.captureMessage("Found an array of inReplyTo")
3332
}
3433
element.first()
3534
} else {

app/src/main/java/com/infomaniak/mail/data/models/correspondent/Correspondent.kt

+1-2
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,11 @@ interface Correspondent : Parcelable {
4444

4545
return@runCatching "$first$last".uppercase()
4646
}.getOrElse { exception ->
47-
Sentry.withScope { scope ->
47+
Sentry.captureException(exception) { scope ->
4848
scope.setExtra("email", email)
4949
scope.setExtra("name size", name.count().toString())
5050
scope.setExtra("name is blank", name.isBlank().toString())
5151
scope.setExtra("characters not letters", name.filterNot(Char::isLetter))
52-
Sentry.captureException(exception)
5352
}
5453

5554
return@getOrElse ""

app/src/main/java/com/infomaniak/mail/data/models/message/Message.kt

+20-22
Original file line numberDiff line numberDiff line change
@@ -165,30 +165,28 @@ class Message : RealmObject {
165165
runCatching {
166166
_folders.single { _folders.count() == 1 || it.id != SEARCH_FOLDER_ID }
167167
}.getOrElse {
168-
Sentry.withScope { scope ->
169-
scope.level = SentryLevel.ERROR
170-
scope.setExtra("messageUid", uid)
171-
scope.setExtra("email", AccountUtils.currentMailboxEmail.toString())
172-
val sentryMessage = if (_folders.isEmpty()) {
173-
"Message has 0 parent folders, it should not be possible"
174-
} else {
175-
scope.setExtra("folders", "${_folders.map { "role:[${it.role?.name}] (id:[${it.id}])" }}")
176-
scope.setExtra("foldersCount", "${_folders.count()}")
177-
val allFoldersAreSearch = _folders.all { it.id == SEARCH_FOLDER_ID }
178-
val allFoldersAreTheSame = _folders.all { it.id == _folders.firstOrNull()?.id }
179-
when {
180-
allFoldersAreSearch -> {
181-
"Message has multiple times the Search folder as parent, it should not be possible"
182-
}
183-
allFoldersAreTheSame -> {
184-
"Message has multiple times the same parent folder, it should not be possible"
185-
}
186-
else -> {
187-
"Message has multiple parent folders, it should not be possible"
188-
}
168+
val sentryMessage = if (_folders.isEmpty()) {
169+
"Message has 0 parent folders, it should not be possible"
170+
} else {
171+
val allFoldersAreSearch = _folders.all { it.id == SEARCH_FOLDER_ID }
172+
val allFoldersAreTheSame = _folders.all { it.id == _folders.firstOrNull()?.id }
173+
when {
174+
allFoldersAreSearch -> {
175+
"Message has multiple times the Search folder as parent, it should not be possible"
176+
}
177+
allFoldersAreTheSame -> {
178+
"Message has multiple times the same parent folder, it should not be possible"
179+
}
180+
else -> {
181+
"Message has multiple parent folders, it should not be possible"
189182
}
190183
}
191-
Sentry.captureMessage(sentryMessage)
184+
}
185+
Sentry.captureMessage(sentryMessage, SentryLevel.ERROR) { scope ->
186+
scope.setExtra("messageUid", uid)
187+
scope.setExtra("email", AccountUtils.currentMailboxEmail.toString())
188+
scope.setExtra("folders", "${_folders.map { "role:[${it.role?.name}] (id:[${it.id}])" }}")
189+
scope.setExtra("foldersCount", "${_folders.count()}")
192190
}
193191
_folders.first()
194192
}

app/src/main/java/com/infomaniak/mail/data/models/thread/Thread.kt

+5-4
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,17 @@ class Thread : RealmObject {
9999
runCatching {
100100
_folders.single()
101101
}.getOrElse { exception ->
102-
Sentry.withScope { scope ->
102+
Sentry.captureMessage(
103+
"Thread has several or 0 parent folders, it should not be possible",
104+
SentryLevel.ERROR,
105+
) { scope ->
103106
scope.setTag("foldersId", _folders.joinToString { it.id })
104107
scope.setTag("foldersCount", "${_folders.count()}")
105108
scope.setExtra("foldersCount", "${_folders.count()}")
106109
scope.setExtra("folders", "${_folders.map { "name:[${it.role?.name}] (id:[${it.id}])" }}")
107110
scope.setExtra("threadUid", uid)
108111
scope.setExtra("email", AccountUtils.currentMailboxEmail.toString())
109112
scope.setExtra("exception message", exception.message.toString())
110-
Sentry.captureMessage("Thread has several or 0 parent folders, it should not be possible", SentryLevel.ERROR)
111113
}
112114
_folders.first()
113115
}
@@ -233,7 +235,7 @@ class Thread : RealmObject {
233235
recipients.firstOrNull() to message.bimi
234236

235237
}.getOrElse { throwable ->
236-
Sentry.withScope { scope ->
238+
Sentry.captureException(throwable) { scope ->
237239
scope.setExtra("thread.folder.role", folder.role?.name.toString())
238240
scope.setExtra("thread.folder.id", folder.id)
239241
scope.setExtra("thread.folderId", folderId)
@@ -242,7 +244,6 @@ class Thread : RealmObject {
242244
scope.setExtra("thread.duplicates.count", "${duplicates.count()}")
243245
scope.setExtra("thread.isFromSearch", "$isFromSearch")
244246
scope.setExtra("thread.hasDrafts", "$hasDrafts")
245-
Sentry.captureException(throwable)
246247
}
247248

248249
null to null

app/src/main/java/com/infomaniak/mail/receivers/NotificationActionsReceiver.kt

+1-2
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,9 @@ class NotificationActionsReceiver : BroadcastReceiver() {
167167
updateFolders(folders = listOf(message.folder, destinationFolder), mailbox, realm)
168168
} else {
169169
executeUndoAction(payload)
170-
Sentry.withScope { scope ->
170+
Sentry.captureException(first().getApiException()) { scope ->
171171
scope.setTag("reason", "Notif action fail because of API call")
172172
scope.setExtra("destination folder role", folderRole.name)
173-
Sentry.captureException(first().getApiException())
174173
}
175174
}
176175
}

app/src/main/java/com/infomaniak/mail/ui/BaseActivity.kt

+1-2
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,8 @@ open class BaseActivity : AppCompatActivity() {
4141
if (AccountUtils.currentUser == null) runBlocking {
4242
AccountUtils.requestCurrentUser()
4343
if (AccountUtils.currentUser == null) {
44-
Sentry.withScope { scope ->
44+
Sentry.captureMessage("BaseActivity> the current user is null") { scope ->
4545
scope.setExtra("has been fixed", "false")
46-
Sentry.captureMessage("BaseActivity> the current user is null")
4746
}
4847
}
4948
}

app/src/main/java/com/infomaniak/mail/ui/MainActivity.kt

+1-2
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ import com.infomaniak.mail.utils.extensions.isUserAlreadySynchronized
7575
import com.infomaniak.mail.workers.DraftsActionsWorker
7676
import dagger.hilt.android.AndroidEntryPoint
7777
import io.sentry.Sentry
78-
import io.sentry.SentryLevel
7978
import kotlinx.coroutines.Dispatchers
8079
import kotlinx.coroutines.delay
8180
import kotlinx.coroutines.launch
@@ -497,7 +496,7 @@ class MainActivity : BaseActivity() {
497496
isVisible = true
498497
playAnimation()
499498
}
500-
Sentry.captureMessage("Easter egg XMas has been triggered! Woohoo!", SentryLevel.INFO)
499+
Sentry.captureMessage("Easter egg XMas has been triggered! Woohoo!")
501500
trackEasterEggEvent("xmas${Date().year()}")
502501
}
503502
}

app/src/main/java/com/infomaniak/mail/ui/main/folder/ThreadListFragment.kt

+4-5
Original file line numberDiff line numberDiff line change
@@ -723,14 +723,13 @@ class ThreadListFragment : TwoPaneFragment(), SwipeRefreshLayout.OnRefreshListen
723723

724724
if (!areThereThreads && !isFilterEnabled && !isBooting && !isWaitingFirstThreads) {
725725
val currentFolder = mainViewModel.currentFolder.value
726-
Sentry.withScope { scope ->
726+
Sentry.captureMessage(
727+
"Should display threads is true but there are no threads to display",
728+
SentryLevel.WARNING,
729+
) { scope ->
727730
scope.setExtra("cursor", "$currentFolderCursor")
728731
scope.setExtra("folderRole", currentFolder?.role?.name.toString())
729732
scope.setExtra("folderThreadsCount", "${currentFolder?.threads?.count()}")
730-
Sentry.captureMessage(
731-
"Should display threads is true but there are no threads to display",
732-
SentryLevel.WARNING,
733-
)
734733
}
735734
}
736735
}

app/src/main/java/com/infomaniak/mail/ui/main/thread/ThreadAdapter.kt

+2-4
Original file line numberDiff line numberDiff line change
@@ -303,16 +303,14 @@ class ThreadAdapter(
303303
}
304304

305305
private fun isThemeTheSameForMessageUid(messageUid: String) = threadAdapterState.isThemeTheSameMap[messageUid] ?: run {
306-
Sentry.withScope { scope ->
306+
// TODO: Find the cause. The bug probably affects other parts of the code that do not crash
307+
Sentry.captureMessage("Missing message uid inside isThemeTheSameMap", SentryLevel.ERROR) { scope ->
307308
val mapStringRepresentation = threadAdapterState.isThemeTheSameMap
308309
.map { (key, value) -> "($key -> $value)" }
309310
.joinToString(prefix = "[", postfix = "]")
310311

311312
scope.setExtra("isThemeTheSameMap", mapStringRepresentation)
312313
scope.setExtra("looking for messageUid", messageUid)
313-
314-
// TODO: Find the cause. The bug probably affects other parts of the code that do not crash
315-
Sentry.captureMessage("Missing message uid inside isThemeTheSameMap", SentryLevel.ERROR)
316314
}
317315

318316
true

app/src/main/java/com/infomaniak/mail/ui/main/thread/ThreadFragment.kt

+4-5
Original file line numberDiff line numberDiff line change
@@ -630,15 +630,14 @@ class ThreadFragment : Fragment() {
630630
} else {
631631
val targetChild = binding.messagesList.getChildAt(indexToScroll)
632632
if (targetChild == null) {
633-
Sentry.withScope { scope ->
633+
Sentry.captureMessage(
634+
"Target child for scroll in ThreadFragment is null. Fallback to scrolling to bottom",
635+
SentryLevel.ERROR,
636+
) { scope ->
634637
scope.setExtra("indexToScroll", indexToScroll.toString())
635638
scope.setExtra("messageCount", threadAdapter.items.count().toString())
636639
scope.setExtra("isExpandedMap", threadState.isExpandedMap.toString())
637640
scope.setExtra("isLastMessageDraft", (threadAdapter.items.lastOrNull() as Message?)?.isDraft.toString())
638-
Sentry.captureMessage(
639-
"Target child for scroll in ThreadFragment is null. Fallback to scrolling to bottom",
640-
SentryLevel.ERROR,
641-
)
642641
}
643642
getBottomY()
644643
} else {

app/src/main/java/com/infomaniak/mail/ui/main/thread/ThreadViewModel.kt

+6-9
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,7 @@ import com.infomaniak.mail.data.models.thread.Thread
3636
import com.infomaniak.mail.di.IoDispatcher
3737
import com.infomaniak.mail.ui.main.thread.ThreadAdapter.SuperCollapsedBlock
3838
import com.infomaniak.mail.utils.*
39-
import com.infomaniak.mail.utils.extensions.MergedContactDictionary
40-
import com.infomaniak.mail.utils.extensions.appContext
41-
import com.infomaniak.mail.utils.extensions.atLeastOneSucceeded
42-
import com.infomaniak.mail.utils.extensions.getUids
43-
import com.infomaniak.mail.utils.extensions.indexOfFirstOrNull
39+
import com.infomaniak.mail.utils.extensions.*
4440
import dagger.hilt.android.lifecycle.HiltViewModel
4541
import io.realm.kotlin.MutableRealm
4642
import io.realm.kotlin.query.RealmResults
@@ -381,11 +377,10 @@ class ThreadViewModel @Inject constructor(
381377
if (apiResponse.isSuccess()) {
382378
updateCalendarEvent(message, apiResponse.data!!)
383379
} else {
384-
Sentry.withScope { scope ->
380+
Sentry.captureMessage("Failed loading calendar event") { scope ->
385381
scope.setExtra("ics attachment mimeType", icsAttachment.mimeType)
386382
scope.setExtra("ics attachment size", icsAttachment.size.toString())
387383
scope.setExtra("error code", apiResponse.error?.code.toString())
388-
Sentry.captureMessage("Failed loading calendar event")
389384
}
390385
}
391386
}
@@ -395,13 +390,15 @@ class ThreadViewModel @Inject constructor(
395390
localMessage?.let {
396391
it.latestCalendarEventResponse = calendarEventResponse
397392
} ?: run {
398-
Sentry.withScope { scope ->
393+
Sentry.captureMessage(
394+
"Cannot find message by uid for fetched calendar event inside Realm",
395+
SentryLevel.ERROR,
396+
) { scope ->
399397
scope.setExtra("message.uid", message.uid)
400398
val hasUserStoredEvent = calendarEventResponse.hasAssociatedInfomaniakCalendarEvent()
401399
scope.setExtra("event has userStoredEvent", hasUserStoredEvent.toString())
402400
scope.setExtra("event is canceled", calendarEventResponse.isCanceled.toString())
403401
scope.setExtra("event has attachmentEvent", calendarEventResponse.hasAttachmentEvent().toString())
404-
Sentry.captureMessage("Cannot find message by uid for fetched calendar event inside Realm", SentryLevel.ERROR)
405402
}
406403
}
407404
}

app/src/main/java/com/infomaniak/mail/ui/main/thread/calendar/CalendarEventBannerView.kt

+1-2
Original file line numberDiff line numberDiff line change
@@ -219,11 +219,10 @@ class CalendarEventBannerView @JvmOverloads constructor(
219219
private fun displayOrganizer(attendees: List<Attendee>) = with(binding) {
220220
val organizers = attendees.filter(Attendee::isOrganizer)
221221
if (organizers.count() > 1) {
222-
Sentry.withScope { scope ->
222+
Sentry.captureMessage("Found more than one organizer for an event") { scope ->
223223
scope.setExtra("amount of organizer", organizers.count().toString())
224224
scope.setExtra("have same email", organizers.all { it.email == organizers[0].email }.toString())
225225
scope.setExtra("have same name", organizers.all { it.name == organizers[0].name }.toString())
226-
Sentry.captureMessage("Found more than one organizer for an event")
227226
}
228227
}
229228

app/src/main/java/com/infomaniak/mail/ui/main/user/AccountFragment.kt

+1-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ import com.infomaniak.mail.utils.extensions.bindAlertToViewLifecycle
5353
import com.infomaniak.mail.utils.extensions.setSystemBarsColors
5454
import dagger.hilt.android.AndroidEntryPoint
5555
import io.sentry.Sentry
56-
import io.sentry.SentryLevel
5756
import kotlinx.coroutines.CoroutineDispatcher
5857
import kotlinx.coroutines.launch
5958
import java.util.Calendar
@@ -144,7 +143,7 @@ class AccountFragment : Fragment(), MailboxListFragment {
144143

145144
if ((month == Calendar.OCTOBER && day >= 26) || (month == Calendar.NOVEMBER && day <= 1)) {
146145
binding.easterEggHalloween.isVisible = true
147-
Sentry.captureMessage("Easter egg Halloween has been triggered! Woohoo!", SentryLevel.INFO)
146+
Sentry.captureMessage("Easter egg Halloween has been triggered! Woohoo!")
148147
trackEasterEggEvent("halloween${Date().year()}")
149148
}
150149
}

app/src/main/java/com/infomaniak/mail/utils/ConfettiUtils.kt

+1-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import com.infomaniak.mail.MatomoMail.trackEasterEggEvent
2424
import com.infomaniak.mail.R
2525
import com.infomaniak.mail.utils.extensions.isInPortrait
2626
import io.sentry.Sentry
27-
import io.sentry.SentryLevel
2827
import com.infomaniak.lib.confetti.R as RConfetti
2928

3029
object ConfettiUtils {
@@ -62,9 +61,8 @@ object ConfettiUtils {
6261
matomoValue: String,
6362
) = with(container.context) {
6463

65-
Sentry.withScope { scope ->
64+
Sentry.captureMessage("Easter egg Confetti has been triggered! Woohoo!") { scope ->
6665
scope.setTag("from", matomoValue)
67-
Sentry.captureMessage("Easter egg Confetti has been triggered! Woohoo!", SentryLevel.INFO)
6866
}
6967

7068
trackEasterEggEvent("confetti$matomoValue")

app/src/main/java/com/infomaniak/mail/utils/LocalStorageUtils.kt

+12-12
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,10 @@ object LocalStorageUtils {
121121

122122
return@use getFileToUpload(context, uri, snackbarManager, attachmentsUploadDir, hashedFileName, inputStream)
123123
} ?: run {
124-
Sentry.withScope { scope ->
124+
Sentry.captureMessage("failed to access uri") { scope ->
125125
scope.setExtra("uri is absolute", uri.isAbsolute.toString())
126126
scope.setExtra("uri is relative", uri.isRelative.toString())
127127
scope.setExtra("uri is opaque", uri.isOpaque.toString())
128-
Sentry.captureMessage("failed to access uri")
129128
}
130129
null
131130
}
@@ -144,20 +143,21 @@ object LocalStorageUtils {
144143
FileOutputStream(file).use(inputStream::copyTo)
145144
true
146145
}.getOrElse {
147-
Sentry.withScope { scope ->
146+
147+
val exception = it.message
148+
?.let { message -> AttachmentMissingFileException(message.replace(file.path, HIDDEN_FILE_NAME)) }
149+
?: it
150+
exception.stackTrace = it.stackTrace
151+
152+
Sentry.captureException(exception) { scope ->
148153
scope.setExtra("uri", uri.toString().replace(file.path, HIDDEN_FILE_NAME))
149-
val exception = it.message
150-
?.let { message -> AttachmentMissingFileException(message.replace(file.path, HIDDEN_FILE_NAME)) }
151-
?: it
154+
}
152155

153-
exception.stackTrace = it.stackTrace
154-
Sentry.captureException(exception)
156+
val isNameLong = exception.message?.contains(NAME_TOO_LONG_EXCEPTION) == true
157+
val snackbarMessageId = if (isNameLong) R.string.errorFileNameTooLong else RCore.string.errorFileNotFound
155158

156-
val isNameLong = exception.message?.contains(NAME_TOO_LONG_EXCEPTION) == true
157-
val snackbarMessageId = if (isNameLong) R.string.errorFileNameTooLong else RCore.string.errorFileNotFound
159+
snackbarManager.postValue(context.getString(snackbarMessageId))
158160

159-
snackbarManager.postValue(context.getString(snackbarMessageId))
160-
}
161161
false
162162
}
163163

app/src/main/java/com/infomaniak/mail/utils/MessageBodyUtils.kt

+1-2
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,9 @@ object MessageBodyUtils {
7878
if (quotes.isEmpty() || quotes.all { it.isBlank() }) SplitBody(bodyContent) else SplitBody(content, bodyContent)
7979
},
8080
onTimeout = {
81-
Sentry.withScope { scope ->
81+
Sentry.captureMessage("Timeout reached while displaying a Message's body", SentryLevel.WARNING) { scope ->
8282
scope.setExtra("body size", "${bodyContent.toByteArray().size} bytes")
8383
scope.setExtra("email", AccountUtils.currentMailboxEmail.toString())
84-
Sentry.captureMessage("Timeout reached while displaying a Message's body", SentryLevel.WARNING)
8584
}
8685
},
8786
)

0 commit comments

Comments
 (0)