Skip to content

Commit

Permalink
fix: FormatException when reading BatteryLevel (#3485)
Browse files Browse the repository at this point in the history
  • Loading branch information
bitsandfoxes authored Jul 22, 2024
1 parent c2a459c commit e747e42
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

### Fixes

- On mobile devices, the SDK no longer throws a `FormatException` when trying to report native events ([#3485](https://github.com/getsentry/sentry-dotnet/pull/3485))
- Race condition in `SentryMessageHandler` ([#3477](https://github.com/getsentry/sentry-dotnet/pull/3477))
- Decrease runtime diagnostics circular buffer when profiling, reducing memory usage ([#3491](https://github.com/getsentry/sentry-dotnet/pull/3491))

Expand Down
13 changes: 12 additions & 1 deletion src/Sentry/Protocol/Device.cs
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,18 @@ public static Device FromJson(JsonElement json)
var model = json.GetPropertyOrNull("model")?.GetString();
var modelId = json.GetPropertyOrNull("model_id")?.GetString();
var architecture = json.GetPropertyOrNull("arch")?.GetString();
var batteryLevel = json.GetPropertyOrNull("battery_level")?.GetInt16();

// TODO: For next major: Remove this and change batteryLevel from short to float
// The Java and Cocoa SDK report the battery as `float`
// Cocoa https://github.com/getsentry/sentry-cocoa/blob/e773cad622b86735f1673368414009475e4119fd/Sources/Sentry/include/SentryUIDeviceWrapper.h#L18
// Java https://github.com/getsentry/sentry-java/blob/25f1ca4e1636a801c17c1662f0145f888550bce8/sentry/src/main/java/io/sentry/protocol/Device.java#L231-L233
short? batteryLevel = null;
var batteryProperty = json.GetPropertyOrNull("battery_level");
if (batteryProperty.HasValue)
{
batteryLevel = (short)batteryProperty.Value.GetDouble();
}

var isCharging = json.GetPropertyOrNull("charging")?.GetBoolean();
var isOnline = json.GetPropertyOrNull("online")?.GetBoolean();
var orientation = json.GetPropertyOrNull("orientation")?.GetString()?.ParseEnum<DeviceOrientation>();
Expand Down

0 comments on commit e747e42

Please sign in to comment.