Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Deserialize SentryException #4791

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions Sentry.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@
628094742D39584C00B3F18B /* SentryUserCodable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 628094732D39584700B3F18B /* SentryUserCodable.swift */; };
6281C5722D3E4F12009D0978 /* DecodeArbitraryData.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6281C5712D3E4F06009D0978 /* DecodeArbitraryData.swift */; };
6281C5742D3E50DF009D0978 /* ArbitraryDataTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6281C5732D3E50D8009D0978 /* ArbitraryDataTests.swift */; };
628308612D50ADAC00EAEF77 /* SentryRequestCodable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 628308602D50ADAC00EAEF77 /* SentryRequestCodable.swift */; };
6283085F2D50AA8C00EAEF77 /* SentryMessage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6283085E2D50AA8C00EAEF77 /* SentryMessage.swift */; };
628308612D50ADAC00EAEF77 /* SentryRequestCodable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 628308602D50ADAC00EAEF77 /* SentryRequestCodable.swift */; };
62862B1C2B1DDBC8009B16E3 /* SentryDelayedFrame.h in Headers */ = {isa = PBXBuildFile; fileRef = 62862B1B2B1DDBC8009B16E3 /* SentryDelayedFrame.h */; };
62862B1E2B1DDC35009B16E3 /* SentryDelayedFrame.m in Sources */ = {isa = PBXBuildFile; fileRef = 62862B1D2B1DDC35009B16E3 /* SentryDelayedFrame.m */; };
62872B5F2BA1B7F300A4FA7D /* NSLock.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62872B5E2BA1B7F300A4FA7D /* NSLock.swift */; };
Expand Down Expand Up @@ -141,6 +141,7 @@
62D6B2A72CCA354B004DDBF1 /* SentryUncaughtNSExceptionsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62D6B2A62CCA354B004DDBF1 /* SentryUncaughtNSExceptionsTests.swift */; };
62E081A929ED4260000F69FC /* SentryBreadcrumbDelegate.h in Headers */ = {isa = PBXBuildFile; fileRef = 62E081A829ED4260000F69FC /* SentryBreadcrumbDelegate.h */; };
62E081AB29ED4322000F69FC /* SentryBreadcrumbTestDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62E081AA29ED4322000F69FC /* SentryBreadcrumbTestDelegate.swift */; };
62E300942D5202890037AA3F /* SentryExceptionCodable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62E300932D5202830037AA3F /* SentryExceptionCodable.swift */; };
62EF86A12C626D39004E058B /* SentryANRTrackerV2Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 621AE74E2C626CF70012E730 /* SentryANRTrackerV2Tests.swift */; };
62F05D2B2C0DB1F100916E3F /* SentryLogTestHelper.m in Sources */ = {isa = PBXBuildFile; fileRef = 62F05D2A2C0DB1F100916E3F /* SentryLogTestHelper.m */; };
62F226B729A37C120038080D /* SentryBooleanSerialization.m in Sources */ = {isa = PBXBuildFile; fileRef = 62F226B629A37C120038080D /* SentryBooleanSerialization.m */; };
Expand Down Expand Up @@ -1160,8 +1161,8 @@
628094732D39584700B3F18B /* SentryUserCodable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryUserCodable.swift; sourceTree = "<group>"; };
6281C5712D3E4F06009D0978 /* DecodeArbitraryData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DecodeArbitraryData.swift; sourceTree = "<group>"; };
6281C5732D3E50D8009D0978 /* ArbitraryDataTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ArbitraryDataTests.swift; sourceTree = "<group>"; };
628308602D50ADAC00EAEF77 /* SentryRequestCodable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryRequestCodable.swift; sourceTree = "<group>"; };
6283085E2D50AA8C00EAEF77 /* SentryMessage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryMessage.swift; sourceTree = "<group>"; };
628308602D50ADAC00EAEF77 /* SentryRequestCodable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryRequestCodable.swift; sourceTree = "<group>"; };
62862B1B2B1DDBC8009B16E3 /* SentryDelayedFrame.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SentryDelayedFrame.h; path = include/SentryDelayedFrame.h; sourceTree = "<group>"; };
62862B1D2B1DDC35009B16E3 /* SentryDelayedFrame.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SentryDelayedFrame.m; sourceTree = "<group>"; };
62872B5E2BA1B7F300A4FA7D /* NSLock.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NSLock.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1193,6 +1194,7 @@
62D6B2A62CCA354B004DDBF1 /* SentryUncaughtNSExceptionsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryUncaughtNSExceptionsTests.swift; sourceTree = "<group>"; };
62E081A829ED4260000F69FC /* SentryBreadcrumbDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SentryBreadcrumbDelegate.h; path = include/SentryBreadcrumbDelegate.h; sourceTree = "<group>"; };
62E081AA29ED4322000F69FC /* SentryBreadcrumbTestDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryBreadcrumbTestDelegate.swift; sourceTree = "<group>"; };
62E300932D5202830037AA3F /* SentryExceptionCodable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryExceptionCodable.swift; sourceTree = "<group>"; };
62F05D292C0DB1C800916E3F /* SentryLogTestHelper.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SentryLogTestHelper.h; sourceTree = "<group>"; };
62F05D2A2C0DB1F100916E3F /* SentryLogTestHelper.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SentryLogTestHelper.m; sourceTree = "<group>"; };
62F226B629A37C120038080D /* SentryBooleanSerialization.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SentryBooleanSerialization.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2225,6 +2227,7 @@
628308602D50ADAC00EAEF77 /* SentryRequestCodable.swift */,
620078732D38F0DF0022CB67 /* SentryCodable.swift */,
6283085E2D50AA8C00EAEF77 /* SentryMessage.swift */,
62E300932D5202830037AA3F /* SentryExceptionCodable.swift */,
);
path = Codable;
sourceTree = "<group>";
Expand Down Expand Up @@ -5086,6 +5089,7 @@
D48724DB2D352597005DE483 /* SentryTraceOrigin.swift in Sources */,
63FE714520DA4C1100CDBAE8 /* SentryCrashObjC.c in Sources */,
63FE710520DA4C1000CDBAE8 /* SentryAsyncSafeLog.c in Sources */,
62E300942D5202890037AA3F /* SentryExceptionCodable.swift in Sources */,
0A2D8D5B289815C0008720F6 /* SentryBaseIntegration.m in Sources */,
639FCF991EBC7B9700778193 /* SentryEvent.m in Sources */,
D8BC28CA2BFF68CA0054DA4D /* NumberExtensions.swift in Sources */,
Expand Down
28 changes: 28 additions & 0 deletions Sources/Swift/Protocol/Codable/SentryExceptionCodable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
@_implementationOnly import _SentryPrivate
import Foundation

extension Exception: Decodable {

private enum CodingKeys: String, CodingKey {
case value
case type
case mechanism
case module
case threadId = "thread_id"
case stacktrace
}

required convenience public init(from decoder: any Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)

let value = try container.decode(String.self, forKey: .value)
let type = try container.decode(String.self, forKey: .type)

self.init(value: value, type: type)

self.mechanism = try container.decodeIfPresent(Mechanism.self, forKey: .mechanism)
self.module = try container.decodeIfPresent(String.self, forKey: .module)
self.threadId = try container.decodeIfPresent(NSNumberDecodableWrapper.self, forKey: .threadId)?.value
self.stacktrace = try container.decodeIfPresent(SentryStacktrace.self, forKey: .stacktrace)
}
}
46 changes: 46 additions & 0 deletions Tests/SentryTests/Protocol/SentryExceptionTests.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@testable import Sentry
import XCTest

class SentryExceptionTests: XCTestCase {
Expand All @@ -24,4 +25,49 @@ class SentryExceptionTests: XCTestCase {
let stacktrace = try XCTUnwrap(actual["stacktrace"] as? [String: Any])
XCTAssertEqual(TestData.stacktrace.registers, stacktrace["registers"] as? [String: String])
}

func testDecode_WithAllProperties() throws {
// Arrange
let exception = TestData.exception
let actual = exception.serialize()
let data = try XCTUnwrap(SentrySerialization.data(withJSONObject: actual))

// Act
let decoded = try XCTUnwrap(decodeFromJSONData(jsonData: data) as Exception?)

// Assert
XCTAssertEqual(exception.type, decoded.type)
XCTAssertEqual(exception.value, decoded.value)
XCTAssertEqual(exception.module, decoded.module)
XCTAssertEqual(exception.threadId, decoded.threadId)

let decodedMechanism = try XCTUnwrap(decoded.mechanism)
let expectedMechanism = try XCTUnwrap(exception.mechanism)
XCTAssertEqual(expectedMechanism.desc, decodedMechanism.desc)
XCTAssertEqual(expectedMechanism.handled, decodedMechanism.handled)
XCTAssertEqual(expectedMechanism.type, decodedMechanism.type)

let decodedStacktrace = try XCTUnwrap(decoded.stacktrace)
let expectedStacktrace = try XCTUnwrap(exception.stacktrace)
XCTAssertEqual(expectedStacktrace.frames.count, decodedStacktrace.frames.count)
XCTAssertEqual(expectedStacktrace.registers, decodedStacktrace.registers)
}

func testDecode_WithAllPropertiesNil() throws {
// Arrange
let exception = Exception(value: "value", type: "type")
let actual = exception.serialize()
let data = try XCTUnwrap(SentrySerialization.data(withJSONObject: actual))

// Act
let decoded = try XCTUnwrap(decodeFromJSONData(jsonData: data) as Exception?)

// Assert
XCTAssertEqual(exception.type, decoded.type)
XCTAssertEqual(exception.value, decoded.value)
XCTAssertNil(decoded.mechanism)
XCTAssertNil(decoded.module)
XCTAssertNil(decoded.threadId)
XCTAssertNil(decoded.stacktrace)
}
}
Loading