More detailed error message for decoding hashtag urls

This commit is contained in:
Shadowfacts 2022-05-17 10:18:28 -04:00
parent 65e75afa8b
commit 0168c05259
1 changed files with 11 additions and 4 deletions

View File

@ -26,10 +26,17 @@ public class Hashtag: Codable {
let container = try decoder.container(keyedBy: CodingKeys.self) let container = try decoder.container(keyedBy: CodingKeys.self)
self.name = try container.decode(String.self, forKey: .name) self.name = try container.decode(String.self, forKey: .name)
// pixelfed (possibly others) don't fully escape special characters in the hashtag url // pixelfed (possibly others) don't fully escape special characters in the hashtag url
if let url = URL(try container.decode(WebURL.self, forKey: .url)) { do {
self.url = url let webURL = try container.decode(WebURL.self, forKey: .url)
} else { if let url = URL(webURL) {
throw DecodingError.dataCorruptedError(forKey: .url, in: container, debugDescription: "unable to convert WebURL to URL") self.url = url
} else {
let s = try? container.decode(String.self, forKey: .url)
throw DecodingError.dataCorruptedError(forKey: .url, in: container, debugDescription: "unable to convert WebURL \(s?.debugDescription ?? "nil") to URL")
}
} catch {
let s = try? container.decode(String.self, forKey: .url)
throw DecodingError.dataCorruptedError(forKey: .url, in: container, debugDescription: "unable to decode WebURL from \(s?.debugDescription ?? "nil")")
} }
self.history = try container.decodeIfPresent([History].self, forKey: .history) self.history = try container.decodeIfPresent([History].self, forKey: .history)
} }