Skip to content

Commit

Permalink
Add logging when decoding of optionals throws
Browse files Browse the repository at this point in the history
  • Loading branch information
Hopsaheysa committed Jul 26, 2024
1 parent 7a227fa commit abf9b32
Show file tree
Hide file tree
Showing 9 changed files with 359 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,17 @@ public class WMTOperationListResponse<T: WMTUserOperation>: WPNResponseArray<T>
public required init(from decoder: Decoder) throws {

let c = try decoder.container(keyedBy: Keys.self)
currentTimestamp = try? c.decode(Date.self, forKey: Keys.currentTimestamp)

if c.contains(.currentTimestamp) {
do {
currentTimestamp = try c.decode(Date.self, forKey: .currentTimestamp)
} catch {
D.error("Failed to decode \(Keys.currentTimestamp) - \(error), setting to null")
currentTimestamp = nil
}
} else {
currentTimestamp = nil
}

try super.init(from: decoder)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,17 @@ public class WMTOperationAttributeImage: WMTOperationAttribute {
let c = try decoder.container(keyedBy: Keys.self)

self.thumbnailUrl = try c.decode(String.self, forKey: .thumbnailUrl)
self.originalUrl = try? c.decode(String.self, forKey: .originalUrl)

if c.contains(.originalUrl) {
do {
originalUrl = try c.decode(String.self, forKey: .originalUrl)
} catch {
D.error("Failed to decode \(Keys.originalUrl) - \(error), setting to null")
originalUrl = nil
}
} else {
originalUrl = nil
}

try super.init(from: decoder)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,25 @@ public class WMTReviewPostApprovalScreenPayload: WMTPostApprovalScreenPayload {
// MARK: Internals

public required init(from decoder: Decoder) throws {

let c = try decoder.container(keyedBy: Keys.self)
attributes = (try? c.decode([WMTOperationAttributeDecodable].self, forKey: .attributes).map {
$0.attrObject }) ?? []

var operationAttributes: [WMTOperationAttribute] = []
do {
var container = try c.nestedUnkeyedContainer(forKey: .attributes)
// If decoding fails log it and continue decoding until the end of container
while container.isAtEnd == false {
do {
let attribute = try WMTOperationAttributeDecodable(from: container.superDecoder())
operationAttributes.append(attribute.attrObject)
} catch {
D.error("Error decoding WMTOperationFormData attribute: \(error)")
}
}
} catch {
D.error("No attributes in WMTOperationFormData: \(error)")
}
attributes = operationAttributes

super.init()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,28 @@ public class WMTPreApprovalScreen: Codable {
type = ScreenType(rawValue: t) ?? .unknown
heading = try c.decode(String.self, forKey: .heading)
message = try c.decode(String.self, forKey: .message)
items = try? c.decode([String].self, forKey: .items)
approvalType = try? c.decode(WMTPreApprovalScreenConfirmAction.self, forKey: .approvalType)

if c.contains(.items) {
do {
items = try c.decode([String].self, forKey: .items)
} catch {
D.error("Failed to decode \(Keys.items) - \(error), setting to null")
items = nil
}
} else {
items = nil
}

if c.contains(.approvalType) {
do {
approvalType = try c.decode(WMTPreApprovalScreenConfirmAction.self, forKey: .approvalType)
} catch {
D.error("Failed to decode \(Keys.approvalType) - \(error), setting to null")
approvalType = nil
}
} else {
approvalType = nil
}
}

public init(type: ScreenType, heading: String, message: String, items: [String]? = nil, approvalType: WMTPreApprovalScreenConfirmAction?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,17 @@ public class WMTOperationFormData: Codable {
let c = try decoder.container(keyedBy: Keys.self)
title = try c.decode(String.self, forKey: .title)
message = try c.decode(String.self, forKey: .message)
resultTexts = try? c.decode(WMTResultTexts.self, forKey: .resultTexts)

if c.contains(.resultTexts) {
do {
resultTexts = try c.decode(WMTResultTexts.self, forKey: .resultTexts)
} catch {
D.error("Failed to decode \(Keys.resultTexts) - \(error), setting to null")
resultTexts = nil
}
} else {
resultTexts = nil
}

var operationAttributes: [WMTOperationAttribute] = []
do {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,61 @@ open class WMTOperationUIData: Codable {

public required init(from decoder: Decoder) throws {
let c = try decoder.container(keyedBy: Keys.self)
flipButtons = try? c.decode(Bool.self, forKey: .flipButtons)
blockApprovalOnCall = try? c.decode(Bool.self, forKey: .blockApprovalOnCall)
preApprovalScreen = try? c.decode(WMTPreApprovalScreen.self, forKey: .preApprovalScreen)
postApprovalScreen = try? c.decode(WMTPostApprovalScreenDecodable.self, forKey: .postApprovalScreen).postApprovalObject
templates = try? c.decode(WMTTemplates.self, forKey: .templates)

if c.contains(.flipButtons) {
do {
flipButtons = try c.decode(Bool.self, forKey: .flipButtons)
} catch {
D.error("Failed to decode \(Keys.flipButtons) - \(error), setting to null")
flipButtons = nil
}
} else {
flipButtons = nil
}

if c.contains(.blockApprovalOnCall) {
do {
blockApprovalOnCall = try c.decode(Bool.self, forKey: .blockApprovalOnCall)
} catch {
D.error("Failed to decode \(Keys.blockApprovalOnCall) - \(error), setting to null")
blockApprovalOnCall = nil
}
} else {
blockApprovalOnCall = nil
}

if c.contains(.preApprovalScreen) {
do {
preApprovalScreen = try c.decode(WMTPreApprovalScreen.self, forKey: .preApprovalScreen)
} catch {
D.error("Failed to decode \(Keys.preApprovalScreen) - \(error), setting to null")
preApprovalScreen = nil
}
} else {
preApprovalScreen = nil
}

if c.contains(.postApprovalScreen) {
do {
postApprovalScreen = try c.decode(WMTPostApprovalScreenDecodable.self, forKey: .postApprovalScreen).postApprovalObject
} catch {
D.error("Failed to decode \(Keys.postApprovalScreen) - \(error), setting to null")
postApprovalScreen = nil
}
} else {
postApprovalScreen = nil
}

if c.contains(.templates) {
do {
templates = try c.decode(WMTTemplates.self, forKey: .templates)
} catch {
D.error("Failed to decode \(Keys.templates) - \(error), setting to null")
templates = nil
}
} else {
templates = nil
}
}

public init(flipButtons: Bool?, blockApprovalOnCall: Bool?, preApprovalScreen: WMTPreApprovalScreen?, postApprovalScreen: WMTPostApprovalScreen?, templates: WMTTemplates? = nil) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,39 @@ public class WMTResultTexts: Codable {

public required init(from decoder: Decoder) throws {
let c = try decoder.container(keyedBy: Keys.self)
success = try? c.decode(String.self, forKey: .success)
failure = try? c.decode(String.self, forKey: .failure)
reject = try? c.decode(String.self, forKey: .reject)

if c.contains(.success) {
do {
success = try c.decode(String.self, forKey: .success)
} catch {
D.error("Failed to decode \(Keys.success) - \(error), setting to null")
success = nil
}
} else {
success = nil
}

if c.contains(.failure) {
do {
failure = try c.decode(String.self, forKey: .failure)
} catch {
D.error("Failed to decode \(Keys.failure) - \(error), setting to null")
failure = nil
}
} else {
failure = nil
}

if c.contains(.reject) {
do {
reject = try c.decode(String.self, forKey: .reject)
} catch {
D.error("Failed to decode \(Keys.reject) - \(error), setting to null")
reject = nil
}
} else {
reject = nil
}
}

public init(success: String?, failure: String?, reject: String?) {
Expand Down
Loading

0 comments on commit abf9b32

Please sign in to comment.