//
//  APIError.swift
//

import Foundation

enum APIError: LocalizedError {
    case invalidURL
    case requestFailed(Int)
    case decodingFailed(String)
    case networkError(Error)
    case unauthorized
    case serverError(String, code: String? = nil)

    /// 语义错误码（如 AUTH_USER_DISABLED），用于 AppSession 状态判断
    var errorCode: String? {
        if case .serverError(_, let code) = self { return code }
        return nil
    }

    var errorDescription: String? {
        switch self {
        case .invalidURL: return "无效的请求地址"
        case .requestFailed(let code): return "请求失败 (\(code))"
        case .decodingFailed(let msg): return "数据解析失败: \(msg)"
        case .networkError(let e): return e.localizedDescription
        case .unauthorized: return "未授权，请重新登录"
        case .serverError(let msg, _): return msg
        }
    }
}
