Skip to content

Commit

Permalink
Added LoggingURLEncodedFormDecoder
Browse files Browse the repository at this point in the history
  • Loading branch information
Philip Niedertscheider committed Oct 9, 2021
1 parent 00df1ad commit cec6f2f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Sources/Postie/Decoder/ResponseDecoding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ internal struct ResponseDecoding: Decoder {
}

private func createFormURLEncodedDecoder() -> URLEncodedFormDecoder {
let decoder = URLEncodedFormDecoder()
let decoder = LoggingURLEncodedFormDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
return decoder
}
Expand Down
1 change: 1 addition & 0 deletions Sources/Postie/Utils/LoggingJSONDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ class LoggingJSONDecoder: JSONDecoder {
}
}
}

15 changes: 15 additions & 0 deletions Sources/Postie/Utils/LoggingURLEncodedFormDecoder.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Foundation
import os.log
import URLEncodedFormCoding

class LoggingURLEncodedFormDecoder: URLEncodedFormDecoder {

override func decode<T>(_ type: T.Type, from data: Data) throws -> T where T: Decodable {
do {
return try super.decode(type, from: data)
} catch {
os_log("Failed to decode form-url-encoded data: %@\nReason: %@\nDetails: %@", type: .error, String(data: data, encoding: .utf8) ?? "nil", error.localizedDescription, String(describing: error))
throw error
}
}
}
4 changes: 2 additions & 2 deletions Sources/URLEncodedFormCoding/URLEncodedFormDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Foundation
import Combine
import PostieUtils

public final class URLEncodedFormDecoder: TopLevelDecoder {
open class URLEncodedFormDecoder: TopLevelDecoder {

public enum DecodingError: Error {
case invalidData
Expand All @@ -17,7 +17,7 @@ public final class URLEncodedFormDecoder: TopLevelDecoder {

public init() {}

public func decode<D>(_ decodable: D.Type, from data: Data) throws -> D where D: Decodable {
open func decode<D>(_ decodable: D.Type, from data: Data) throws -> D where D: Decodable {
let urlEncodedFormData = String(data: data, encoding: .utf8) ?? ""
let context = URLEncodedFormDataContext()

Expand Down

0 comments on commit cec6f2f

Please sign in to comment.