Skip to content

Commit

Permalink
Enhance error handling in ProductImageStatus
Browse files Browse the repository at this point in the history
- Add new cases for `errorDomain`, `errorCode`, and `errorUserInfo` in `ProductImageStatus.swift`.
- Modify decoding logic to handle detailed error information.
- Update encoding logic to include `errorDomain`, `errorCode`, and `errorUserInfo`.
- Change `productID` to be always encoded in `uploading` and `uploadFailure` cases.
  • Loading branch information
pmusolino committed Feb 25, 2025
1 parent aafaa19 commit 0c362dc
Showing 1 changed file with 15 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,15 @@ public enum ProductImageStatus: Equatable, Codable {
case error
case siteID
case productID
case errorDomain
case errorCode
case errorUserInfo
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let typeString = try container.decode(String.self, forKey: .type)

switch typeString {
case "uploading":
let asset = try container.decode(ProductImageAssetType.self, forKey: .asset)
Expand All @@ -43,11 +47,12 @@ public enum ProductImageStatus: Equatable, Codable {
self = .remote(image: image, siteID: sID, productID: pID)
case "uploadFailure":
let asset = try container.decode(ProductImageAssetType.self, forKey: .asset)
let errorMessage = try container.decode(String.self, forKey: .error)
let error = NSError(domain: "ProductImageStatus", code: 0, userInfo: [NSLocalizedDescriptionKey: errorMessage])
let sID = try container.decode(Int64.self, forKey: .siteID)
let pID = try container.decode(ProductOrVariationID.self, forKey: .productID)
self = .uploadFailure(asset: asset, error: error, siteID: sID, productID: pID)
let domain = try container.decode(String.self, forKey: .errorDomain)
let code = try container.decode(Int.self, forKey: .errorCode)
let userInfo = try container.decode([String: String]?.self, forKey: .errorUserInfo)
self = .uploadFailure(asset: asset, error: NSError(domain: domain, code: code, userInfo: userInfo), siteID: sID, productID: pID)
default:
throw DecodingError.dataCorruptedError(forKey: .type,
in: container,
Expand All @@ -62,7 +67,7 @@ public enum ProductImageStatus: Equatable, Codable {
try container.encode("uploading", forKey: .type)
try container.encode(asset, forKey: .asset)
try container.encode(siteID, forKey: .siteID)
try container.encodeIfPresent(productID, forKey: .productID)
try container.encode(productID, forKey: .productID)
case .remote(let image, let siteID, let productID):
try container.encode("remote", forKey: .type)
try container.encode(image, forKey: .image)
Expand All @@ -71,10 +76,13 @@ public enum ProductImageStatus: Equatable, Codable {
case .uploadFailure(let asset, let error, let siteID, let productID):
try container.encode("uploadFailure", forKey: .type)
try container.encode(asset, forKey: .asset)
let errorMessage = (error as NSError).localizedDescription
try container.encode(errorMessage, forKey: .error)
try container.encode(siteID, forKey: .siteID)
try container.encodeIfPresent(productID, forKey: .productID)
try container.encode(productID, forKey: .productID)

let errorMessage = error as NSError
try container.encode(errorMessage.domain, forKey: .errorDomain)
try container.encode(errorMessage.code, forKey: .errorCode)
try container.encode(errorMessage.userInfo as? [String: String], forKey: .errorUserInfo)
}
}

Expand Down

0 comments on commit 0c362dc

Please sign in to comment.