Skip to content

Commit

Permalink
comment payload decoding error fix
Browse files Browse the repository at this point in the history
  • Loading branch information
87kangsw committed Apr 13, 2024
1 parent 02b6c11 commit 1a6d366
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 0 deletions.
16 changes: 16 additions & 0 deletions GitTime/Sources/Models/Me.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,20 @@ struct Me: ModelType {
case followers
case following
}

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

id = try container.decode(Int.self, forKey: .id)
name = try container.decode(String.self, forKey: .name)
additionalName = try container.decodeIfPresent(String.self, forKey: .additionalName) ?? ""
profileURL = try container.decodeIfPresent(String.self, forKey: .profileURL) ?? ""
url = try container.decode(String.self, forKey: .url)
bio = try? container.decode(String.self, forKey: .bio)
location = try? container.decode(String.self, forKey: .location)
publicRepos = try? container.decode(Int.self, forKey: .publicRepos)
privateRepos = try? container.decode(Int.self, forKey: .privateRepos)
followers = try? container.decode(Int.self, forKey: .followers)
following = try? container.decode(Int.self, forKey: .following)
}
}
5 changes: 5 additions & 0 deletions GitTime/Sources/Services/ActivityService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ class ActivityService: ActivityServiceType {
func fetchActivities(userName: String, page: Int) -> Observable<[Event]> {
return self.networking.request(.activityEvent(userName: userName, page: page))
.map([Event].self)
.do(onError: { error in
if let decodingErrorInfo = error.decodingErrorInfo {
log.error(decodingErrorInfo)
}
})
.asObservable()
}

Expand Down
79 changes: 79 additions & 0 deletions GitTime/Sources/Utils/GitTimeDecodingError.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
//
// GitTimeDecodingError.swift
// GitTime
//
// Created by Kanz on 4/13/24.
//

// https://gist.github.com/nunogoncalves/4852077f4e576872f72b70d9e79942f3

import Foundation

import Moya

extension Error {
var decodingErrorInfo: DecodingErrorInfomation? {
if let moyaError = self as? MoyaError {
if case .objectMapping(let error, let response) = moyaError {
if let decodeError = error as? DecodingError,
let path = response.request?.url?.getPath() {
return DecodingErrorInfomation(
path: path,
error: GitTimeDecodingError(with: decodeError)
)
}
}
}
return nil
}
}

struct DecodingErrorInfomation {
let path: String
let error: GitTimeDecodingError
}

enum GitTimeDecodingError: CustomStringConvertible {
case dataCorrupted(_ message: String)
case keyNotFound(_ message: String)
case typeMismatch(_ message: String)
case valueNotFound(_ message: String)
case any(_ error: Error)

init(with error: DecodingError) {
switch error {
case let .dataCorrupted(context):
let debugDescription = (context.underlyingError as NSError?)?.userInfo["NSDebugDescription"] ?? ""
self = .dataCorrupted("Data corrupted. \(context.debugDescription) \(debugDescription)")
case let .keyNotFound(key, context):
self = .keyNotFound("Key not found. Expected -> \(key.stringValue) <- at: \(context.prettyPath())")
case let .typeMismatch(_, context):
self = .typeMismatch("Type mismatch. \(context.debugDescription), at: \(context.prettyPath())")
case let .valueNotFound(_, context):
self = .valueNotFound("Value not found. -> \(context.prettyPath()) <- \(context.debugDescription)")
default:
self = .any(error)
}
}
var description: String {
switch self {
case let .dataCorrupted(message), let .keyNotFound(message), let .typeMismatch(message), let .valueNotFound(message):
return message
case let .any(error):
return error.localizedDescription
}
}
}

extension DecodingError.Context {
func prettyPath(separatedBy separator: String = ".") -> String {
codingPath.map { $0.stringValue }.joined(separator: ".")
}
}

extension URL {
func getPath() -> String? {
let components = URLComponents(url: self, resolvingAgainstBaseURL: false)
return components?.path
}
}

0 comments on commit 1a6d366

Please sign in to comment.