I am trying to decode the downloaded JSON into a structure with the following code.
static func request(url: URL) -> AnyPublisher<SomeDecodableStruct, Error> {
return URLSession.shared.dataTaskPublisher(for: url)
.map { $0.data }
.decode(type: SomeDecodableStruct.self, decoder: JSONDecoder())
.eraseToAnyPublisher()
}
However, if processing fails, I would like you to return information on whether the request processing failed or the decoding processing failed.
Therefore, I defined the FailureReason
enum that conforms to the Error
protocol as follows.
enum FailureReason : Error {
case sessionFailed(error: URLError)
case decodingFailed
}
static func request(url: URL) -> AnyPublisher<SomeDecodableStruct, FailureReason> {
// ???
}
How do I define a request(url:)
that satisfies this FailureReason
?