Improve error message when uploading attachment to Pixelfed fails
See #425
This commit is contained in:
parent
3f15a453bd
commit
2be1ee19de
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue