Skip to content

Commit

Permalink
Set User and Breadcrumb from Map (#1454)
Browse files Browse the repository at this point in the history
Co-authored-by: Manoel Aranda Neto <[email protected]>
  • Loading branch information
denrase and marandaneto authored May 16, 2023
1 parent 0a82a1e commit 3ba8d7e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 155 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -287,86 +287,22 @@ class SentryFlutterPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
}

private fun setUser(user: Map<String, Any?>?, result: Result) {
if (user == null) {
if (user != null) {
val options = HubAdapter.getInstance().options
val userInstance = User.fromMap(user, options)
Sentry.setUser(userInstance)
} else {
Sentry.setUser(null)
result.success("")
return
}

val userInstance = User()
val userData = mutableMapOf<String, String>()
val unknown = mutableMapOf<String, Any>()

(user["email"] as? String)?.let { userInstance.email = it }
(user["id"] as? String)?.let { userInstance.id = it }
(user["username"] as? String)?.let { userInstance.username = it }
(user["ip_address"] as? String)?.let { userInstance.ipAddress = it }
(user["segment"] as? String)?.let { userInstance.segment = it }
(user["name"] as? String)?.let { userInstance.name = it }
(user["geo"] as? Map<String, Any?>)?.let {
val geo = Geo()
geo.city = it["city"] as? String
geo.countryCode = it["country_code"] as? String
geo.region = it["region"] as? String
userInstance.geo = geo
}

(user["extras"] as? Map<String, Any?>)?.let { extras ->
for ((key, value) in extras.entries) {
if (value != null) {
userData[key] = value.toString()
}
}
}
(user["data"] as? Map<String, Any?>)?.let { data ->
for ((key, value) in data.entries) {
if (value != null) {
// data has precedence over extras
userData[key] = value.toString()
}
}
}

if (userData.isNotEmpty()) {
userInstance.data = userData
}
if (unknown.isNotEmpty()) {
userInstance.unknown = unknown
}

Sentry.setUser(userInstance)

result.success("")
}

private fun addBreadcrumb(breadcrumb: Map<String, Any?>?, result: Result) {
if (breadcrumb == null) {
result.success("")
return
if (breadcrumb != null) {
val options = HubAdapter.getInstance().options
val breadcrumbInstance = Breadcrumb.fromMap(breadcrumb, options)
Sentry.addBreadcrumb(breadcrumbInstance)
}
val breadcrumbInstance = Breadcrumb()

(breadcrumb["message"] as? String)?.let { breadcrumbInstance.message = it }
(breadcrumb["type"] as? String)?.let { breadcrumbInstance.type = it }
(breadcrumb["category"] as? String)?.let { breadcrumbInstance.category = it }
(breadcrumb["level"] as? String)?.let {
breadcrumbInstance.level = when (it) {
"fatal" -> SentryLevel.FATAL
"warning" -> SentryLevel.WARNING
"info" -> SentryLevel.INFO
"debug" -> SentryLevel.DEBUG
"error" -> SentryLevel.ERROR
else -> SentryLevel.INFO
}
}
(breadcrumb["data"] as? Map<String, Any?>)?.let { data ->
for ((key, value) in data.entries) {
breadcrumbInstance.data[key] = value
}
}

Sentry.addBreadcrumb(breadcrumbInstance)

result.success("")
}

Expand Down
86 changes: 4 additions & 82 deletions flutter/ios/Classes/SentryFlutterPluginApple.swift
Original file line number Diff line number Diff line change
Expand Up @@ -566,99 +566,21 @@ public class SentryFlutterPluginApple: NSObject, FlutterPlugin {
}
}

// swiftlint:disable:next cyclomatic_complexity
private func setUser(user: [String: Any?]?, result: @escaping FlutterResult) {
if let user = user {
let userInstance = User()

if let email = user["email"] as? String {
userInstance.email = email
}
if let id = user["id"] as? String {
userInstance.userId = id
}
if let username = user["username"] as? String {
userInstance.username = username
}
if let ipAddress = user["ip_address"] as? String {
userInstance.ipAddress = ipAddress
}
if let segment = user["segment"] as? String {
userInstance.segment = segment
}
if let extras = user["extras"] as? [String: Any] {
userInstance.data = extras
}
if let data = user["data"] as? [String: Any] {
if let oldData = userInstance.data {
userInstance.data = oldData.reduce(into: data) { (first, second) in first[second.0] = second.1 }
} else {
userInstance.data = data
}
}
if let name = user["name"] as? String {
userInstance.name = name
}
if let geoData = user["geo"] as? [String: Any] {
let geo = Geo()
geo.city = geoData["city"] as? String
geo.countryCode = geoData["country_code"] as? String
geo.region = geoData["region"] as? String
userInstance.geo = geo
}

let userInstance = PrivateSentrySDKOnly.user(with: user)
SentrySDK.setUser(userInstance)
} else {
SentrySDK.setUser(nil)
}
result("")
}

// swiftlint:disable:next cyclomatic_complexity
private func addBreadcrumb(breadcrumb: [String: Any?]?, result: @escaping FlutterResult) {
guard let breadcrumb = breadcrumb else {
result("")
return
if let breadcrumb = breadcrumb {
let breadcrumbInstance = PrivateSentrySDKOnly.breadcrumb(with: breadcrumb)
SentrySDK.addBreadcrumb(breadcrumbInstance)
}

let breadcrumbInstance = Breadcrumb()

if let message = breadcrumb["message"] as? String {
breadcrumbInstance.message = message
}
if let type = breadcrumb["type"] as? String {
breadcrumbInstance.type = type
}
if let category = breadcrumb["category"] as? String {
breadcrumbInstance.category = category
}
if let level = breadcrumb["level"] as? String {
switch level {
case "fatal":
breadcrumbInstance.level = SentryLevel.fatal
case "warning":
breadcrumbInstance.level = SentryLevel.warning
case "info":
breadcrumbInstance.level = SentryLevel.info
case "debug":
breadcrumbInstance.level = SentryLevel.debug
case "error":
breadcrumbInstance.level = SentryLevel.error
default:
breadcrumbInstance.level = SentryLevel.error
}
}
if let data = breadcrumb["data"] as? [String: Any] {
breadcrumbInstance.data = data
}

if let timestampValue = breadcrumb["timestamp"] as? String,
let timestamp = dateFrom(iso8601String: timestampValue) {
breadcrumbInstance.timestamp = timestamp
}

SentrySDK.addBreadcrumb(breadcrumbInstance)

result("")
}

Expand Down

0 comments on commit 3ba8d7e

Please sign in to comment.