Skip to content

Commit 3ba8d7e

Browse files
denrasemarandaneto
andauthored
Set User and Breadcrumb from Map (#1454)
Co-authored-by: Manoel Aranda Neto <[email protected]>
1 parent 0a82a1e commit 3ba8d7e

File tree

2 files changed

+13
-155
lines changed

2 files changed

+13
-155
lines changed

flutter/android/src/main/kotlin/io/sentry/flutter/SentryFlutterPlugin.kt

+9-73
Original file line numberDiff line numberDiff line change
@@ -287,86 +287,22 @@ class SentryFlutterPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
287287
}
288288

289289
private fun setUser(user: Map<String, Any?>?, result: Result) {
290-
if (user == null) {
290+
if (user != null) {
291+
val options = HubAdapter.getInstance().options
292+
val userInstance = User.fromMap(user, options)
293+
Sentry.setUser(userInstance)
294+
} else {
291295
Sentry.setUser(null)
292-
result.success("")
293-
return
294296
}
295-
296-
val userInstance = User()
297-
val userData = mutableMapOf<String, String>()
298-
val unknown = mutableMapOf<String, Any>()
299-
300-
(user["email"] as? String)?.let { userInstance.email = it }
301-
(user["id"] as? String)?.let { userInstance.id = it }
302-
(user["username"] as? String)?.let { userInstance.username = it }
303-
(user["ip_address"] as? String)?.let { userInstance.ipAddress = it }
304-
(user["segment"] as? String)?.let { userInstance.segment = it }
305-
(user["name"] as? String)?.let { userInstance.name = it }
306-
(user["geo"] as? Map<String, Any?>)?.let {
307-
val geo = Geo()
308-
geo.city = it["city"] as? String
309-
geo.countryCode = it["country_code"] as? String
310-
geo.region = it["region"] as? String
311-
userInstance.geo = geo
312-
}
313-
314-
(user["extras"] as? Map<String, Any?>)?.let { extras ->
315-
for ((key, value) in extras.entries) {
316-
if (value != null) {
317-
userData[key] = value.toString()
318-
}
319-
}
320-
}
321-
(user["data"] as? Map<String, Any?>)?.let { data ->
322-
for ((key, value) in data.entries) {
323-
if (value != null) {
324-
// data has precedence over extras
325-
userData[key] = value.toString()
326-
}
327-
}
328-
}
329-
330-
if (userData.isNotEmpty()) {
331-
userInstance.data = userData
332-
}
333-
if (unknown.isNotEmpty()) {
334-
userInstance.unknown = unknown
335-
}
336-
337-
Sentry.setUser(userInstance)
338-
339297
result.success("")
340298
}
341299

342300
private fun addBreadcrumb(breadcrumb: Map<String, Any?>?, result: Result) {
343-
if (breadcrumb == null) {
344-
result.success("")
345-
return
301+
if (breadcrumb != null) {
302+
val options = HubAdapter.getInstance().options
303+
val breadcrumbInstance = Breadcrumb.fromMap(breadcrumb, options)
304+
Sentry.addBreadcrumb(breadcrumbInstance)
346305
}
347-
val breadcrumbInstance = Breadcrumb()
348-
349-
(breadcrumb["message"] as? String)?.let { breadcrumbInstance.message = it }
350-
(breadcrumb["type"] as? String)?.let { breadcrumbInstance.type = it }
351-
(breadcrumb["category"] as? String)?.let { breadcrumbInstance.category = it }
352-
(breadcrumb["level"] as? String)?.let {
353-
breadcrumbInstance.level = when (it) {
354-
"fatal" -> SentryLevel.FATAL
355-
"warning" -> SentryLevel.WARNING
356-
"info" -> SentryLevel.INFO
357-
"debug" -> SentryLevel.DEBUG
358-
"error" -> SentryLevel.ERROR
359-
else -> SentryLevel.INFO
360-
}
361-
}
362-
(breadcrumb["data"] as? Map<String, Any?>)?.let { data ->
363-
for ((key, value) in data.entries) {
364-
breadcrumbInstance.data[key] = value
365-
}
366-
}
367-
368-
Sentry.addBreadcrumb(breadcrumbInstance)
369-
370306
result.success("")
371307
}
372308

flutter/ios/Classes/SentryFlutterPluginApple.swift

+4-82
Original file line numberDiff line numberDiff line change
@@ -566,99 +566,21 @@ public class SentryFlutterPluginApple: NSObject, FlutterPlugin {
566566
}
567567
}
568568

569-
// swiftlint:disable:next cyclomatic_complexity
570569
private func setUser(user: [String: Any?]?, result: @escaping FlutterResult) {
571570
if let user = user {
572-
let userInstance = User()
573-
574-
if let email = user["email"] as? String {
575-
userInstance.email = email
576-
}
577-
if let id = user["id"] as? String {
578-
userInstance.userId = id
579-
}
580-
if let username = user["username"] as? String {
581-
userInstance.username = username
582-
}
583-
if let ipAddress = user["ip_address"] as? String {
584-
userInstance.ipAddress = ipAddress
585-
}
586-
if let segment = user["segment"] as? String {
587-
userInstance.segment = segment
588-
}
589-
if let extras = user["extras"] as? [String: Any] {
590-
userInstance.data = extras
591-
}
592-
if let data = user["data"] as? [String: Any] {
593-
if let oldData = userInstance.data {
594-
userInstance.data = oldData.reduce(into: data) { (first, second) in first[second.0] = second.1 }
595-
} else {
596-
userInstance.data = data
597-
}
598-
}
599-
if let name = user["name"] as? String {
600-
userInstance.name = name
601-
}
602-
if let geoData = user["geo"] as? [String: Any] {
603-
let geo = Geo()
604-
geo.city = geoData["city"] as? String
605-
geo.countryCode = geoData["country_code"] as? String
606-
geo.region = geoData["region"] as? String
607-
userInstance.geo = geo
608-
}
609-
571+
let userInstance = PrivateSentrySDKOnly.user(with: user)
610572
SentrySDK.setUser(userInstance)
611573
} else {
612574
SentrySDK.setUser(nil)
613575
}
614576
result("")
615577
}
616578

617-
// swiftlint:disable:next cyclomatic_complexity
618579
private func addBreadcrumb(breadcrumb: [String: Any?]?, result: @escaping FlutterResult) {
619-
guard let breadcrumb = breadcrumb else {
620-
result("")
621-
return
580+
if let breadcrumb = breadcrumb {
581+
let breadcrumbInstance = PrivateSentrySDKOnly.breadcrumb(with: breadcrumb)
582+
SentrySDK.addBreadcrumb(breadcrumbInstance)
622583
}
623-
624-
let breadcrumbInstance = Breadcrumb()
625-
626-
if let message = breadcrumb["message"] as? String {
627-
breadcrumbInstance.message = message
628-
}
629-
if let type = breadcrumb["type"] as? String {
630-
breadcrumbInstance.type = type
631-
}
632-
if let category = breadcrumb["category"] as? String {
633-
breadcrumbInstance.category = category
634-
}
635-
if let level = breadcrumb["level"] as? String {
636-
switch level {
637-
case "fatal":
638-
breadcrumbInstance.level = SentryLevel.fatal
639-
case "warning":
640-
breadcrumbInstance.level = SentryLevel.warning
641-
case "info":
642-
breadcrumbInstance.level = SentryLevel.info
643-
case "debug":
644-
breadcrumbInstance.level = SentryLevel.debug
645-
case "error":
646-
breadcrumbInstance.level = SentryLevel.error
647-
default:
648-
breadcrumbInstance.level = SentryLevel.error
649-
}
650-
}
651-
if let data = breadcrumb["data"] as? [String: Any] {
652-
breadcrumbInstance.data = data
653-
}
654-
655-
if let timestampValue = breadcrumb["timestamp"] as? String,
656-
let timestamp = dateFrom(iso8601String: timestampValue) {
657-
breadcrumbInstance.timestamp = timestamp
658-
}
659-
660-
SentrySDK.addBreadcrumb(breadcrumbInstance)
661-
662584
result("")
663585
}
664586

0 commit comments

Comments
 (0)