//
//  MastodonError.swift
//  Pachyderm
//
//  Created by Shadowfacts on 9/8/18.
//  Copyright © 2018 Shadowfacts. All rights reserved.
//

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 error
        // used by pixelfed
        case message
    }
}
