Skip to content

Commit de216fa

Browse files
fix: FormatException when reading ProcessorFrequency (#3541)
--------- Co-authored-by: James Crosswell <[email protected]>
1 parent bddec9f commit de216fa

File tree

3 files changed

+153
-8
lines changed

3 files changed

+153
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
### Fixes
2323

24+
- On mobile devices, the SDK no longer throws a `FormatException` for `ProcessorFrequency` when trying to report native events ([#3541](https://github.com/getsentry/sentry-dotnet/pull/3541))
2425
- Unfinished spans are now correctly stored and retrieved by the CachingTransport ([#3533](https://github.com/getsentry/sentry-dotnet/pull/3533))
2526

2627
### Dependencies

src/Sentry/Protocol/Device.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -427,16 +427,13 @@ public static Device FromJson(JsonElement json)
427427
var modelId = json.GetPropertyOrNull("model_id")?.GetString();
428428
var architecture = json.GetPropertyOrNull("arch")?.GetString();
429429

430-
// TODO: For next major: Remove this and change batteryLevel from short to float
430+
// TODO: For next major: Remove this and change BatteryLevel from short to float
431431
// The Java and Cocoa SDK report the battery as `float`
432432
// Cocoa https://github.com/getsentry/sentry-cocoa/blob/e773cad622b86735f1673368414009475e4119fd/Sources/Sentry/include/SentryUIDeviceWrapper.h#L18
433433
// Java https://github.com/getsentry/sentry-java/blob/25f1ca4e1636a801c17c1662f0145f888550bce8/sentry/src/main/java/io/sentry/protocol/Device.java#L231-L233
434-
short? batteryLevel = null;
435-
var batteryProperty = json.GetPropertyOrNull("battery_level");
436-
if (batteryProperty.HasValue)
437-
{
438-
batteryLevel = (short)batteryProperty.Value.GetDouble();
439-
}
434+
var batteryLevel = json.GetPropertyOrNull("battery_level")?.TryGetDouble(out var level) is true
435+
? (short)level
436+
: (short?)null;
440437

441438
var isCharging = json.GetPropertyOrNull("charging")?.GetBoolean();
442439
var isOnline = json.GetPropertyOrNull("online")?.GetBoolean();
@@ -456,7 +453,14 @@ public static Device FromJson(JsonElement json)
456453
var bootTime = json.GetPropertyOrNull("boot_time")?.GetDateTimeOffset();
457454
var processorCount = json.GetPropertyOrNull("processor_count")?.GetInt32();
458455
var cpuDescription = json.GetPropertyOrNull("cpu_description")?.GetString();
459-
var processorFrequency = json.GetPropertyOrNull("processor_frequency")?.GetInt32();
456+
457+
// TODO: For next major: Remove this and change ProcessorFrequency from int to float
458+
// The Java SDK reports the processorFrequency as `double`
459+
// Java https://github.com/getsentry/sentry-java/blob/9762f09afa51944b40a9b77e116a55e54636e6c5/sentry/src/main/java/io/sentry/protocol/Device.java#L130
460+
var processorFrequency = json.GetPropertyOrNull("processor_frequency")?.TryGetDouble(out var frequency) is true
461+
? (int)frequency
462+
: (int?)null;
463+
460464
var deviceType = json.GetPropertyOrNull("device_type")?.GetString();
461465
var batteryStatus = json.GetPropertyOrNull("battery_status")?.GetString();
462466
var deviceUniqueIdentifier = json.GetPropertyOrNull("device_unique_identifier")?.GetString();
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
namespace Sentry.Tests.Protocol;
2+
3+
public class DeviceTests
4+
{
5+
[Fact]
6+
public void Clone_CopyValues()
7+
{
8+
// Arrange
9+
var sut = TestDevice();
10+
11+
// Act
12+
var result = sut.Clone();
13+
14+
// Assert
15+
AssertAreEqual(result, sut);
16+
}
17+
18+
[Fact]
19+
public void WriteTo_FromJson_Symmetric()
20+
{
21+
// Arrange
22+
var sut = TestDevice();
23+
24+
var json = sut.ToJsonString();
25+
using var document = JsonDocument.Parse(json);
26+
var jsonElement = document.RootElement;
27+
28+
// Act
29+
var result = Device.FromJson(jsonElement);
30+
31+
// Assert
32+
AssertAreEqual(result, sut);
33+
}
34+
35+
[Fact]
36+
public void FromJson_JavaTypes_CastCorrectly()
37+
{
38+
// Arrange
39+
var sut = TestDevice();
40+
41+
var json = sut.ToJsonString()
42+
// In the Java SDK, the Processor Frequency is stored as a Double
43+
.Replace(@"""processor_frequency"": 12", @"""processor_frequency"": 12.0");
44+
45+
using var document = JsonDocument.Parse(json);
46+
var jsonElement = document.RootElement;
47+
48+
// Act
49+
var result = Device.FromJson(jsonElement);
50+
51+
// Assert
52+
AssertAreEqual(result, sut);
53+
}
54+
55+
private static Device TestDevice()
56+
{
57+
return new Device
58+
{
59+
Name = "TestName",
60+
Manufacturer = "TestManufacturer",
61+
Brand = "TestBrand",
62+
Architecture = "TestArchitecture",
63+
BatteryLevel = 1,
64+
IsCharging = true,
65+
IsOnline = true,
66+
BootTime = new DateTimeOffset(2001, 06, 15, 12, 30, 0, TimeSpan.Zero),
67+
ExternalFreeStorage = 2,
68+
ExternalStorageSize = 3,
69+
ScreenResolution = "800x600",
70+
ScreenDensity = 1.2f,
71+
ScreenDpi = 4,
72+
Family = "TestFamily",
73+
FreeMemory = 5,
74+
FreeStorage = 6,
75+
MemorySize = 7,
76+
Model = "TestModel",
77+
ModelId = "TestModelId",
78+
Orientation = DeviceOrientation.Landscape,
79+
Simulator = true,
80+
StorageSize = 8,
81+
Timezone = TimeZoneInfo.Utc,
82+
UsableMemory = 9,
83+
LowMemory = true,
84+
ProcessorCount = 11,
85+
CpuDescription = "TestCpuDescription",
86+
ProcessorFrequency = 12,
87+
SupportsVibration = true,
88+
DeviceType = "TestDeviceType",
89+
BatteryStatus = "TestBatteryStatus",
90+
DeviceUniqueIdentifier = "TestDeviceUniqueIdentifier",
91+
SupportsAccelerometer = true,
92+
SupportsGyroscope = true,
93+
SupportsAudio = true,
94+
SupportsLocationService = true
95+
};
96+
}
97+
98+
private static void AssertAreEqual(Device actual, Device expected)
99+
{
100+
using (new AssertionScope())
101+
{
102+
actual.Name.Should().Be(expected.Name);
103+
actual.Manufacturer.Should().Be(expected.Manufacturer);
104+
actual.Brand.Should().Be(expected.Brand);
105+
actual.Architecture.Should().Be(expected.Architecture);
106+
actual.BatteryLevel.Should().Be(expected.BatteryLevel);
107+
actual.IsCharging.Should().Be(expected.IsCharging);
108+
actual.IsOnline.Should().Be(expected.IsOnline);
109+
actual.BootTime.Should().Be(expected.BootTime);
110+
actual.ExternalFreeStorage.Should().Be(expected.ExternalFreeStorage);
111+
actual.ExternalStorageSize.Should().Be(expected.ExternalStorageSize);
112+
actual.ScreenResolution.Should().Be(expected.ScreenResolution);
113+
actual.ScreenDensity.Should().Be(expected.ScreenDensity);
114+
actual.ScreenDpi.Should().Be(expected.ScreenDpi);
115+
actual.Family.Should().Be(expected.Family);
116+
actual.FreeMemory.Should().Be(expected.FreeMemory);
117+
actual.FreeStorage.Should().Be(expected.FreeStorage);
118+
actual.MemorySize.Should().Be(expected.MemorySize);
119+
actual.Model.Should().Be(expected.Model);
120+
actual.ModelId.Should().Be(expected.ModelId);
121+
actual.Orientation.Should().Be(expected.Orientation);
122+
actual.Simulator.Should().Be(expected.Simulator);
123+
actual.StorageSize.Should().Be(expected.StorageSize);
124+
actual.Timezone.Should().Be(expected.Timezone);
125+
actual.UsableMemory.Should().Be(expected.UsableMemory);
126+
actual.LowMemory.Should().Be(expected.LowMemory);
127+
actual.ProcessorCount.Should().Be(expected.ProcessorCount);
128+
actual.CpuDescription.Should().Be(expected.CpuDescription);
129+
actual.ProcessorFrequency.Should().Be(expected.ProcessorFrequency);
130+
actual.SupportsVibration.Should().Be(expected.SupportsVibration);
131+
actual.DeviceType.Should().Be(expected.DeviceType);
132+
actual.BatteryStatus.Should().Be(expected.BatteryStatus);
133+
actual.DeviceUniqueIdentifier.Should().Be(expected.DeviceUniqueIdentifier);
134+
actual.SupportsAccelerometer.Should().Be(expected.SupportsAccelerometer);
135+
actual.SupportsGyroscope.Should().Be(expected.SupportsGyroscope);
136+
actual.SupportsAudio.Should().Be(expected.SupportsAudio);
137+
actual.SupportsLocationService.Should().Be(expected.SupportsLocationService);
138+
}
139+
}
140+
}

0 commit comments

Comments
 (0)