Improve error message when uploading attachment to Pixelfed fails

See #425
This commit is contained in:
Shadowfacts 2023-09-16 13:56:46 -04:00
parent 3f15a453bd
commit 2be1ee19de
2 changed files with 15 additions and 1 deletions

View File

@ -113,6 +113,7 @@ public class Client {
var urlRequest = URLRequest(url: url, timeoutInterval: timeoutInterval)
urlRequest.httpMethod = request.method.name
urlRequest.httpBody = request.body.data
urlRequest.setValue("application/json", forHTTPHeaderField: "Accept")
for (name, value) in request.headers {
urlRequest.setValue(value, forHTTPHeaderField: name)
}

View File

@ -11,7 +11,20 @@ import Foundation
struct MastodonError: Decodable, CustomStringConvertible {
var description: String
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
if let error = try container.decodeIfPresent(String.self, forKey: .error) {
self.description = error
} else if let message = try container.decodeIfPresent(String.self, forKey: .message) {
self.description = message
} else {
throw DecodingError.keyNotFound(CodingKeys.error, .init(codingPath: container.codingPath, debugDescription: "Missing error or message key"))
}
}
private enum CodingKeys: String, CodingKey {
case description = "error"
case error
// used by pixelfed
case message
}
}