gpt4 book ai didi

json - 如果第一次解码失败使用 Combine 和 Swift 解码另一个响应

转载 作者:行者123 更新时间:2023-12-01 21:26:11 24 4
gpt4 key购买 nike

我有以下模型:

struct Response: Decodable {
let message: String
}

struct ErrorResponse: Decodable {
let errorMessage: String
}

enum APIError: Error {
case network(code: Int, description: String)
case decoding(description: String)
case api(description: String)
}

我正在尝试获取一个 url 并使用此流程解析 JSON 响应:

func fetch(url: URL) -> AnyPublisher<Response, APIError> {
URLSession.shared.dataTaskPublisher(for: URLRequest(url: url))

// #1 URLRequest fails, throw APIError.network
.mapError { .network(code: $0.code.rawValue, description: $0.localizedDescription) }

// #2 try to decode data as a `Response`
.tryMap { JSONDecoder().decode(Response.self, from: $0.data) }

// #3 if decoding fails, decode as an `ErrorResponse`
// and throw `APIError.api(description: errorResponse.errorMessage)`

// #4 if both fail, throw APIError.decoding

// #5 return
.eraseToAnyPublisher()
}

#3 有问题:如何解码 tryMap 部分之后的原始数据?

似乎我可以访问的唯一值是来自 tryMap 的错误,但我需要原始数据来解码 ErrorRepsonse

注意:不幸的是,错误响应带有 200 状态,区分它们的唯一方法是解码它们。

最佳答案

您可以使用 flatMap 并在其中处理解码:

URLSession.shared.dataTaskPublisher(for: URLRequest(url: url))
// #1 URLRequest fails, throw APIError.network
.mapError {
APIError.network(code: $0.code.rawValue, description: $0.localizedDescription)
}

.flatMap { data -> AnyPublisher<Response, APIError> in
// #2 try to decode data as a `Response`
if let response = try? JSONDecoder().decode(Response.self, from: data) {
return Just(response).setFailureType(to: APIError.self)
.eraseToAnyPublisher()
}

do {
// #3 if decoding fails, decode as an `ErrorResponse`
let error = try decoder.decode(ErrorResponse.self, from: data)

// and throw `APIError.api(description: errorResponse.errorMessage)`
return Fail(error: APIError.api(description: errorResponse.errorMessage))
.eraseToAnyPublisher()
} catch {
// #4 if both fail, throw APIError.decoding
return Fail(error: APIError.decoding(description: error.localizedDescription))
.eraseToAnyPublisher()
}
}

编辑

如果您想以“纯粹的”合并方式执行此操作,那么您仍然希望使用 flatMap 来访问原始数据并回避原始可能的网络错误,并且然后使用tryCatch处理失败路径。

请注意,第 4 步位于第 3 步的两个部分之间:

URLSession.shared.dataTaskPublisher(for: URLRequest(url: url))
// #1 URLRequest fails, throw APIError.network
.mapError {
APIError.network(code: $0.code.rawValue, description: $0.localizedDescription)
}
.flatMap { v in
Just(v)

// #2 try to decode data as a `Response`
.decode(type: Response.self, decoder: JSONDecoder())

// #3 if decoding fails,
.tryCatch { _ in
Just(v)
// #3.1 ... decode as an `ErrorResponse`
.decode(type: ErrorResponse.self, decoder: JSONDecoder())

// #4 if both fail, throw APIError.decoding
.mapError { _ in APIError.decoding(description: "error decoding") }

// #3.2 ... and throw `APIError.api
.tryMap { throw APIError.api(description: $0.errorMessage) }
}

// force unwrap is not terrible here, since you know
// that `tryCatch` only ever throws APIError
.mapError { $0 as! APIError }
}

关于json - 如果第一次解码失败使用 Combine 和 Swift 解码另一个响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63143578/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com